Python Cheatsheet
Honestly, I don’t really care if you struggle with syntax. I’m making you a simple cheatsheet while this does not have everything it has the essesstials.
Print it. Pin it on the wall. Bookmark this page. Keep this handy.
Variables
<name of variable> = <value>
pi = 3.14
name = "Harsh Deep"
age = 18
Types
Integers
These are just whole numbers. Positive, negative and zero.
Ex. 3, 4, 0, -4234, 523542354
To convert:
>>> int("42")
42
Pretty reliable to do math on. Careful though, division only returns the whole number part.
Floats
Same as integers, but can have decimal parts.
Ex. 13423.4234
, 0.0
, -42.0
To convert:
>>> float("2.718")
2.718
You can do most math on these, but careful because computers are weird with rounding.
Strings
These are how programs represent strings. You can’t do math on
Ex. ""
, "Harsh Deep"
, '42'
To convert:
>>> str(42)
'42'
>>> str(1000.5)
'1000.5'
For length:
>>> len("Harsh")
5
Math
>>> 1 + 3
4
>>> 2.0 - 0.5
1.5
>>> 1.5 * 9
13.5
>>> 18/4 # Float division
4.5
>>> 18//4 # Integer division
4
>>> 5**2 # 5^2 or 5 to the power of 2
25
Other math is pretty similar.
Blocks
How python marks a chunk of code, useful for the next sections.
block_start:
line1
line2
line3
So it’s started with a :
, then every line is indented one level (4 spaces). To mark the end of the block leave an empty line or start another block.
Nested blocks
block_start1:
line1
line2
block_start2:
line3
line4
line5
If conditions
For branching
if grade > 90:
print("A")
elif grade > 80:
print("B")
else:
print("C")
Boolean logic
Inequalities. Either output True
or False
>>> 5 == 5
True
>>> 5 > 4
True
>>> 5 < 4
False
>>> 5 >= 5
True
>>> 4 <= 2
False
Combining:
# And gives true only when both are true
>>> True and True
True
>>> True and False
False
# Or gives true when atleast one is true
>>> False or False
False
>>> False or True
True
>>> True or True # Both being true is still true
True
# Not just flips in
>>> not True
False
>>> not False
True
Loops
While
Simple looping
while(condition):
what_ever_you_wanna_loop
The loop stops when the condition becomes false. It’s basically like a loop version of an if condition.
while(not_good_at_programming && i_want_to_learn_to_get_better):
practice()
learn()
ask()
teach()
create()
For
Looping over something or a certain number of times.
Ranges - a range of numbers
for i in range(10):
print(i)
Ranges with a start and end
for teen in range(13, 19):
print(teen)
For strings
for letter in "Harsh Deep":
print(letter)
Lists
Python3 has LinkedList, or array with no fixed length like data structure for storing many elements.
Delcare
Empty
empty_list = []
Non empty
primes = [1, 2, 3, 5, 7, 11, 13, 17, 19, 21, 23, 29]
Read
>>> squares = [1, 4, 9, 16, 25]
>>> squares[0] # First element
1
>>> squares[1] # Second element
4
>>> squares[4] # Last element
25
>>> squares[-1] # Better way to get last element
25
>>> squares[-2] # Second last element
16
Write
>>> squares = [1, 4, 9, 16, 25]
>>> squares[2] = 36 # Just use an equals to change whatever
>>> squares
[1, 4, 36, 16, 25]
Append
You can add to the end of the list.
>>> squares = [1, 4, 9, 16, 25]
>>> squares.append(36)
>>> squares
[1, 4, 9, 16, 25, 36]
Pop
Remove the last element of the list.
>>> squares
[1, 4, 9, 16, 25, 36]
>>> squares.pop()
36
>>> squares
[1, 4, 9, 16, 25]
Insert
>>> numbers = [1, 2, 4, 5]
>>> numbers.insert(2, 3) # Oops forgot to add 3, let's add 3 at position 2
>>> numbers
[1, 2, 3, 4, 5]
Functions
The python (and lots of other languages) way of named blocks of code.
>>> def add(number1, number2):
... return number1 + number2
...
>>> print(add(1, 3))
4
note the empty line. Return is how we show the output of our function.
Execution stops when we hit return.
No parameters
>>> def father_of_cs():
... return "Alan Turing"
...
>>> father_of_cs()
'Alan Turing'
No return
>>> def hi():
... print("Hi how are you doing")
...
>>> hi()
Hi how are you doing
Basically if you don’t need to return something just don’t include it.
Other cheatsheets
And the best: the one you make for yourself. I highly highly suggest you do one for yourself. It won’t have the parts you don’t care and only the stuff you want to remember.