📘 CodingMarble Learn

Data Types, Operators and Errors in Python

Every value in Python has a data type: numbers (int, float, complex), bool (True/False), None, sequences (str, list, tuple) and mapping (dict). Mutable types (list, dict) can be changed in place; immutable types (int, float, bool, str, tuple) cannot. Operators are arithmetic (+ − * / // % **), relational (< > <= >= == !=), logical (and, or, not), assignment (= += −= …), identity (is, is not) and membership (in, not in). Precedence decides the order of evaluation, with ** highest and or lowest. Type conversion can be implicit (automatic, int → float) or explicit (int(), float(), str()). input() reads text as a string; print() shows output with sep and end. Errors are syntax errors (grammar), runtime errors or exceptions (crash while running) and logical errors (wrong answer).

🎬 Step-by-step story

  1. Every value has a type. Numbers, True/False, None, text, lists, tuples and dictionaries.
  2. A list can be changed in place and keeps its id. A string cannot be changed; Python makes a new one.
  3. Operators do the work: maths, comparing, and/or/not, assigning, 'is' and 'in'.
  4. Python follows an order. Power first, then multiply and divide, then add and subtract. 2 + 3 * 4 ** 2 becomes 50.
  5. input() always gives text. "5" + "5" is "55". Change it with int() first to get 10.
  6. Your turn: choose two numbers and an operator and see the answer and its type.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why is bool a data type of its own if True is 1?

bool is a special kind of int with only two values, used for conditions. True + True is 2, but we use bools for yes/no decisions.

If strings are immutable, how does s = s + '!' work?

Python builds a new string and moves the name s to it. The old string is not changed. Watch the new block with a new id appear.

What is the difference between == and is?

== checks if values are equal; is checks if both names point to the very same object in memory.

Why is 2 + 3 * 4 ** 2 not 400?

Python does ** before * and * before +. Doing left to right would be wrong. Follow the three lines in the 3D.

Why does input() not give a number?

The keyboard sends characters, so input() always returns text. You decide how to convert it with int() or float().

Python data types

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

Operator precedence

Highest to lowest:

  1. () brackets
  2. ** (right to left: 2**3**2 = 2**9 = 512)
  3. unary +x −x
  4. * / // %
  5. + −
  6. relational < > == != …, is, in
  7. not
  8. and
  9. or

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

Key formulas and definitions

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

Practice quiz

1. Which type is immutable?
2. Value of 2 + 3 * 2 ** 2:
3. input() returns a:
4. Dividing by zero causes a:
5. 'in' is a ___ operator.

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are the data types in Python class 11?

Numbers (int, float, complex), bool, None, sequences (str, list, tuple) and mapping (dict).

What is the difference between mutable and immutable?

Mutable objects (list, dict) can be changed in place; immutable ones (int, float, str, tuple) cannot, so a change creates a new object.

What are the three types of errors in Python?

Syntax errors (grammar mistakes), runtime errors or exceptions (crash while running) and logical errors (wrong result).

Where this is taught

Canada (Ontario)Grade 12A. Programming Concepts and Skills
RomaniaClasa a XI-aModern user skills
CBSE (India)Class 11Computational Thinking and Programming - 1

Learn first

Learn next

Related lessons

All Computer Science lessons