Core Python
🔢 Data Types
int
Integer numbers (e.g., 1, 42, -5)
Fast operations
Arithmetic like 1000 * 1000 runs quickly
Limited by memory
Very large ints use more memory: 10**1000
float
Decimal numbers (e.g., 3.14, -0.01)
Precise for many cases
0.1 + 0.2 appears fine in simple math
Rounding/precision issues
0.1 + 0.2 == 0.3 → False due to binary float representation
str
Text data ("hello", 'world')
Rich methods, Unicode support
"abc".upper() → 'ABC'
Immutable
"abc"[0] = 'x' → Error
bool
Logical values (True, False)
Clear for control flow
Used in if, while for readability
Only two values
Can't represent uncertainty beyond True/False
list
Ordered, mutable sequence ([1, 2, 3])
Flexible, powerful methods
append, pop, slicing
Slower lookup than dict/set
5 in large_list is O(n)
tuple
Ordered, immutable sequence ((1, 2, 3))
Safer (can't be modified)
Useful as dict keys: {(1,2): 'a'}
Immutable
Can't append or remove
set
Unordered unique items ({1, 2, 3})
Fast membership testing
x in my_set is O(1)
No indexing/order
my_set[0] → Error
dict
Key-value store ({'a': 1})
Fast key-based lookup
my_dict['name'] is fast
Keys must be unique
Duplicate keys overwrite: {'a': 1, 'a': 2} → {'a': 2}
🔄 Type Casting (Type Conversion)
int()
Converts to integer
Removes decimal points
int(3.9) → 3
Loses precision
int(9.99) → 9
float()
Converts to float
Adds precision
float(5) → 5.0
Can lead to rounding issues
float("3.14") + 1e-16 may not be 3.1400000000000001
str()
Converts to string
Useful for printing/logs
str(123) → '123'
Not usable in math ops
'10' + 5 → TypeError
bool()
Converts to Boolean
Helps in logical filtering
bool([]) → False
Can be confusing
bool("False") → True
list()
Converts to list
Good for iterables
list("abc") → ['a', 'b', 'c']
Can create large lists
list(range(1_000_000)) → High memory usage
set()
Converts to set
Removes duplicates
set([1,2,2,3]) → {1,2,3}
Unordered result
No guarantee of element order
tuple()
Converts to tuple
Immutable, hashable
tuple([1,2,3]) → (1,2,3)
Can't modify
my_tuple[0] = 9 → Error
➕ Operators
🧮 Arithmetic Operators
+
Addition
2 + 3 → 5
Works on numbers and strings ('a' + 'b')
-
Subtraction
5 - 2 → 3
*
Multiplication
3 * 4 → 12
Also works for string repetition: 'a' * 3 → 'aaa'
/
Division
7 / 2 → 3.5
Always returns float
//
Floor Division
7 // 2 → 3
Rounds down
%
Modulus
7 % 2 → 1
Remainder
**
Exponentiation
2 ** 3 → 8
Power of a number
🔍 Comparison
==
Equal
5 == 5 → True
!=
Not Equal
5 != 3 → True
>
Greater than
5 > 3 → True
<
Less than
3 < 5 → True
>=
Greater or equal
5 >= 5 → True
<=
Less or equal
4 <= 5 → True
🔗 Logical
and
True if both are true
True and True → True
or
True if at least one is true
True or False → True
not
Negation
not True → False
📦 Assignment
is
Same object identity
a is b
Compares memory addresses
is not
Not same identity
a is not b
Use with caution for numbers/strings (interning)
🧪 Identity & Membership
in
Checks if element is in sequence
'a' in 'cat' → True
not in
Checks if element is NOT in sequence
5 not in [1,2,3] → True
Last updated