Python Cheatsheet for
Quick Reference
WEEK 1
1. print() : method to print on the console.
print(“Hello World”)
2. Variables are used to store values. The values can be
number, text etc.
3. a = 10 #int
n = 10.3 #float
s = “Student” #str
t = True #bool
4. input() method is used to take input from user in the
form of string
5. type() method is used to get datatype of a variable or
value
6. Typecasting:to change a datatype from one to another.
a = int(10.1) #This will convert 10.1 to 10
a1 = int(10.1) #This will convert 10.1 to 10
b = float(10) #This will convert 10 to 10.0
c = str(10.3) #This will convert 10.3 to ‘10.3’
d = bool(0) #False
e = bool(10) #True
f = bool(-10) #True
g = bool(‘India’) #True
i = bool(‘’) #False
7. Arithmetic operators
-> Addition (int or float) , Concatenation (str)
, - -> Subtraction
* -> Multiplication (int or float) , Repetition (str)
/ -> Division
// -> Floor Division (Integer division)
% -> Modulus operator (remainder after division)
** -> Exponentiation (power)
8.Relational operators(It will compare and give True or False)
> -> Greater than
< -> Less than
>= -> Greater than or equal
<= -> Less than or equal
== -> Equal
!= -> Not equal
9. Logical operators
and -> Returns True if both LHS and RHS are true
or -> Returns False if both LHS and RHS are false
not -> Returns negation of given operand
10.Strings
s1 = ‘Hello’
Or
s2 = “World”
11.String indexing (starts from 0)
s1[0] -> H
s1[-1] -> o
12.String slicing
s1[0:3] -> Hel
13.String comparison - (order in English dictionary)
‘apple’ > ‘one’ -> False
‘apple’ > ‘ant’ -> True
14.String length
len() method gives length of a string.
len(s1) -> 5
WEEK 2
,1. Comments: A section of our program that is ignored by the
compiler(to increase the readability of the code).
-> # This is a comment
-> ‘’’ This is
A multiline
Comment ‘’’
2. Dynamic Typing: The type of a variable in Python is
determined by the value it stores and changes with it.
-> message = 'Hello'
-> message = 10
3. Multiple Assignments
Assign multiple values or the same value to multiple variables
x, y, z= 1 , “Hi” , False x=y=z=100
print(x,y,z) -> 1 “Hi” False print(x,y,z) -> 100 100 100
4. Del x - > Delete the reference x
5. Shorthand Operators
+= , -= , *= , /= ….etc , Basically any operator with the “=” sign.
num = num + 1 is same as num +=1
num = num * 2 is same as num *=2
6. in operator - Tells whether something is inside/part of the other thing
(similar to the English definition)
“ IT “ in “ IIT Madras “ - > True # searches for the string “IT” in “IIT Madras”
“ ads “ in “ IIT Madras “ - > False
7. Chaining Operators - Using multiple relational operators (<,>,==,<=,>=,!
=) together
x=5
print( 1 < x < 6 ) - > True
, print( x < 2 * x < 3 *x > 4 *x ) - > False
# above can be looked as
print( 5 < 10 < 15 > 20 ) - > False # 15 is not greater than 20
8. Escape Characters - To insert characters that are illegal in a string, use an
escape character.
An escape character is a backslash \ followed by the character you
want to insert.
print(‘It’s Raining’) - > Error #All Characters after second ‘ (single
quote) are not considered as a part of the string (s Raining)
# So we use \ before the ‘ print(‘It\’s Raining’) - > It’s Raining
Other Escape Characters - \n : Cursor moves to the next line
\t: Shifts cursor by 5 spaces
9. Multiline Strings: Similar to multiline comment
S = ‘ ‘ ‘ A multiline
String ‘ ‘ ‘
print(S) - > A multiline
String
10. In Python, variable names must start with a letter (a-z, A-Z) or an
underscore (_), and can be followed by letters, digits (0-9), or underscores.
They are case-sensitive and cannot use Python-reserved keywords.
Variable name cannot start with any digit (0-9)
Quick Reference
WEEK 1
1. print() : method to print on the console.
print(“Hello World”)
2. Variables are used to store values. The values can be
number, text etc.
3. a = 10 #int
n = 10.3 #float
s = “Student” #str
t = True #bool
4. input() method is used to take input from user in the
form of string
5. type() method is used to get datatype of a variable or
value
6. Typecasting:to change a datatype from one to another.
a = int(10.1) #This will convert 10.1 to 10
a1 = int(10.1) #This will convert 10.1 to 10
b = float(10) #This will convert 10 to 10.0
c = str(10.3) #This will convert 10.3 to ‘10.3’
d = bool(0) #False
e = bool(10) #True
f = bool(-10) #True
g = bool(‘India’) #True
i = bool(‘’) #False
7. Arithmetic operators
-> Addition (int or float) , Concatenation (str)
, - -> Subtraction
* -> Multiplication (int or float) , Repetition (str)
/ -> Division
// -> Floor Division (Integer division)
% -> Modulus operator (remainder after division)
** -> Exponentiation (power)
8.Relational operators(It will compare and give True or False)
> -> Greater than
< -> Less than
>= -> Greater than or equal
<= -> Less than or equal
== -> Equal
!= -> Not equal
9. Logical operators
and -> Returns True if both LHS and RHS are true
or -> Returns False if both LHS and RHS are false
not -> Returns negation of given operand
10.Strings
s1 = ‘Hello’
Or
s2 = “World”
11.String indexing (starts from 0)
s1[0] -> H
s1[-1] -> o
12.String slicing
s1[0:3] -> Hel
13.String comparison - (order in English dictionary)
‘apple’ > ‘one’ -> False
‘apple’ > ‘ant’ -> True
14.String length
len() method gives length of a string.
len(s1) -> 5
WEEK 2
,1. Comments: A section of our program that is ignored by the
compiler(to increase the readability of the code).
-> # This is a comment
-> ‘’’ This is
A multiline
Comment ‘’’
2. Dynamic Typing: The type of a variable in Python is
determined by the value it stores and changes with it.
-> message = 'Hello'
-> message = 10
3. Multiple Assignments
Assign multiple values or the same value to multiple variables
x, y, z= 1 , “Hi” , False x=y=z=100
print(x,y,z) -> 1 “Hi” False print(x,y,z) -> 100 100 100
4. Del x - > Delete the reference x
5. Shorthand Operators
+= , -= , *= , /= ….etc , Basically any operator with the “=” sign.
num = num + 1 is same as num +=1
num = num * 2 is same as num *=2
6. in operator - Tells whether something is inside/part of the other thing
(similar to the English definition)
“ IT “ in “ IIT Madras “ - > True # searches for the string “IT” in “IIT Madras”
“ ads “ in “ IIT Madras “ - > False
7. Chaining Operators - Using multiple relational operators (<,>,==,<=,>=,!
=) together
x=5
print( 1 < x < 6 ) - > True
, print( x < 2 * x < 3 *x > 4 *x ) - > False
# above can be looked as
print( 5 < 10 < 15 > 20 ) - > False # 15 is not greater than 20
8. Escape Characters - To insert characters that are illegal in a string, use an
escape character.
An escape character is a backslash \ followed by the character you
want to insert.
print(‘It’s Raining’) - > Error #All Characters after second ‘ (single
quote) are not considered as a part of the string (s Raining)
# So we use \ before the ‘ print(‘It\’s Raining’) - > It’s Raining
Other Escape Characters - \n : Cursor moves to the next line
\t: Shifts cursor by 5 spaces
9. Multiline Strings: Similar to multiline comment
S = ‘ ‘ ‘ A multiline
String ‘ ‘ ‘
print(S) - > A multiline
String
10. In Python, variable names must start with a letter (a-z, A-Z) or an
underscore (_), and can be followed by letters, digits (0-9), or underscores.
They are case-sensitive and cannot use Python-reserved keywords.
Variable name cannot start with any digit (0-9)