🎯 Learning Objectives
By the end of this module, students will be able to:
Understand what variables are and how to use them.
Identify and apply different data types.
Perform type conversions (casting).
Avoid common variable naming mistakes.
🧠 1. What is a Variable?
A variable is a name that refers to a value stored in memory.
Think of it as a label on a box that stores a piece of data.
name
holds "Alice"
, age
holds 20
.
🗂️ 2. Python Data Types
Data Type Example Description int
25
Whole numbers float
3.14
Decimal numbers str
"Hi"
Text values bool
True
Logical values (True/False)
Example:
x = 10 # int
y = 3.5 # float
name = "Sam" # str
flag = True # bool
📝 3. Variable Naming Rules
✅ Valid Names:
Must begin with a letter or _
Can contain letters, digits, and underscores
Case-sensitive (name
≠ Name
)
Example :
# 2name = "Alex" ❌ starts with number
# user-name = "Bob" ❌ contains dash
# my name = "Joe" ❌ contains space
🔄 4. Type Conversion (Casting)
age = "20" # str
nextyearage=age+1
#TypeError: can only concatenate str (not "int") to str
nextyearage=int(age)+1
print (nextyearage) # 21
Use int()
, float()
, str()
, bool()
for conversions.
🧪 5. Checking Data Types
use type() function
print(type(10)) # <class 'int'>
print(type("hello")) # <class 'str'>
⚠️ 6. Common Mistakes
Mistake Why it’s wrong Fix "age" + 5
Can’t add string and integer Convert first: int("age") + 5
user name = "Tom"
Variable names can’t have spaces Use underscore: user_name
🧩 7. Practice Time (Live Demo)
Create a variable city
and assign your city name.
Create variables for your birth year and calculate your age.
Convert a float to a string and print the result and type.
Check if bool(0)
and bool("Hi")
return True or False.
📚 Quick Recap
Variables hold data values.
Data types define the kind of value.
Use type()
to inspect types.
Convert types using casting functions like int()
, str()
.
Follow naming rules for valid variables.
Example 1:
name = "John"
age = 28
height = 5.9
print(name)
print(age)
print(height)
Output
Example 2: Check Data Type
print(type(name)) # str
print(type(age)) # int
print(type(height)) # float
Output
<class 'str'>
<class 'int'>
<class 'float'>
Example 3: Type Casting
age = 28
height = 5.9
age = float(age)
height = str(height)
print(age)
print(type(age))
print(height)
print(type(height))
Output
28.0
<class 'float'>
5.9
<class 'str'>
Example 4: Assign multiple values in one line.
a, b, c = 1, 2, 3
print(a, b, c)
Output
Example 5: Assign Same Value to Multiple Variables
x = y = z = "Python"
print(x, y, z)
Output
Example 6: String Concatenation using Variables
first = "Hello"
last = "World"
message = first + " " + last
print(message)
Output
Example 7: Numeric Calculation Using Variables
price = 50
quantity = 3
total = price * quantity
print("Total:", total)
Output
Example 8: Boolean Example
is_logged_in = True
print("Login status:", is_logged_in)
Output
Example 9: Dynamic Typing in Python
var = 10
print(type(var)) # int
var = "Ten"
print(type(var)) # str
Output
<class 'int'>
<class 'str'>
Example 10: Input and Type Conversion
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}, you are {age} years old.")
Output
Enter your name: Alex
Enter your age: 21
Hello Alex, you are 21 years old.
Example 11: Using type()
in Debugging
value = "123"
print(value + " is a string") # works
# print(value + 5) # ❌ TypeError
value = int(value)
print(value + 5) # ✅ Now works
Output
Example 12: Calculate the Area of a Rectangle
length = 10
width = 5
area = length * width
print("Area of rectangle:", area)
Output
Example 13: Store and Display Product Info
product_name = "Laptop"
price = 799.99
in_stock = True
print("Product:", product_name)
print("Price: $", price)
print("Available:", in_stock)
Output
Product: Laptop
Price: $ 799.99
Available: True
Example 14: Temperature Storage in Celsius and Fahrenheit
celsius = 25
fahrenheit = (celsius * 9/5) + 32
print("Celsius:", celsius)
print("Fahrenheit:", fahrenheit)
Output
Celsius: 25
Fahrenheit: 77.0
Example 15: Using Variables to Format Strings
name = "Sara"
score = 95
print(f"{name} scored {score} points.")
Output
Example 16: Variable Reassignment
x = 10
print("Before:", x)
x = 20
print("After:", x)
Output
Example 17: Using bool()
Function
print(bool(0)) # False
print(bool(123)) # True
print(bool("")) # False
print(bool("Hi")) # True
Output
Example 18: Type Conversion with Errors (Demonstration)
x = "ten"
# y = int(x) # This will cause an error
print("Cannot convert non-numeric string to integer")
Output
Cannot convert non-numeric string to integer
Example 19: Combining Strings and Numbers with Casting
name = "Liam"
score = 88
print(name + " scored " + str(score) + " marks.")
Output
Example 20: Constants (Using UPPERCASE by Convention)
PI = 3.14159
radius = 7
area = PI * radius * radius
print("Circle Area:", round(area, 2))
Output
Welcome to the Python Programming Quiz!
Test your knowledge by selecting the correct answers. You will immediately see if you’re right or wrong.
Score: 0
Reset Quiz