Lesson 2: Python Syntax and Variables

๐ŸŽฏ 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.

TypeSyntaxExample
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

RuleExample
Must start with a letter or underscore_user, name โœ…
Cannot start with a number1name โŒ
Case-sensitiveName โ‰  name
Can contain letters, digits, underscoresuser_name, age2 โœ…
Avoid reserved keywordsif, 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

TypeExampleDescription
intx = 10Integer numbers
floaty = 3.14Decimal numbers
strname = "Alice"String/text
boolis_ready = TrueTrue or False
listfruits = ["apple", "banana"]Ordered, mutable collection
tuplecolors = ("red", "blue")Ordered, immutable collection
dictstudent = {"name": "Ali", "age": 21}Key-value pairs
setnums = {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

ConceptDescription
IndentationDefines code structure
CommentAdds explanation, ignored by interpreter
VariableStores data in memory
Dynamic TypingType changes automatically
Multiple AssignmentAssign 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

more examples


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *