Install Python . Type py in terminal to enter into command shell and see the version. For me it is Python 3.10.1 Type exit() or Ctrl+Z then Return to exit. Get started Create a file file.py with the following code print("hello world") , run it from the terminal with command py file.py and get output hello world . So Python is installed and executable. Print Quotes
print("hello world") # hello world
print('hello world') # hello world
Line break
# print w/o line break
print("line 1 ", end = "")
print("line 2 ", end = "")
# line 1 line 2
Concatenate
print("5 + 2 =", 5 + 2) # 5 + 2 = 7
var1 = "like"
var2 = "to"
var3 = "eat"
print('I', var1, var2, var3) # I like to eat
Interpolation
print("{} + {} = {}".format(1, 2, 3)) # 1 + 2 = 3
Duplicate
print("hi " * 5) # hi hi hi hi hi
Format
print("%c is my %s letter and my number %d number is %.5f" %
('X', 'favorite', 1, .14))
# X is my favorite letter and my number 1 number is 0.14000
Comments
# comment
'''
multiline comment
'''
Variable
name = "John"
print(name)
Types Types
"five" # <class 'str'>
5 # <class 'int'>
5.5 # <class 'float'>
range(3) # <class 'range'> # nums from 0 to 2
["apple", "banana", "cherry"] # <class 'list'>
{"apple", "banana", "cherry"} # <class 'set'>v # unchangeable
("apple", "banana", "cherry") # <class 'tuple'> # ordered, unchangeable
{"name" : "John", "age" : 36} # <class 'dict'>
True # <class 'bool'>
# others
1j # <class 'complex'>
b"Hello" # <class 'bytes'>
bytearray(5) # <class 'bytearray'>
memoryview(bytes(5)) # <class 'memoryview'>
Check type
print(type(5)) # <class 'int'>
Convert type
str("Hello World")
bool(5)
int(20)
float(20.5)
list(("apple", "banana", "cherry"))
tuple(("apple", "banana", "cherry"))
range(6)
dict(name="John", age=36)
set(("apple", "banana", "cherry"))
#others
frozenset(("apple", "banana", "cherry"))
complex(1j)
bytes(5)
bytearray(5)
memoryview(bytes(5))
Operators Math
5 + 2 # 7
5 - 2 # 3
5 * 2 # 10
5 / 2 # 2.5
5 % 2 # 1
5 ** 2 # 25
5 // 2 # 2
Comparison
==, !=, >, <, >=, <=
No triple equal === Logical
and, or, not
String Single vs multiline
str = "single line string"
str = "single line string with quotation mark \""
str = '''
multiline
text
goes inside triple quotes
'''
Concatenation
print('hello ' + 'John') # hello John
First N chars
str = "123456789"
print(str[0:4]) # 1234
Last N chars
str = "123456789"
print(str[-5:]) # 56789
Up to N chars
str = "123456789"
print(str[:-5]) # 1234
Capitalize
print("hi".capitalize()) # Hi
Find
print("Hello my friend".find('m')) # 6
Length
print(len("Hello my friend")) # 15
Is number
print("1234".isalnum()) # True
Is alpha
print("abc".isalpha()) # True
Replace
print("abc".replace("b", "B")) # aBc
Strip
print(" abc ".strip()) # abc
Split
print("a b c".split(" ")) # ['a', 'b', 'c']
print(list("a b c")) # ['a', ' ', 'b', ' ', 'c']
Split with empty string
print("a b c".list(" ")) # ['a', 'b', 'c']
List (array) Create
grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas']
Access
grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas']
grocery_list[1] # Tomatoes
grocery_list[0:2] # ['Juice', 'Tomatoes']
arr = [1, 2, [3, 4]]
arr[2][0] # 3
for x in [1, 2, 3, 4, 5]:
print(x, ' ', end="") # 1 2 3 4 5
Change
arr = [1, 2, 3]
arr[0] = 666
print(arr) # [666, 2, 3]
Add
arr = [1, 2, 3]
arr.append(4)
print(arr) # [1, 2, 3, 4]
arr.insert(0, 0)
print(arr) # [0, 1, 2, 3, 4]
Remove, delete
arr = [0, 1, 2, 3, 4]
arr.remove(0)
print(arr) # [1, 2, 3, 4]
del arr[0]
print(arr) # [2, 3, 4]
Reverse
arr = [0, 1, 2, 3, 4]
arr.reverse()
print(arr) # [4, 3, 2, 1, 0]
Concatenate
arr1 = [0, 1, 2]
arr2 = [3, 4, 5]
arr3 = arr1 + arr2
print(arr3) # [0, 1, 2, 3, 4, 5]
Length
arr = [0, 1, 2]
print(len(arr)) # 3
Max, min
arr = [0, 1, 2]
print(max(arr)) # 2
print(min(arr)) # 0
All
arr = [1, 2, 3, 4, 5]
print(all(i > 0 for i in arr)) # True
print(all(i > 3 for i in arr)) # False
Filter
arr = [1, 2, 3, 4, 5]
def filterFunc(x):
return x > 2
print(list(filter(filterFunc, arr))) # [3, 4, 5]
# alternatively
print(list(filter(lambda x: x > 2, arr))) # [3, 4, 5]
# alternatively
print([x for x in arr if x > 2]) # [3, 4, 5]
Next Returns the first element that meets a condition
arr = [1, 2, 3, 4, 5]
print(next(x for x in arr if x > 2)) # 3
# print(next(x for x in arr if x > 5)) # error: StopIteration
print(next((x for x in arr if x > 5), None)) # None
Includes
arr = [1, 2, 3, 4, 5]
print(1 in arr) # True
print(6 in arr) # False
For each
arr = [1, 2, 3, 4, 5]
for x in arr:
print(x, end="") # 12345
Map
arr = [1, 2, 3, 4, 5]
def square(x):
return x * x
print(list(map(square, arr))) # [1, 4, 9, 16, 25]
# alternatively
print(list(map(lambda x: x * x, arr))) # [1, 4, 9, 16, 25]
# alternatively
print([x * x for x in arr]) # [1, 4, 9, 16, 25]
Reduce
import functools
arr = [1, 2, 3, 4, 5]
def square_reducer(x, y):
return x * y
print(functools.reduce(square_reducer, arr)) # 120
# alternatively
print(functools.reduce(lambda x, y: x * y, arr)) # 120
Sort
arr = [2, 1, 5, 3, 4]
print(sorted(arr)) # [1, 2, 3, 4, 5]
print(sorted(arr, reverse=True)) # [5, 4, 3, 2, 1]
students = [
{'name': 'Jimmy', 'age': 15},
{'name': 'Hector', 'age': 18},
{'name': 'Paige', 'age': 16}
]
print(sorted(students, key=lambda x: x['age'])) # [{'name': 'Jimmy', 'age': 15}, {'name': 'Paige', 'age': 16}, {'name': 'Hector', 'age': 18}]
Sort 2
arr = [0, 4, 3, 2, 1]
arr.sort()
print(arr) # [0, 1, 2, 3, 4]
Convert to tuple
arr = [1, 2, 3, 4, 5]
print(arr) # [1, 2, 3, 4, 5]
tuple = tuple(arr)
print(tuple) # (1, 2, 3, 4, 5)
Tuple Same as list, but not changeable Create
tuple = (1, 2, 3, 4, 5)
Convert to a list
tuple = (1, 2, 3, 4, 5)
print(tuple) # (1, 2, 3, 4, 5)
list = list(tuple)
print(list) # [1, 2, 3, 4, 5]
Length
tuple = (1, 2, 3, 4, 5)
print(len(tuple)) # 5
Min, max
tuple = (1, 2, 3, 4, 5)
print(min(tuple)) # 1
print(max(tuple)) # 5
Dictionary Same as maps, objects, key-valued storage Create
dict = {
"name": "John",
"sex": "male",
"age": 35,
}
print(dict) # {'name': 'John', 'sex': 'male', 'age': 35}
Access
dict = {
"name": "John",
"sex": "male",
"age": 35,
}
print(dict['name']) # John
print(dict.get("name")) # John
print(dict.keys()) # dict_keys(['name', 'sex', 'age'])
print(dict.values()) # dict_values(['John', 'male', 35])
Delete
dict = {
"name": "John",
"sex": "male",
"age": 35,
}
del dict['name']
print(dict) # {'sex': 'male', 'age': 35}
Length
dict = {
"name": "John",
"sex": "male",
"age": 35,
}
print(len(dict)) # 3
Update
dict = {
"name": "John",
"sex": "male",
"age": 35,
}
dict['name'] = 'James'
print(dict) # {'name': 'James', 'sex': 'male', 'age': 35}
Conditionals if
age = 21
if age > 16:
print('can drive')
if-else
age = 21
if age > 16:
print('can drive')
else:
print('can not drive')
else-if
age = 21
if age >= 21:
print('can drive a tractor')
elif age >= 16:
print('can drive a car')
else:
print('can not drive')
ternary
a = 1 if (1 == 1) else 0
print(a) # 1
logical operators
num = 2
if (num == 1) and (num != 2) and not(num == 3):
print('num is 1')
else:
print('num is not 1')
# num is not 1
Loops For
for x in range(0, 5):
print(x, ' ', end="") # 0 1 2 3 4
for x in [1, 2, 3, 4, 5]:
print(x, ' ', end="") # 1 2 3 4 5
While
i = 0
while (i <= 10):
if(i % 2 == 0):
print(i, end="")
i += 1
# 0246810
import random
randNum = 0
while(randNum != 5):
print(randNum, end='')
randNum = random.randrange(0, 10)
# 0904994
Break, continue
for i in range(0, 10):
if (i == 5):
continue
if (i == 9):
break
print(i, end="") # 01234678
Functions Variables defined in function are not visible outside. Functions declaration
def sum(num1, num2):
sumNum = num1 + num2
return sumNum
print(sum(1, 2)) # 3
Lambda function (arrow)
sum = lambda num1, num2 : num1 + num2
print(sum(1, 2)) # 3
def func(n):
return lambda a : a * n
tripler = myfunc(3)
print(tripler(11)) # 33
Input
import sys
print('what is your name?')
name = sys.stdin.readline()
print('hello', name)
# what is your name?
# John
# hello John
File Create & write
file = open("text.txt", "wb")
print(file.mode) # wb
print(file.name) # text.txt
file.write(bytes("Line1
", 'UTF-8'))
file.close()
Open & read
file = open("text.txt", "r+")
text = file.read()
print(text) # Line1
file.close()
Delete
import os
os.remove("text.txt")
Class Initialize, setters, getters, methods
class Human:
__name = None # private
__sex = None # like 'null' in JS
__age = None
# constructor to initialize an object
# self = this in JS
def __init__(self, name, sex, age, height):
self.__name = name
self.__sex = sex
self.__age = age
self.height = height
# setters
def set_name(self, name):
self.__name = name
def set_sex(self, sex):
self.__sex = sex
def set_age(self, sex):
self.__sex = sex
# getters
def get_name(self):
return self.__name
def get_sex(self):
return str(self.__sex)
def get_age(self):
return str(self.__age)
def get_type(self):
print("Human")
#method
def toString(self):
return "{} is {}, {} years old, {} cm".format(self.__name, self.__sex, self.__age, self.height)
# create an object
john = Human('John', 'male', 35, 180)
print(john.toString()) # John is male, 35 years old
print(john.height) # 180
print(john.get_age()) # 35
print(john.__age) # !!! object has no attribute '__age' # it is private
Inherit
from classFile import Human
class Superhero(Human):
__superpower = None
def __init__(self, name, sex, age, height, superpower):
self.__superpower = superpower
# call the super class constructor
super(Superhero, self).__init__(name, sex, age, height)
def set_superpower(self, superpower):
self.__superpower = superpower
def get_superpower(self):
return self.__superpower
def get_type(self):
print ("Superhero")
# We can overwrite functions in the super class
def toString(self):
return "{} is a superhero {}, {} years old, {} cm, with {} superpower".format(self.get_name(), self.get_sex(), self.get_age(), self.height, self.__superpower)
immortalJohn = Superhero('John', 'male', 35, 180, "immortality")
print(immortalJohn.toString()) # John is a superhero male, 35 years old, 180 cm, with immortality superpower
print(immortalJohn.get_superpower()) # immortality
immortalJohn.set_superpower("transparency")
print(immortalJohn.get_superpower()) # transparency
Python cheat-sheet on every property and method.