1. Tokens - Smallest Units
Tokens are the smallest individual units in a Python program. The Python interpreter breaks the program into tokens. The five types of tokens are:
| Token Type | Description | Example |
|---|---|---|
| Keywords | Reserved words with special meaning | if, else, for, while, def |
| Identifiers | Names given to variables, functions, etc. | myVar, student_name, calculate |
| Literals | Constant values | 10, 3.14, "Hello", True |
| Operators | Symbols performing operations | +, -, *, /, =, == |
| Punctuators | Symbols used for grouping and separation | (), {}, [], , ; : |
2. Keywords
Python has 35 reserved keywords (in Python 3.12) that cannot be used as identifiers.
False, None, True, and, as, assert, async, await, break, class,
continue, def, del, elif, else, except, finally, for, from, global,
if, import, in, is, lambda, nonlocal, not, or, pass, raise,
return, try, while, with, yield3. Identifiers
Identifiers are names given to program elements like variables, functions, classes, and modules.
Rules for naming identifiers:
- Must start with a letter (a-z, A-Z) or underscore (_).
- Can contain letters, digits, and underscores.
- Cannot start with a digit.
- Cannot be a keyword.
- Case-sensitive (Age, age, and AGE are different).
- No spaces or special symbols except underscore.
Valid: name, _count, student1, total_marks
Invalid: 1st, my-name, class, first name
4. Literals
Literals are constant values that appear directly in a program.
| Type | Example | Description |
|---|---|---|
| Integer | 10, -5, 0b1010, 0o12, 0xA | Whole numbers (decimal, binary, octal, hex) |
| Float | 3.14, -2.5, 1.5e3 | Decimal numbers with fractional part |
| String | "Hello", 'Python', """Multi-line""" | Sequence of characters enclosed in quotes |
| Boolean | True, False | Logical values |
| None | None | Represents absence of a value |
5. Operators
Operators are symbols that perform operations on operands. Python supports: Arithmetic (+, -, *, /, //, %, **), Relational (==, !=, >, <, >=, <=), Logical (and, or, not), Assignment (=, +=, -=, etc.), Bitwise (&, |, ^, ~, <<, >>), Identity (is, is not), and Membership (in, not in) operators.
6. Punctuators
Punctuators are symbols used for grouping and separating code:
() - Parentheses for function calls, tuples, grouping
[] - Square brackets for lists, indexing
{} - Curly braces for dictionaries, sets
, - Comma for separating items
: - Colon for defining blocks, slicing
; - Semicolon for multiple statements on one line7. Variables in Python
A variable is a named memory location that stores a value. Unlike other languages, Python variables do not need explicit type declaration.
# Variable assignment
name = "Riya"
age = 17
marks = 95.5
is_student = True
# Multiple assignment
x = y = z = 10
a, b, c = 1, 2, 38. Data Types
| Data Type | Class | Example | Description |
|---|---|---|---|
| int | int | 10, -5, 0xFF | Integer (whole numbers) |
| float | float | 3.14, -2.5, 1e6 | Floating-point number |
| bool | bool | True, False | Boolean (logical) value |
| str | str | "Hello", 'Python' | String (text) |
| NoneType | NoneType | None | Absence of value |
| list | list | [1, 2, 3] | Mutable sequence |
| tuple | tuple | (1, 2, 3) | Immutable sequence |
| dict | dict | {"key": "value"} | Key-value pairs |
Use type() function to check the data type of a variable: print(type(x)).
9. Mutable and Immutable Types
| Category | Types | Description |
|---|---|---|
| Immutable | int, float, bool, str, tuple, NoneType | Cannot be changed after creation |
| Mutable | list, dict, set | Can be modified after creation |
10. Type Conversion
Implicit conversion: Python automatically converts one data type to another when needed.
x = 10 # int
y = 3.5 # float
z = x + y # z will be float (13.5) - implicit conversionExplicit conversion: Programmer manually converts using functions.
a = "10"
b = int(a) # Explicit conversion: string to int
c = float(b) # int to float
d = str(c) # float to string11. Input and Output
Output: The print() function displays output.
print("Hello, World!")
print("Value:", 10)
print("Sum of", 5, "and", 3, "is", 8)Input: The input() function reads input as a string.
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert to int
height = float(input("Enter height: ")) # Convert to float12. Output Formatting
# Using f-strings (Python 3.6+)
name = "Riya"
marks = 95.5
print(f"Student: {name}, Marks: {marks}")
# Using format() method
print("Student: {}, Marks: {}".format(name, marks))
# Using % formatting
print("Student: %s, Marks: %.1f" % (name, marks))13. Dynamic Typing
Python is dynamically typed, meaning a variable can change its type during program execution.
x = 10 # x is int
print(type(x)) #
x = "Hello" # x is now str
print(type(x)) #
x = 3.14 # x is now float
print(type(x)) # 14. Revision Questions and Answers
Very Short Answer Questions
1. What are tokens in Python?
Tokens are the smallest individual units in a Python program.
2. Name the five types of tokens.
Keywords, Identifiers, Literals, Operators, Punctuators.
3. Which function is used to take input from the user?
input().
4. What is the return type of input() function?
String (str).
5. What does the type() function return?
The data type of the given value or variable.
Short Answer Questions
1. Differentiate between mutable and immutable data types with examples.
Mutable types can be modified after creation (list, dict, set). Immutable types cannot be changed after creation (int, float, bool, str, tuple). For example, you can modify a list by appending elements, but you cannot modify a string once created.
2. Explain explicit and implicit type conversion with examples.
Implicit conversion is automatic (int + float = float). Explicit conversion is done by the programmer using functions like int(), float(), str(). Example: int("10") converts string "10" to integer 10.
Long Answer Questions
1. Write a Python program that accepts a student's name, age, and marks from the user and displays them formatted.
name = input("Enter name: "); age = int(input("Enter age: ")); marks = float(input("Enter marks: ")); print(f"Name: {name}, Age: {age}, Marks: {marks}")
2. Explain all rules for naming identifiers in Python with valid and invalid examples.
Rules: (1) Start with letter or underscore. (2) Followed by letters, digits, underscores. (3) Case-sensitive. (4) Cannot use keywords. (5) No spaces or special symbols. Valid: myVar, _count, total1. Invalid: 1st (starts with digit), my-var (hyphen not allowed), class (keyword).