|
Integer
|
-100, 0, -100
|
|
Float
|
-100.98, 0.0001, 90.00
|
|
String
|
'Python', '400', '100+200', 'True'
|
|
Boolean
|
True, False
|
|
Numeric
|
Comparison
|
||
|
+
|
Addition
|
==
|
Equal
|
|
-
|
Subtraction
|
!=
|
Different
|
|
*
|
Multplication
|
>
|
Higher
|
|
/
|
Division
|
<
|
Lower
|
|
**
|
Exponent
|
>=
|
Higher or Equal
|
|
%
|
Modulus
|
<=
|
Lower or Equal
|
|
//
|
Floor Division
|
||
|
Boolean
|
String
|
||
|
&
|
Logical AND
|
+
|
Concatenation
|
|
|
|
Logical OR
|
||
|
not
|
Logical NOT
|
||
|
Expression is a combination of values, variables, and operators
|
|
|
222
|
'text'
|
|
25+36
|
'App' + 'le'
|
|
Assignment Statement links a variable name on the left hand side of the operator, with an expression on the right hand side.
|
|
|
a = 222
|
b = 'text'
|
|
c = 25 +36
|
d = 'App' + 'le'
|
|
a = a + 1
|
|
|
Create a List
|
L1 = [1, 2, 3, 4, 5, 6]
|
|
Get the first element
|
L1[0]
|
|
Get the last element
|
L1[-1]
|
|
Count List elements
|
len()
|
|
Insert an element
|
insert()
|
|
Insert an element to the end
|
append()
|
|
Sort all elements
|
sort()
|
|
Remove an element
|
pop()
|
|
Convert object to List
|
list()
|
|
Slicing Expression
|
|
|
List Name[start index : stop index : step size]
|
|
|
(Step size is optional)
|
|
|
Examples
|
|
|
2nd - 5th elements
|
L1[1:5]
|
|
2nd - Last elements
|
L1[1:]
|
|
1st - 3rd elements
|
L1[0:3]
|
|
All alternate elements
|
L1[::2]
|
|
Create a Dict.
|
D1 = {"Andrew":18, "Johnson":23, "Olivia":22}
|
|
Create a Dict. from two Lists
|
dict(zip())
|
|
Access Dict. value
|
D1["Andrew"]
|
|
Update Dict. value
|
D1["Andrew"] = 20
|
|
Add an element
|
D1["Sue"] = 25
|
|
Drop an element
|
del D1["Johnson"]
|
|
Count Dict. elements
|
len()
|
|
Return all keys
|
keys()
|
|
Return all values
|
values()
|
|
Create a Tuple
|
T1 = (1, 2, 3, 4, 5, 6)
|
|
Convert List to Tuple
|
tuple()
|
|
Note: Tuple elements are immutable and cannot be changed via operations.
|
|
|
Create a Set
|
S1 = {1, 2, 3, 4, 5}
|
|
Insert an element
|
add()
|
|
Find unique elements
|
set()
|
|
Create a Set with all elements from 2 Sets
|
union()
|
|
Create a Set with common elements from 2 Sets
|
intersection()
|
if Condition 1:
Code Block 1
elif Condition 2:
Code Block 2
elif Condition 3:
Code Block 3
else:
Code Block 4
for <variable> in <sequence>:
Code Block
Example:
for x in range(0,5):
print(x)
while <expression>:
Code Block
Example:
i = 2
while i <= 10:
print(i)
i = i + 3
for iterating_var in sequence:
for iterating_var in sequence:
Code Block1
Code Block2
while <expression>:
while <expression>:
Code Block1
Code Block2
|
break
|
Terminate the whole loop
|
|
continue
|
Stop the current iteration of the loop, and continue with the next. Loop does not terminate.
|
|
pass
|
Do nothing and continue the rest of the code inside a loop for the current iteration
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.