COMPSCI 101L Lecture 29: CS Notes up to 11-22

49 views48 pages

For unlimited access to Class Notes, a Class+ subscription is required.

9/5 Lecture
Use / for division (NOT //)
Ex) 7/2 will be 3
     7.0/2 will be 3.5
Variable name of a storage location, holds a value
·      Use = to assign value to a variable
Type
● A variable can store data of a certain type
● int, float, str
Operators for numbers
● Arithmetic: + - * / % **
● Built-in functions: pow, abs, round, int, float
○ Ex: pow(2,3)+round(1.6)
Eclipse 3 ways to run
1.     Write program and store in file
a.     Creat a PyDev project folder for programs
b.     Create a PyDev module for each program (file)
c.      Run in console
2.     Creat an APT in Eclipse and run on web
3.     Run interactively
a.     Open PyDev console
b.     Execute each line as typed
c.      Code not saved
Strings MUST be in quotes
You can say
Print type(x)
<type ‘int’>
Tells you what type it is
Function
● Def functionName(parameters):
● Block of code
● Comment is always between “”” “”” (for us to read)
Def sum(a,b):
Return a+b
Print sum(3,5)
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 48 pages and 3 million more documents.

Already have an account? Log in
Print sum(1.4)
Print sum(“cat”,”dog”)
Returns:
8
5
catdog (concatenation)
BUT cannot concatenate str and int objects must be str, str or int, int
APT (Automated/Algorithmic Problem Testing)
Ex)
New→ PyDev Project→ Create a new Python module Template→ Module Main
BMI Example:
Name project: myAPTs Sec02
Name: BMI.py
module main
def calculate(weight,height):
(will indent means ready for you to calculate function)
answer = 703.0695*weight/(height*height)
return answer
if name
==’_main_’:
print calculate (200,60)
APT Test (this will test to see if green)
To submit APT, go to Submit, then REFLECT
Placing a comma after string places next string along same line
Def OldMacAnimal (animal,sound):
When you see pig, change to “animal” and change oink oink to “sound”
“With a”, sound, “, sound, “here,”,
OldMacAnimal(“cow”, “moo”)
Print
OldMacAnimal(“pig”, “oink”)
*Make sure to read both pages/totem how to page for assignment 2
*Pay attention to requirements
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 48 pages and 3 million more documents.

Already have an account? Log in
● Variable can be file
● If you have “return,” you have to “print” etc., aka CALL the function
● Output for bit.ly questions:
11
answer is 11
Hello Sue
Name is None
Hello Sue
Hello Jo
None
● Name = hw(“Sue”) does not have a return value because it doesn’t have a return
statement
9/12 Lecture
● Check submit history on Ambient to check assignments
● Best explanation for code 'for w in f:'
Looping over the lines from the file word.txt
If statements:
If word.endswith(“ch”):
ret = word + “es”
Splicing: take everything except the last character and add “ves”
If word.endswith(“f”):
Ret. = word[:-1]+”ves
One line at a time
#f is the file
# for each line in the file
#remove carriage return from line
#process word
if __name__ == "__main__":
f = open("words.txt") for w in f:
w = w.strip()
print w, pluralize(w)
Split function knows where to split using white space, if  3 blanks between 3 words, assumes
you want to get rid of those blanks and split it there
Another way for the main Iterate over words, not lines
if __name__ == "__main__":
f = open("words.txt")
all = f.read()
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 48 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
1 Booster Class