■ Python Basics: Syntax, Variables, Data Types,
and Operators
1. Syntax
Syntax refers to the rules for writing code. In Python:
- Indentation is important (use 4 spaces).
Example:
print("Hello, World!")
2. Variables
Variables are containers for storing data. Python does not require declaring the type explicitly.
Examples:
name = "Yousuf" # string
age = 20 # integer
height = 5.9 # float
3. Data Types
1 int → whole numbers (10, -5, 0)
2 float → decimal numbers (3.14, -2.5)
3 str → text ('Hello')
4 bool → True/False values
5 list → collection of items ([1, 2, 3])
6 tuple → ordered but immutable collection ((1, 2, 3))
7 dict → key-value pairs ({'name': 'Yousuf', 'age': 20})
4. Operators
Operators are symbols that perform operations on variables and values.
Arithmetic Operators
x = 10
y = 3
print(x + y) # 13
print(x - y) # 7
print(x * y) # 30
print(x / y) # 3.333...
print(x % y) # 1 (remainder)
print(x ** y) # 1000 (10^3)
print(x // y) # 3 (floor division)
Comparison Operators
==, !=, >, <, >=, <=
Logical Operators
and, or, not
■ Practice Exercise
1. Create variables a = 15, b = 4.
2. Print the results of:
1 a+b
2 a-b
3 a*b
4 a/b
5 a%b
6 a ** b