Python data types
- Numbers:
int(whole: 25, −3),float(decimal: 3.14, 2e3),complex(3 + 4j). - bool:
TrueorFalse(a kind of int: True = 1, False = 0). - None: means 'no value'; type NoneType.
- Sequences (ordered, indexed):
str'hello',list[1, 'a', 2.5],tuple(1, 2, 3). - Mapping:
dict{'name': 'Riya', 'age': 16} of key: value pairs.
Use type(x) to see a value's type.
Mutable and immutable types
Mutable: the value can be changed in place without making a new object: list, dict (and set).
Immutable: cannot be changed; 'changing' makes a new object with a new id: int, float, bool, complex, str, tuple.
L = [1, 2, 3]; L[0] = 5 # OK, same id s = 'cat'; s[0] = 'b' # TypeError
Operators in Python
- Arithmetic: + − * / (true division, gives float) // (floor division) % (remainder) ** (power). 7/2 = 3.5, 7//2 = 3, 7%2 = 1, 2**3 = 8, −7//2 = −4.
- Relational: < > <= >= == != give True/False.
- Logical: and, or, not.
5 > 3 and 2 > 4→ False. - Assignment: = and augmented += −= *= /= //= %= **=.
x += 2means x = x + 2. - Identity: is, is not — do two names refer to the same object (same id)?
- Membership: in, not in — is a value inside a sequence?
'a' in 'cat'→ True.
Operator precedence
Highest to lowest:
()brackets**(right to left: 2**3**2 = 2**9 = 512)- unary
+x −x * / // %+ −- relational
< > == != …,is,in notandor
Same level: left to right (except **). Example: 2 + 3 * 4 ** 2 = 2 + 3 * 16 = 2 + 48 = 50.
Type conversion
Implicit (automatic): Python converts to avoid data loss, e.g. 5 + 2.0 = 7.0 (int → float).
Explicit (type casting): you convert with functions: int('25') → 25, float('3.5') → 3.5, str(10) → '10', int(7.9) → 7 (cuts the decimal), bool(0) → False. int('abc') gives a ValueError.
Input and output
input(prompt) shows the prompt and returns what the user types as a string. Convert for numbers: n = int(input('Enter n: ')).
print(values, sep=' ', end='\n') shows values separated by sep and ends with end. print(1, 2, 3, sep='-') → 1-2-3; print('Hi', end='!') → Hi! with no new line.
Errors: syntax, logical and runtime
- Syntax error: the code breaks Python's grammar, so it does not run at all: missing colon, unclosed bracket,
prnt('hi')is a NameError at run time butprint('hi'is a syntax error. - Runtime error (exception): the code is valid but crashes while running: division by zero (ZeroDivisionError),
int('abc')(ValueError), using an undefined name (NameError), wrong index (IndexError). - Logical error: the program runs but gives the wrong answer, e.g. average = a + b / 2 instead of (a + b) / 2. Hardest to find; needs testing.
Key formulas and definitions
- / → float; // → floor; % → remainder; ** → power
- Precedence: () > ** > unary > * / // % > + − > relational > not > and > or
- Mutable: list, dict · Immutable: int, float, bool, str, tuple
- input() returns str → use int()/float()
- Errors: syntax, runtime (exception), logical
Worked examples
1. Evaluate: 17 // 5, 17 % 5, 17 / 5, 2 ** 4.
17 // 5 = 3, 17 % 5 = 2, 17 / 5 = 3.4, 2 ** 4 = 16.
2. Evaluate step by step: 10 - 4 * 2 ** 2 + 6 / 3
2 ** 2 = 4 → 10 - 4 * 4 + 6 / 3 → 4 * 4 = 16, 6 / 3 = 2.0 → 10 - 16 + 2.0 = -4.0.
3. Evaluate: not 5 > 3 or 4 < 6 and 2 == 3
Relational first: not True or True and False. not → False or True and False. and → False or False. or → False.
4. Output? a = '7'; b = 3; print(a * b, int(a) * b)
777 21. A string times an int repeats it; int('7') * 3 = 21.
5. Name the error type: (i) print('Hi' (ii) x = 10 / 0 (iii) area = l + b for a rectangle.
(i) Syntax error (bracket not closed). (ii) Runtime error: ZeroDivisionError. (iii) Logical error: it runs but the formula is wrong.
6. a = [1, 2]; b = a; c = [1, 2]. Find a is b, a is c, a == c.
a is b → True (same object). a is c → False (different objects). a == c → True (same values).
7. Output? print(10, 20, 30, sep='*', end='#')
10*20*30#
Common mistakes
- Expecting 7 / 2 to give 3. / always gives a float (3.5); use // for 3.
- Forgetting to convert input(): input() + 5 gives a TypeError.
- Using is to compare values. Use == for values; is checks the same object.
- Thinking -7 // 2 = -3. Floor division rounds down: -4.