📖 Day 2: Variables & Data Types

Sunday, April 5, 2026 | 6:00 PM

🎯 Today's Learning Objectives

By the end of today, you will be able to:

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

name = "John" age = 25 height = 5.9

Breaking this down:

Using Variables

name = "John" print(name) print("Hello, " + name)
John Hello, John

💡 Variable Naming Rules

  • Must start with a letter or underscore _
  • Can contain letters, numbers, and underscores
  • Case sensitive: Namename
  • Can't use Python keywords (print, if, for, etc.)
  • Use lowercase with underscores: my_age not myAge

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:

age = 25 year = 2026 count = 0 negative = -100 print(age) print(type(age)) # type() tells us the data type
25 <class 'int'>

Float (float)

Numbers with decimals:

height = 5.9 price = 99.99 pi = 3.14159 print(height) print(type(price))
5.9 <class 'float'>

String (str)

Text. Always use quotes!

name = "John" city = "Mumbai" message = "Hello World" print(name) print(type(message))
John <class 'str'>

📝 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):

is_student = True is_adult = False print(is_student) print(type(is_student))
True <class 'bool'>

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 string to int number = int("25") print(number) print(type(number)) # Convert float to int (loses decimal) converted = int(3.9) print(converted) # Becomes 3, not 4!
25 <class 'int'> 3

Convert to Float: float()

# Convert string to float price = float("99.99") print(price) # Convert int to float temperature = float(25) print(temperature)
99.99 25.0

Convert to String: str()

# Convert number to string age = 25 age_text = str(age) print(age_text) print(type(age_text))
25 <class 'str'>

Combining Strings and Numbers

age = 25 message = "I am " + str(age) + " years old" print(message)
I am 25 years old

💡 Pro Tip: F-Strings (Modern Way)

Instead of str() conversion, use f-strings:

age = 25
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:

name = input("What is your name? ") print(f"Hello, {name}!")

What happens:

  1. Python displays: "What is your name? "
  2. User types something: "John"
  3. Python stores it in the name variable
  4. Print shows: "Hello, John!"

Important: input() Returns a String

input() always returns text, even if the user types a number:

age = input("How old are you? ") # User types: 25 print(type(age)) # It's a string, not an integer!
<class 'str'>

Getting Numbers from User Input

If you need a number, convert it:

# Get age as string, convert to int age = int(input("How old are you? ")) print(f"Next year you'll be {age + 1}")
How old are you? 25 Next year you'll be 26

Real Example: Age Calculator

# Get user's birth year birth_year = int(input("What year were you born? ")) # Calculate age current_year = 2026 age = current_year - birth_year # Display result print(f"You are {age} years old!")

🎓 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:

age = input("Age: ")
print(age + 1) # Error! Can't add string + number

✅ Correct:

age = int(input("Age: "))
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

🌍 Outside India? Private 1-on-1 tutoring available →