🎯 Today's Learning Objectives
By the end of today, you will be able to:
- Understand what variables are and why we use them
- Create variables and assign values to them
- Know the different data types: int, float, string, bool
- Convert between data types
- Take input from users using input() function
1️⃣ What are Variables?
Think of Variables as Containers
A variable is like a labeled box that holds information. We give it a name, and Python remembers what's inside.
Real-World Example
Imagine you have a box labeled "age". You put the number 25 in it. Later, when you need to know the age, you just look at the box!
Creating Variables
Breaking this down:
name- Variable name (like a label)=- Assignment operator (put this value in the box)"John"- The value (what goes in the box)
Using Variables
💡 Variable Naming Rules
- Must start with a letter or underscore _
- Can contain letters, numbers, and underscores
- Case sensitive:
Name≠name - Can't use Python keywords (print, if, for, etc.)
- Use lowercase with underscores:
my_agenotmyAge
2️⃣ Data Types
What are Data Types?
Different kinds of data require different types. Python automatically detects the type, but you need to understand them.
| Data Type | What It Is | Example | Code |
|---|---|---|---|
| int | Whole numbers | 25, -10, 0 | age = 25 |
| float | Decimal numbers | 3.14, 5.9, -0.5 | height = 5.9 |
| str | Text/words | "John", "Hello" | name = "John" |
| bool | True or False | True, False | is_student = True |
Integer (int)
Whole numbers without decimals:
Float (float)
Numbers with decimals:
String (str)
Text. Always use quotes!
📝 String Quotes
You can use single ' ' or double " " quotes:
name = "John" ✅
name = 'John' ✅
Both work the same way!
Boolean (bool)
True or False (very useful for decisions, more later):
3️⃣ Type Conversion
Converting Between Types
Sometimes we need to convert one type to another. Python provides functions for this:
Convert to Integer: int()
Convert to Float: float()
Convert to String: str()
Combining Strings and Numbers
💡 Pro Tip: F-Strings (Modern Way)
Instead of str() conversion, use f-strings:
message = f"I am {age} years old"
print(message)
Output: I am 25 years old
Much cleaner! We'll use f-strings from now on.
4️⃣ Taking User Input
The input() Function
The input() function lets us ask the user for information:
What happens:
- Python displays: "What is your name? "
- User types something: "John"
- Python stores it in the
namevariable - Print shows: "Hello, John!"
Important: input() Returns a String
input() always returns text, even if the user types a number:
Getting Numbers from User Input
If you need a number, convert it:
Real Example: Age Calculator
🎓 Key Takeaways from Day 2
- Variables store information in labeled boxes
- Main data types: int, float, str, bool
- Convert between types using int(), float(), str()
- input() gets information from the user (always a string)
- Use f-strings to combine text and variables
- Always convert input() to the type you need
📝 Homework (Due Day 3)
Practice variables and data types:
- Create a program that asks for the user's name and age, then displays: "You are [name] and [age] years old"
- Create a program that calculates the area of a rectangle (ask for length and width)
- Create a program that converts temperature from Celsius to Fahrenheit (Formula: F = C × 9/5 + 32)
- Bonus: Create a program that asks for a number, then displays it 3 times using different data types
Save all files as: day2_homework_1.py, day2_homework_2.py, etc.
❌ Common Mistakes from Day 2
Mistake 1: Forgetting Quotes Around Strings
❌ Wrong: name = John
✅ Correct: name = "John"
Mistake 2: Not Converting input() to Numbers
❌ Wrong:
print(age + 1) # Error! Can't add string + number
✅ Correct:
print(age + 1) # Works! Both are numbers
Mistake 3: Variable Names with Spaces
❌ Wrong: my age = 25
✅ Correct: my_age = 25
📚 Liked these free notes?
They're from a live 3-month Python + Modern AI course taught by an IIT Kanpur PhD Scholar — 24 live sessions, 8+ real projects, and a 100% refund guarantee.
🚀 Join the Live Course 💬 WhatsApp the Instructor