Most beginners think coding is hard…
But the truth?
It’s just about storing, understanding, and transforming data.
Today, you learned the foundation of Python — and this is where real programmers are built.
1. What is a Variable?
A variable is like a container that stores data.
name = "Alice"
age = 25
👉 Here:
-
namestores a string -
agestores a number
💡 Think of variables as labeled boxes where you keep information.
2. Data Types in Python
Python has different types of data:
🧩 Common Data Types
| Data Type | Example | Description |
|---|---|---|
| int | 10 | Whole numbers |
| float | 3.14 | Decimal numbers |
| str | "Hello" | Text |
| bool | True | True/False values |
Example:
a = 10 # int
b = 3.5 # float
c = "Python" # string
d = True # boolean
3. Checking Data Type
Use type() to check:
x = 100
print(type(x))
👉 Output: <class 'int'>
4. Typecasting (Type Conversion)
Typecasting means converting one data type into another.
Examples:
x = "10"
# Convert string to integer
y = int(x)
# Convert integer to float
z = float(y)
# Convert number to string
s = str(z)
Important Note:
int("hello") # ❌ Error
👉 You can only convert compatible values.
Why Typecasting Matters?
- Taking user input
- Performing calculations
- Formatting output
Example:
age = input("Enter your age: ")
age = int(age)
print(age + 5)
Real-Life Example
price = "100"
quantity = 2
total = int(price) * quantity
print("Total:", total)
Assignment (Practice Time )
Basic Level
-
Create variables:
- Your name
- Your age
- Your favorite number
- Print their data types.
Intermediate Level
-
Take user input for:
- Name
- Age
-
Convert age into integer and print:
"Your age after 10 years will be: X"
Advanced Level
- Write a program:
# Input: price as string
# Input: quantity as int
# Output: total price
- Convert:
- int → float
- float → string
- string → int
Print all results.
Bonus Challenge
- What will be the output?
x = "5"
y = 2
print(x * y)
👉 Explain why.
.png)

0 Comments:
Post a Comment