Features of Python
- High-level and easy: English-like, short programs.
- Free and open source: anyone can use and study it.
- Interpreted: runs line by line, so errors are easy to find.
- Portable: the same code runs on Windows, Linux, macOS.
- Case-sensitive:
Ageandageare different. - Rich library, used in web, data science, AI and games.
Your first program: Hello World
print("Hello World")print() is a built-in function that shows its value on the screen. Text is written inside quotes. Python has no semicolons at line ends and uses indentation to group lines.
Interactive mode and script mode
Interactive mode: type one statement after the >>> prompt and press Enter; the result appears immediately. Good for testing small things, but the code is not saved.
Script mode: write many lines in a file with the .py extension, save it, and run it (in IDLE: Run → Run Module or F5). The program can be saved, edited and run again.
Python character set
The characters Python accepts: letters A–Z, a–z; digits 0–9; special symbols like + − * / ( ) [ ] { } = , : ' " # @; whitespace (space, tab, newline); and other Unicode characters.
Tokens
A token is the smallest meaningful unit of a program.
- Keywords: reserved words with a fixed meaning:
if, else, for, while, True, False, None, and, or, not, import, def. Cannot be used as names. - Identifiers: names we give to variables, functions etc. Rules: letters, digits and _ only; cannot start with a digit; no spaces; not a keyword; case-sensitive. Valid:
marks_1, _total. Invalid:1marks, my name, for. - Literals: fixed values:
25, 3.14, 'Hi', True, None. - Operators:
+ - * / // % ** == < andetc. - Punctuators:
( ) [ ] { } , : . ' " #.
Variables
A variable is a name that refers to a value stored in memory. It is created when you first assign to it: age = 15. Python does not need you to declare the type; it takes the type from the value. Giving a new value makes the name refer to the new object: age = 16.
Multiple assignment: a = b = 0 and x, y = 2, 5. Every object has an identity (id()), a type (type()) and a value.
l-value and r-value
In x = y + 5:
- r-value (right side): an expression that gives a value. It is evaluated first.
- l-value (left side): the name that receives the value. It must be a variable, never a literal or expression.
5 = x and a + b = c are errors because the left side is not a name.
Comments
A comment is a note for humans that Python ignores. A single-line comment starts with #: # find the total. For a multi-line note, programmers often use a triple-quoted string '''…''' (a docstring when placed at the start of a function or file). Comments explain why the code does something and make it easier to read and debug.
Key formulas and definitions
- Tokens: keywords, identifiers, literals, operators, punctuators
- Identifier rules: letters/digits/_ ; not starting with a digit; not a keyword
- Assignment: l-value = r-value (right side evaluated first)
- # single-line comment
- Interactive: >>> ; Script: .py file
Worked examples
1. Which of these identifiers are valid? total_marks, 2ndPlace, _count, my-name, while, Name1
Valid: total_marks, _count, Name1. Invalid: 2ndPlace (starts with a digit), my-name (hyphen), while (keyword).
2. Count the tokens in: area = 3.14 * r * r
Tokens: area, =, 3.14, *, r, *, r → 7 tokens (identifiers: area, r, r; operators: =, *, *; literal: 3.14).
3. What is printed? a = 5; b = a; a = 7; print(b)
5. b was tagged to the value 5; moving the tag a to 7 does not move b.
4. Find the error: 10 = marks
The left side must be a variable (l-value). Correct: marks = 10.
5. What does x, y = y, x do if x = 2, y = 9?
The right side (9, 2) is made first, then assigned: x = 9, y = 2. The values are swapped.
6. What will Python show? print('Sum', 4 + 5) # adding
Sum 9. The comment after # is ignored.
Common mistakes
- Starting a variable name with a digit, e.g. 1st = 5.
- Treating Name and name as the same. Python is case-sensitive.
- Writing the value on the left: 5 = x.
- Expecting interactive-mode code to be saved. Use script mode for programs you want to keep.