Cheatsheet x in y results in True if x is a member of sequence y. print(x) print the value in x and a new line.
Python-3.x x not in y results in True if x is not a member of sequence y. prin(x, y) print the value in x and a space.
print(x,y, sep=“...”) prints the values of x, y separated by “...” instead
1. Storing values in variables 7. Identity Operators of the default space. print(x, y, sep=“’’, end = “::”) prints the values
x = 5 stores the integer 5 in x x is y Evaluates to True if the variables on either side of the operator of x, y seperated by a tab and instead of ending with a newline
y = 2.5 stores the float 2.5 in y point to the same object.
s = “Hello World” stores string Hello World in s x is not y Evaluates to False if the variables on either side of the op-
erator point to the same object. 12. if statement
i f x > 0:
2. Boolean Constants 8. Conversions print ( ‘ ‘ positive ’ ’ )
The Boolean constants are True and False. int(“65”) gives the integer 65
Note the capitalization. int(65.75) gives the integer 65
float(“65.75”) gives the float 65.75 13. if...else statement
float(65) gives the float 65.0 i f x > 0:
3. Arithmetic Operations str(65) gives the string “65” print ( ‘ ‘ positive ’ ’ )
str(65.75) gives the string “65.75” else :
x + y computes the sum of x and y
int(“65.75”) gives an error p r i n t ( ’ ’ not p o s i t i v e ’ ’ )
x - y computes the value of y subtracted from x
x * y computes the product of x and y
x ** y computes x raised to y 9. Indentation
14. if...elif statement
x % y computes the remainder when x is divided by y I n P y t h o n b l o c k s a r e i d e n t i f i e d by
x / y computes the float value of x divided by y. i f x > 0:
indentation .
17 / 4 gives 4.25 print ( ‘ ‘ positive ’ ’ )
statement 1 :
x // y computes the quotient when x is divided by y e l i f x < 0:
statement 2
17 // 4 gives 4 print ( ‘ ‘ negative )
statement 3
else :
statement 1 must end in a colon. It can be an if statement, while p r i n t ( ‘ ‘ Ze ro ’ ’ )
4. Comparison Operations statement, for statement or a def statement
Returns Boolean values True or False Similarly, 15. while statement
x == y checks if x is equal to y statement 1 x = 1
x != y checks if x is not equal to y statement 2 while x < 10:
x > y checks if value in x is greater than y statement 3 p r i n t ( ‘ ‘ The v a l u e o f x i s ’ ’ , x )
x ≥ y checks if x is greater than or equal to y statement 4 x += 1
x < y checks if value in x is less than that in y statement 5
x ≤ y checks if value in x is less than or equal to y statement 6 Prints x value from 1 to 9
x < y < z checks if value in y is in between x and z
Use only 4 spaces for an indent. 16. Defining Strings
s = "I am a string"
5. Logical Operations enclosed in double quotes.
x == 5 and y != 7 returns True if both conditions are True 10. Simple Input s = ’He said "Good Morning", to the class’
x == 5 or y != 7 returns True if either condition is True x = input() for taking input. use single quotes if there is a double quote in the string.
not x > 7 not negates the condition x = input("Enter number: ") display a prompt while taking input. s = "It’s time"
The value given by input is always a string. use double quotes if there is a single quote in the string.
6. Membership Operators 11. Simple Output 17. Accessing characters in strings
s[0] accesses the first character in the string s. d e f add_one ( x ) : pr.extend([17, 19, 21]) appends 17, 19, 21
s[4] accesses the fifth character in the string s. return x + 1 pr becomes [2, 3, 5, 7, 11, 13, 17, 19, 21]
Indexing starts with 0 for the first character. Operations mentioned above modify the list itself.
s[-1] accesses the last character in the string s. defines the add_one function that takes one argument and returns
s[-2] accesses the last but one character in s. the value of argument plus one.
26. for loop
Negative indexing starts with -1 from last. d e f getMax ( x , y ) :
f o r i in pr :
if x > y:
print ( i )
return x
18. Slicing strings return y iterates over the list pr one item at a time.
s = “Hello World” defines the getMax function that takes two arguments and returns
s[3:] returns “lo World” the greater one from them. 27. dictionaries
substring from character with index 3 to end. mm2num = {“jan”: 1, “feb”: 2, “mar”: 4}
s[:7] returns “Hello W” creates the dictionary mm2num
substring from start to character with index 6. mm2num[“feb”] gives the corresponding value, 2
22. Calling functions
s[3:7] returns “lo W” mm2num[“mar”] = 3
substring from character with index 3 to character with index add_one(5) returns 6.
x = add_one(8) stores the value 9 in x. changes the value for the key ‘mar” to 3
6. mm2num[“apr”] = 4
s[2:-2] returns “llo Wor” x = add_one(x) increments x by one.
y = getMax(4, 8) stores the return value 8 in y. creates the key “apr” with 4 as the value
substring from third character to the third character from the mm2num.values() returns list of values, [1, 2, 3, 4]
end. mm2num.keys() returns list of keys,
23. lists
[“jan”, “feb”, “mar”, “apr”]
pr = [2, 3, 5, 7, 11, 13] creates the list pr.
19. string methods len(pr) returns the length of the list, 6
28. sets
s = “Hello” + ‘World” stores HelloWorld in s. 15 in pr checks for the presence of 15 in the list pr.
pr + [17, 19, 23] adds the lists and returns a new list. prs = set([2, 3, 2, 5, 3, 7, 7, 2, 3])
len(s) length of the string s creates the set set([2, 3, 5, 7]) and stores in prs.
"ell" in s checks for the presence of “ell” in s. ods = set([1, 3, 5, 9, 3, 7, 7, 9, 3])
s.lower() returns “helloworld” 24. slicing lists
creates the set set([1, 3, 5, 7, 9]) and stores in ods.
a new string with characters of s, in lower case. pr[0] accesses the first item, 2. prs | ods gives the union of the sets, set([1, 2, 3, 5, 7, 9])
s.upper() returns “HELLOWORLD” pr[-4] accesses the fourth item from end, 5. prs & ods gives the intersection of the sets, set([3, 5, 7])
a new string with characters of s, in upper case. pr[2:] accesses [5, 7, 11, 13] ods - prs gives the difference of sets
s.replace(“l”, “m”) returns “Hemmo Wormd” list of items from third to last. items in ods that are not in prs, which is set([1, 9])
a new string with all the l replaced with m. pr[:4] accesses [2, 3, 5, 7] ods ˆ prs gives the symmetric difference
s.split() returns [“Hello”, “World”] list of items from first to fourth. items in ods or in prs but not in both, set([1, 2, 9])
a list of words in the string. pr[2:4] accesses [5, 7]
All the above operations return new strings. The original string list of items from third to fifth.
29. Reading from files
remains unaltered. pr[1::2] accesses [3, 7, 13]
alternate items, starting from the second item. f i l e L o c = ‘ ‘ / home / t s p r i n t / p r i m e s . t x t ’ ’
f o r l i n e i n open ( f i l e L o c ) :
20. range function prime = i n t ( l i n e )
range(8) returns list of numbers from 0 to 7. p r i n t ( prime * prime )
range(3, 13, 2) returns odd numbers from 3 to 12. Data in the file is read as a string line by line.
range returns a “generator”, converts it to list to see the values,
Example: print(list(range(8)))
25. list methods
21. Defining functions pr.append(17) adds 17 at the end of the list pr.
pr becomes [2, 3, 5, 7, 11, 13, 17]