📘 CodingMarble Learn

Python Basics: Modes, Variables, Data Types and Operators

Python runs in interactive mode (one line at a time) or script mode (a saved .py file). Blocks are shown by indentation. Variables are names tied to values; every value has a data type, and some types are mutable. Operators build expressions that Python works out by precedence. input() reads text, int()/float()/str() convert types, and debugging fixes syntax, runtime and logical errors.

🎬 Step-by-step story

  1. Python has two modes: interactive for one line, script for a saved program.
  2. A program runs top to bottom. Lines after a colon are indented to form a block.
  3. A variable is a name tag tied to a value. Assigning again moves the tag.
  4. Every value has a type. Lists and dictionaries can change; numbers, strings and tuples cannot.
  5. Python works out an expression by precedence: power, then multiply, then add.
  6. Your turn: pick an expression, predict, then watch Python solve it.

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

🤔 Common doubts, cleared

If interactive mode is faster, why use script mode?

Interactive mode forgets everything when closed. Script mode saves the program so you can run, share and fix it later.

Why does Python care about spaces at the start of a line?

Python uses indentation to know which lines belong to a block (other languages use { }). Same block = same indentation.

Does x = x + 1 mean x equals x + 1?

No. = means assign: work out the right side, then tie the name on the left to the result. The tag moves to the new value.

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

It builds a new string and moves the name s to it. The old string is not changed.

Why is 10 / 5 equal to 2.0 and not 2?

In Python 3, / always gives a float. Use // if you want a whole-number result.

Why does "5" + 2 give an error but "5" * 2 gives "55"?

You cannot add a string and an int (TypeError). But a string times an int repeats the string.

Two ways to run Python: interactive and script mode

Interactive mode: you type one statement after the >>> prompt and Python answers at once. Good for testing small things. Nothing is saved.

Script mode: you write many lines in a file ending in .py, save it and run it (F5 in IDLE). Good for real programs; you can run it again and again.

Structure of a program and indentation

A Python program is a list of statements, run from top to bottom. A comment starts with #; Python ignores it, but it helps humans read the code.

A line that ends with a colon : (like if, for, while) starts a block. Every line of the block must be pushed right by the same amount, usually 4 spaces. This is indentation. Python uses it instead of brackets. Wrong indentation gives an IndentationError.

if marks >= 33:
    print("Pass")
else:
    print("Try again")

Identifiers, keywords, constants and variables

Keywords are reserved words with a fixed meaning: if, else, elif, for, while, True, False, None, and, or, not, in, is, def, import, etc. You cannot use them as names.

An identifier is a name you give to a variable or function. Rules: letters, digits and _ only; cannot start with a digit; no spaces or symbols; not a keyword; case-sensitive (Marks and marks are different). Valid: total_marks, _x, mark2. Invalid: 2mark, total marks, if.

A variable is a name that refers to a value in memory: age = 16. Python works out its type from the value; no declaration is needed. = means "tie this name to this value", not "equals".

A constant is a value that should not change. Python has no true constants; by habit we write the name in capitals: PI = 3.14. Fixed values written directly (10, "hi", 3.5) are called literals.

Data types

Use type(x) to see a type.

Mutable and immutable types

Mutable: can be changed in place, same object. list, dict (and set).

Immutable: cannot be changed in place. int, float, bool, str, tuple. When you "change" them, Python makes a new value and moves the name tag. Example: s = "cat"; s[0] = "b" gives a TypeError, but L = [1,2]; L[0] = 9 works.

Operators and precedence

An expression is a mix of values, variables and operators that gives one value, like 2 + 3 * 4.

Precedence (high to low)

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

Same level: left to right (except **).

Input and output

input("prompt") shows a message, waits for the user, and always returns a string.

print(a, b, sep=" ", end="\n") shows values. sep is put between values, end at the end.

name = input("Name: ")
print("Hello", name, sep=", ", end="!\n")

Type conversion

Explicit (you ask for it): int("25") → 25, float("2.5") → 2.5, str(10) → "10", int(7.9) → 7 (cuts the decimal). int("abc") gives a ValueError.

Implicit (Python does it): 3 + 2.0 → 5.0 (int becomes float so no data is lost).

Since input() gives a string, write age = int(input("Age: ")) to do maths with it.

Debugging: three kinds of errors

Debugging means finding and fixing errors: read the error message and line number, print values in between, and test with small known inputs.

Try it: predict before you run

Open Python (IDLE or any online Python). Before pressing Enter, write down your guess for each: 7 // 2, 7 % 2, 2 ** 3 ** 2, "5" + "2", int("5") + 2, 10 / 5. Then run each. Every wrong guess: open step 6 in the 3D and watch that expression reduce.

Key formulas and definitions

Worked examples

1. Evaluate 2 + 3 * 4 ** 2.

** first: 4 ** 2 = 16. Then 3 * 16 = 48. Then 2 + 48 = 50.

2. Evaluate 17 // 5 and 17 % 5.

17 ÷ 5 = 3 remainder 2. So 17 // 5 = 3 and 17 % 5 = 2. Check: 3 × 5 + 2 = 17.

3. Evaluate 2 ** 3 ** 2.

** goes right to left: 3 ** 2 = 9, then 2 ** 9 = 512 (not 64).

4. What is printed? x = 5; x += 3; x *= 2; print(x)

x = 5 → x = 8 → x = 16. Prints 16.

5. Evaluate not 5 > 3 and 2 == 2.

Relational first: 5 > 3 is True, 2 == 2 is True. Then not True = False. Then False and True = False.

6. a = input("Enter: ") and the user types 4. What is a * 3?

a is the string "4", so a * 3 = "444" (string repeated). To get 12, use int(a) * 3.

7. Find the error type: avg = m1 + m2 / 2 for m1 = 80, m2 = 60.

It runs and gives 80 + 30 = 110, which is wrong. This is a logical error. Fix: avg = (m1 + m2) / 2 = 70.0.

Common mistakes

Practice quiz

1. What is 7 // 2?
2. Which is a valid identifier?
3. Which type is mutable?
4. What does input() return?
5. Dividing by zero while running gives a:

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 modes of Python?

Interactive mode (>>> prompt, one line at a time) and script mode (a saved .py file run as a whole).

What is the difference between mutable and immutable?

Mutable objects (list, dict) can be changed in place; immutable ones (int, float, str, tuple, bool) cannot.

What are the types of errors in Python?

Syntax errors (grammar), runtime errors (fail while running, like ZeroDivisionError) and logical errors (wrong answer, no message).

Where this is taught

Canada (Ontario)Grade 11A. Programming Concepts and Skills
Canada (Ontario)Grade 11A. Programming Concepts and Skills
Ukraine10 класProgramming language and data structures
CBSE (India)Class 11Python Programming
CBSE (India)Class 11Computational Thinking and Programming - 1
CBSE (India)Class 11Introduction to Python
England (GCSE, A level)Year 124.1 Fundamentals of programming
USA (Common Core, NGSS, AP)Grade 10Big Idea 3: Algorithms and Programming
USA (Common Core, NGSS, AP)Grade 11Using Objects and Methods
South Korea고등학교 2학년Algorithms and programming
South Korea고등학교 3학년Programming
FranceSecondeAlgorithms and logic
FranceSecondeProgramming basics
FrancePremièreAlgorithms and logic
FrancePremièreLanguages and programming
FrancePremièreMathematics (2026 programme)
FranceTerminaleContent
FranceTerminaleMathematics (2019 programme)
Russia10 классAlgorithms and programming
China高一Comp.1 Ch.2 Algorithms and programs

Learn first

Learn next

Related lessons

All Informatics Practices lessons