Core Python

🔢 Data Types

Data Type
Description
Pros
Example (Pro)
Cons
Example (Con)

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.3False 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)

Type Cast
Description
Pros
Example (Pro)
Cons
Example (Con)

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

Operator
Description
Example
Notes

+

Addition

2 + 35

Works on numbers and strings ('a' + 'b')

-

Subtraction

5 - 23

*

Multiplication

3 * 412

Also works for string repetition: 'a' * 3'aaa'

/

Division

7 / 23.5

Always returns float

//

Floor Division

7 // 23

Rounds down

%

Modulus

7 % 21

Remainder

**

Exponentiation

2 ** 38

Power of a number


🔍 Comparison

Operator
Description
Example

==

Equal

5 == 5True

!=

Not Equal

5 != 3True

>

Greater than

5 > 3True

<

Less than

3 < 5True

>=

Greater or equal

5 >= 5True

<=

Less or equal

4 <= 5True


🔗 Logical

Operator
Description
Example

and

True if both are true

True and TrueTrue

or

True if at least one is true

True or FalseTrue

not

Negation

not TrueFalse


📦 Assignment

Operator
Description
Example
Notes

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

Operator
Description
Example

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