






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







type(3) <type 'int'> type(3.14) <type 'float'> pi = 3. type(pi) <type 'float'>
num = 3 num = num - 1 print num 2 num = num + 10 print num 12 num += 10 print num 22 num -= 12 print num 10 num *= 10 num 100
“string list” ‘string list’ ‘string list’ ‘string list’
“I can’t do that” “I can’t do that” “He said \“no\” to me” “He said “no” to me”
a = “first” b = “last” a + b ‘firstlast’
a = “first ” a + b
“first last”
w='woah!' w.upper() 'WOAH!'
w='WOAH!' w.lower() 'woah!'
r='rule' r.replace('r','m') 'mule'
numbers=['1','2','1','2','2'] numbers.count('2')
3
"{0} is a lot of {1}".format("Python", "fun!") “string list” ‘string list’ >>> ‘string list’ ‘string list’ ## Now what if you have a quote in the middle of the string? Python needs help to ## recognize quotes as part of the English language and not as part of the Python ## language. >>> “I can’t do that” “I can’t do that” >>> “He said \“no\” to me” “He said “no” to me” ## Now you can also join strings with use of variables as well. >>> a = “first” >>> b = “last” >>> a + b ‘firstlast’ ## If you want a space in between, you can change a to the word with a space after. >>> a = “first ” >>> a + b “first last” ## There are also different string methods for you to choose from as well - like ## upper , lower , replace , and count. ## Upper does just what it sounds like, changes your string to have uppercase ## letters. >>> w='woah!' >>> w.upper() 'WOAH!' ## Lower is just that as well, keeping your string in lower case letters. >>> w='WOAH!' >>> w.lower() 'woah!' ## Replace allows you to replace any character with another character. >>>r='rule' >>>r.replace('r','m') 'mule' ## Count lets you know how many times x appears in the string (can be numbers or ## a string of words as well). >>>numbers=['1','2','1','2','2'] >>>numbers.count('2') 3 ## You can also format strings with the format method. >>> "{0} is a lot of {1}".format("Python", "fun!") 'Python is a lot of fun!'
Booleans
fruits = ['apple','lemon','orange','grape'] fruits ['apple', 'lemon', 'orange', 'grape']
fruits[2] ‘orange’
fruits[-2] ‘orange’
len(fruits) 4
2. Collections Lists
fruits.append('blueberry') fruits ['apple', 'lemon', 'orange', 'grape', 'blueberry'] fruits.append('tomato') fruits ['apple', 'lemon', 'orange', 'grape', 'blueberry', 'tomato'] fruits.pop() 'tomato' fruits ['apple', 'lemon', 'orange', 'grape', 'blueberry']
'apple' in fruits True 'tomato' in fruits False
words={'apple':'red','lemon':'yellow'} words {'lemon': 'yellow', 'apple': 'red'} words['apple'] 'red' words['lemon'] 'yellow'
Dictionaries
num = 20 if num == 20: ... print 'the number is 20' ... else:
... print 'the number is not 20' ... the number is 20
num = 21 if num == 20: ... print 'the number is 20' ... else: ... print 'the number is not 20' ... the number is not 20
num = 21 if num == 20: ... print 'the number is 20' ... elif num > 20: ... print 'the number is greater then 20' ... else: ... print 'the number is less then 20' ... the number is greater then 20
Loops
colors =('red','blue','green') colors ('red', 'blue', 'green') for favorite in colors: ... print "I love " + favorite ...
'tomato' in fruits False ## A dictionary optimizes element lookups. It uses keys and values, instead of ## numbers as placeholders. Each key must have a value. You can used a word to ## look up a value. >>> words={'apple':'red','lemon':'yellow'} >>> words {'lemon': 'yellow', 'apple': 'red'} >>> words['apple'] 'red' >>> words['lemon'] 'yellow' ## This will also work with numbers. Dictionaries >>> num = 20 >>> if num == 20: ... print 'the number is 20' ... else: ... print 'the number is not 20' ... the number is 20 >>> num = 21 >>> if num == 20: ... print 'the number is 20' ... else: ... print 'the number is not 20' ... the number is not 20 ## You can also add an elif clause to add another condition. >>> num = 21 >>> if num == 20: ... print 'the number is 20' ... elif num > 20: ... print 'the number is greater then 20' ... else: ... print 'the number is less then 20' ... the number is greater then 20 ## There are 2 kinds of loops used in Python. The For loop and the While loop. For ## loops are traditionally used when you have a piece of code which you want to ## repeat n number of times. They are also commonly used to loop or iterate over ## lists. Loops >>> colors =('red','blue','green') >>> colors ('red', 'blue', 'green') >>> for favorite in colors: ... print "I love " + favorite ... I love red I love blue I love green
num = 1 num 1 while num <=5: ... print num ... num += 1 ... 1 2 3 4 5
4. Functions