Variables / Data types
Numeric types
N = int(24) or Integer = 24
N = float(1.5) or float = 1.5
Complex = complex('A1') or a = 1j
Text type
String = "A" or N =str('Majid')
Sequence Types
List
L1 = ['A','B','C']
L1 = list(('A','B','C'))
Tuple
T1 = ("A", "B", "C")
T1 = tuple(("A", "B", "C"))
Range
R1 = range(6)
Mapping Type
Dictionary
D1 = {"name" : "Majid", "O" : 36}
D2 = dict(name="Majid", O=36)
Set Types
set
S1 = {"A", "B", "C"}
S2 = set(("A", "B", "C"))
frozenset
FS1 = {"A", "B", "C"}
FS2 = frozenset(("A", "B", "C"))
Boolean Type
B1 = True
B2 = False
Python Operator
Python Casting
x = int(x)
x = str(x)
x = float(x)
Python Booleans
Python Booleans
if b > a
if myFunction()
Python Tuple
Tuple
T1 = ("a","b","c")
T2 = tuple(("a","b","c"))
Access Tuple Items
Update and change
T1 = list(T1)
T1.append("d")
print(tuple(T1))
add to tuple
T1 += "E"
Loop Through a Tuple
for x in thistuple:
print(x)
for i in range(len(thistuple)):
print(thistuple[i])
Join:
TF = T1 + T2
Python Sets
S1 = {"A","B","C"}
S2 = set(("A","B","C"))
Add/Remove Set Items
S1.add("E")
S1.update(S2)
S1.remove("E")
S2.discard("E")
S1.pop("E")
S2.clear()
Join Two Sets
S1.union(S2)
Duplicates
S1.intersection(S2)
Python Loops
Python while Loop:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Python for loop:
L = ["A", "B", "C"]
for x in L:
print(x)
for x in range(6):
Python lambda
function lambda:
num = lambda a,b : a + b
print(num(4,3))
Python Dictionary
#dictionary:
D1 = {'name':'Majid',"Age":26}
print(type(D1))
#Get Access:
x = D1.values()
y = D1.keys()
#Check:
if "name" in D1:
#Update Dictionary
D1.update({"name": 'Reza'})
#Removing Items
D1.pop("name")
del D1["name"]
D1.clear()
#Loop
for x in D1:
print(D1[x])
for x, y in D1.items():
print(x, y)
#Copy
D2 = dict(D1)
print(D2)
#Nested
D1 = {
"S1":{
"name":"Majid",
"Age" : 25
},
"S2":{
"name":"Reza",
"Age" : 30
}
}
print(D1)
for x in range(len(D1)):
n = 'S'+str(x+1)
print(D1[n]['name'])
Python Programming (Cheat Sheet)
Python Install
Python Syntax
Best Python IDE : PHP Strorm
Python Syntax / Standard
Python Comments #
Print : print("M")
Python Syntax / Standard
Python Comments #
Print : print("M")
Python String
Python Casting
Strings are Arrays
M = "Majid"
print(M[1])
Looping Through a String
for x in "Majid":
print(x)
String Length
print(len(x))
Check String
print("free" in txt)
Check if NOT
print("expensive" not in txt)
Other
print(b[2:5])
print(b[-5:-2])
print(a.upper())
print(a.lower())
print(a.strip())
print(a.replace("H", "J"))
print(a.split(","))
capitalize()
count("apple", 10, 24)
find("e", 5, 10)
String Concatenation
print(a+b)
String Format
A = 10
F = " Num is {}"
print(F.format(A))
A = 20
B = 30
C = 40
F = "The mess is {1} and {2} and {0}"
print(F.format(A,B,C))
Escape Characters
\\ Backslash
\n New Line
\t Tab
Python List
L1 = ["A", "B", "C"]
print(L1[1])
print(L1[-1])
print(L1[:1])
print(L1[1:])
print(L1[-4:-1])
Check if Item Exists
if "A" in L1
Change List Items
L1[1] = "D"
Change a Range of Item Values
L1[1:3] = ["E", "F"]
Insert Items
L1.insert(2, "G")
Append Items
L1.append("H")
Extend List
L4 = ["1", "2", "3"]
L5 = ["4", "5", "6"]
L4.extend(L5)
Remove Specified Item
L1.remove("A")
Remove Specified Index
L1.pop(1)
del L1[0]
Loop Through a List
for x in L1:
print(x)
for i in range(len(L1)):
print(L1[i])
while i < len(L1):
print(L1[i])
i = i + 1
List Comprehension
L2 = ["Asd", "Bsd", "Csd", "Dsd", "er"]
LF = []
for x in L2:
if "a" in x:
LF.append(x)
print(LF)
Sort Lists
L1.sort()
L1.sort(reverse = True)
Join Lists
L3 = L1 + L2
L1.extend(L2)
Python Condition
if(2 > 1):
print('OK')
Python Functions
Python Functions
def A():
print("OK")
def B(*H):
print("Nums is " + H[2])
B("1", "2", "3")
def C(country = "Norway"):
print("I am from " + country)
C("Sweden")
C("India")
C()
def D(x):
return 5 * x
Python Arrays
Arrays
A = ["1", "2", "3"]
Modify:
A[0] = "0"
Removing Array Elements
A.pop(1)
A.remove("Volvo")
Add Array Elements
A.append("4")
A.insert(1, "5")