Week 2: Basic Programming
Concepts (I)
Variables, Data Types & Type Casting
What is a Variable?
• • A variable is a name that stores data.
• • Think of it like a box with a label.
• Example:
• age = 20
• name = 'Ali'
• is_happy = True
Rules for Variables
• • Must start with a letter or _ (underscore)
• • Cannot start with a number
• • No spaces allowed
• • Case-sensitive (Name ≠ name)
Data Types in Python
• • Numbers → int, float
• • Strings → str
• • Boolean → True/False
Numbers
• • int → whole numbers (5, 10, -3)
• • float → decimal numbers (3.14, -2.5)
• Example:
• age = 25
• pi = 3.14
Strings (Text)
• • Strings = text inside quotes
• • Example:
• name = 'Python'
• message = "Hello World"
• • Strings can be joined with +
• 'Hello' + ' Python'
Boolean (True/False)
• • Only 2 values: True or False
• • Used in decisions (yes/no, on/off)
• Example:
• is_sunny = True
• is_raining = False
Type Casting
• • Changing one data type into another
• Functions:
• int('10') → 10
• float('3.5') → 3.5
• str(20) → '20'
• Example:
• x = '100'
Practice Exercises
• 1. Make variables: int, float, str, bool
• 2. Add 2 numbers and print result
• 3. Convert number → string using str()
• 4. Convert string → number using int()
• 5. Print True/False values
Summary
• • Variables = containers for data
• • Data Types = int, float, str, bool
• • Type Casting = change one type into another
• • Practice makes programming easy!