Vous êtes sur la page 1sur 2

2.1 Expressions, Variables, and Assignments (can be named anything. Ex. t=amy.Turtle, s=bob.

Turtle)
** exponents -case sensitive
// division that returns integer values t.forward(distance) moves turtle 't' in the direction the turtles headed
% modulus (computes the remainder) by 'distance' pixels
abs() absolute value t.left(angle) rotates turtle counterclockwise by 'angle' degrees
min() minimum of whatever is in the parenthesis t.right(angle) rotates turtle clockwise by 'angle' degrees
max() maximum of whatever is in the parenthesis t.circle(radius) draws a circle with given 'radius'; the center of the
== equality symbol circle is 'radius' pixels to the left of the circle
<=, >= less than or equal to, greater than or equal to
!= does not equal (ex. 3!=4) 3.1 Python Programs
true has integer value 1 print( )
false has integer value 0 -prints whatever is contained in the ( )
and -if it is a string, remember to put quotations
- true and true = true input( )
- true and false= false -helps program interact with the user
- false and true = false -execution of program will temporarily stop until user types what is
- false and false = false needed and clicks the enter/return key
or -the input( ) function will always treat whatever the user types as a
- true or true= true string
- true or false= true eval( )
- false or true= true -takes a string as input and evaluates string as if it were a Python
- false or false= false expression
-can be used with the input( ) function
2.2 Strings
x in s true if string x is a substring of s, and false otherwise 3.2 Execution Control Structures
indexing operators general format of if statements
-are from 0 to (length of the string-1) if <condition>:
-or from -1 to -(length of string) <indented code block 1>
note: string are NOT mutable. aka the contents can't be changed else:
<indented code block 2>
2.3 Lists <non-indented statement>
lists -enclosed within brackets, separated by commas
-Ex. pets= ['goldfish', 'cat', 'dog'] general form of for loops
len(lst) -length of list "lst" for <variable> in <sequence>:
min(lst) -smallest item on list "lst" <indented code block>
sum(lst) -sum of items in list "lst" <non-indented code block
lst.count(item) -returns the number of occurrences of "item" in list range ( )
"lst" -iterate the integers from 0 to n-1
lst.index(item) returns the index of the first occurrence of "item" in list -only if there is one variable in the parenthesis
"lst" range(start, end) iterate a sequence when it starts at a nonzero
Ex. avg of a list inside a list number
def avg(x): range(start, end, step) generate sequences that uses a step size other
for i in len(x): than 1
newx=sum(x[0]/len(x[0])
print(newx) 3.3 User-Defined Functions
general format of defining a function
2.4 Objects and Classes def<function name>
operator order: <indented function body>
1. [expressions...] notes on the print function - can print multiple things (ex. strings and
2. x[ ], x[index: index] variables) and is separated by a comma.
3. ** Ex.
4. +x, -x >>> x=3
5. *,/,//, % >>> y=5
6. +, - >>> print('The sum of', x, 'plus', y, 'is', x+y)
7. in, not in, <, <=, >, >=, <>, !=, == The sum of 3 plus 5 is 8
8. not x print(x, end= ' ') instead of starting a new line, it makes it into a space.
9. and Ex.
10. or >>> for i in range(10):
print(i, end=' ')
2.6 Case Study: Turtle Graphics Objects 0123456789
note: you must first import the turtle module and then instantiate a print vs return -the print function only prints statements
screen object -return evaluates function
>>> import turtle swapping terms:
then, you must create a Turtle object a,b = b,a (swapped a to b and b to a)
>>>t= turtle.Turtle
4.1 Strings, Revisited 5.2 for Loop and Iteration Patterns
if you have quotes in a string: use the other quotation counter loops for i in range(len(lst))
escape sequence - denoted: \ accumulator loop accumulates integers strings, etc using a for loop
- used to indicate that a quote is not the string delimiter but is a part shortcut for mySum = mySum + num:
of the string - mySum += num
- must use the print( ) function nested loop a loop statement contained inside another loop statement
\n -another use for the escape sequence
-starts a new line example of a nested loop
slicing -takes a string and slices it. 1. def nested(n):
- denoted: str[i:j] ->it is a substring of string 'str' that starts at index i 2. for j in range(n):
and ends at index j-1. 3. for i in range(n):
s.count(target) the number of occurrences of string 'target' in string s 4. print(i, end=' ')
s.split(sep) a list of substrings of string s, obtained using delimiter 5. print( )
string 'sep'; the default delimiter is the blank space
5.3 More on Lists: Two-Dimensional Lists
4.2 Formatted Outputs index of a two dimensional list - denoted lst[row][column ]
sep=' ' -changes what the values are separated by
-Ex. 5.4 while Loop
>>> print( n, r, name, sep=';') general format of the while loop
5;1.66667;Ida while <condition>:
format( ) - denoted '{ }, { }, { }'.format(first, second, third) <indented code block>
Example of a format( ) <non-indented statement>
>>> print('{ }, { } { }, { } at { }:{ }:{ }'.format(weekday, month, day, year, while loop -good for situations in which we need to iterate but we do
hour, minute, second)) not know how many times
- idea is to use the range( ) function
Wednesday, March 10, 2010 at 11:45:33 - keeps iterating the indented code block until it becomes false
example of a while loop
4.3 Files >>> n=5
make func to open file - infile= open('example.txt', 'r') >>> while n>0:
‘r’ reading mode (default) >>> print n
‘w’ writing mode; if the file already exists, its content is wiped >>> n=n-1
infile.read( ) read characters from file 'infile' until the end of the file >>> print('Blast off!')
and return characters as a string Break/ continue
outfile.write(s) write string s to file 'outfile' - <loop 1> (for loop or while loop)
file.close( ) close the file o <loop 2>:
Ex. stringCount( ) –returns the number of occurrences of line  if<condition>
def stringCount(x, t):  if <condition2>
infile=open(x)  break
content=infile.read()
infile.close() 6.1 Dictionaries
return content.count(t) general form of dictionaries - denoted { }
Ex. -{<key 1>:<value 1>, <key 2>:<value 2><key i>...<value i>}
def getListBegin(c, ifile, ofile): - access the value using the key and [ ]
for i in ifile: example of dictionary
if c in i[0]: >>> days= {'Mo': 'Monday', 'Tu': 'Tuesday', 'We': 'Wednesday', 'Th':
ofile.write(i) 'Thursday }
>>> days['We']
def getListEnd(c, ifile, ofile): 'Wednesday'
for i in ifile: add values to the dictionary
if c in i[-2]: #last index is \n >>> days['Fr']= 'Friday
ofile.write(i) >>> days
{'Fr': 'Friday', 'Mo': 'Monday', 'Tu': 'Tuesday', 'We': 'Wednesday', 'Th':
5.1 Decision Control and the if Statement 'Thursday }
general format of if/ elif statements
if <condition 1>:
<indented code block>
elif <condition 2>:
<indented code block 2>
else:
<indented code block last>
<non-indented statement>
elif statement - stand for 'else if'
- for three-way (or more) decisions
note - order is important in multi-way statements

Vous aimerez peut-être aussi