๐ฏ Lesson Objective
By the end of this lesson, youโll understand:
- Pythonโs basic syntax and indentation rules
- Naming conventions for variables
- How to declare, assign, and use variables
- Data types and dynamic typing in Python
๐ 1. What is Python Syntax?
Syntax is the set of rules that defines how Python code must be written and formatted.
Python emphasizes readability and simplicity โ indentation is used instead of braces {}.
โ๏ธ 2. Indentation and Code Blocks
In Python, indentation (spaces at the start of a line) defines code blocks.
You must use consistent spaces โ typically 4 spaces per block.
โ Example:
if True:
print("This is inside the block.")
print("Indentation defines scope.")
print("This is outside the block.")
Output:
This is inside the block.
Indentation defines scope.
This is outside the block.
โ Incorrect Example (will cause error):
if True:
print("Error due to missing indentation")
Error:
IndentationError: expected an indented block
๐งฉ 3. Comments in Python
Comments are ignored by the interpreter and used to explain code.
| Type | Syntax | Example |
|---|---|---|
| Single-line | # | # This is a comment |
| Multi-line | '''...''' or """...""" | ''' This is a multi-line comment ''' |
โ Example:
# This is a single-line comment
'''
This is a
multi-line comment
'''
print("Comments make code easier to read.")
๐ก 4. Variables in Python
A variable is a name that stores a value in memory.
Syntax:
variable_name = value
โ Example:
name = "Sameer"
age = 25
is_student = True
print(name)
print(age)
print(is_student)
Output:
Sameer
25
True
๐ง 5. Variable Naming Rules
| Rule | Example |
|---|---|
| Must start with a letter or underscore | _user, name โ
|
| Cannot start with a number | 1name โ |
| Case-sensitive | Name โ name |
| Can contain letters, digits, underscores | user_name, age2 โ
|
| Avoid reserved keywords | if, class, True, etc. โ |
๐งฎ 6. Multiple Assignments
Python allows multiple assignments in one line.
โ Examples:
# Assign same value
x = y = z = 10
print(x, y, z)
# Assign different values
a, b, c = 1, 2, 3
print(a, b, c)
Output:
10 10 10
1 2 3
๐ข 7. Dynamic Typing
You donโt need to declare variable types.
Python automatically assigns types based on values.
โ Example:
x = 10 # int
x = "Python" # now string
x = 3.14 # now float
print(x)
Output:
3.14
๐ 8. Basic Data Types
| Type | Example | Description |
|---|---|---|
int | x = 10 | Integer numbers |
float | y = 3.14 | Decimal numbers |
str | name = "Alice" | String/text |
bool | is_ready = True | True or False |
list | fruits = ["apple", "banana"] | Ordered, mutable collection |
tuple | colors = ("red", "blue") | Ordered, immutable collection |
dict | student = {"name": "Ali", "age": 21} | Key-value pairs |
set | nums = {1,2,3} | Unordered unique items |
โ Example:
a = 10
b = 3.5
c = "Python"
d = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
๐งช 9. Practice Tasks
โ
Task 1: Create variables for your name, age, and city. Print them.
โ
Task 2: Swap two variable values (e.g., x=5, y=10 โ x=10, y=5).
โ
Task 3: Assign multiple values to variables in one line.
โ
Task 4: Create variables of all 4 basic types and print their data types.
โ
Task 5: Write a Python script that prints your name and year of birth using variables.
๐งฑ 10. Summary
| Concept | Description |
|---|---|
| Indentation | Defines code structure |
| Comment | Adds explanation, ignored by interpreter |
| Variable | Stores data in memory |
| Dynamic Typing | Type changes automatically |
| Multiple Assignment | Assign values in one line |
๐งฎ 11. Mini Project: Variable Display App
Goal:
Create a simple program that displays user info dynamically.
โ Example:
# Personal Info Display
name = "Sameer Pasha"
profession = "Python Developer"
experience = 2
print("Name:", name)
print("Profession:", profession)
print("Experience:", experience, "years")
Output:
Name: Sameer Pasha
Profession: Python Developer
Experience: 2 years

Leave a Reply