Lesson 2 - Variables, Data Types & I/O
Learning Objective
Create variables of the four core built-in types, inspect their types with type(), and read user input from the keyboard.
Introduction
A variable is a name that points to a value stored in memory. Unlike some languages, Python doesn’t require you to declare a variable’s type upfront — you just assign a value, and Python figures out the type for you.
This lesson covers the four types you’ll use constantly:
str(text)int(whole numbers)float(decimal numbers)bool(True/False)
You’ll also use input() to bring a user’s keyboard input into your program, and immediately hit one of Python’s most common beginner gotchas: input() always returns text, even if the user types a number.
The Four Core Data Types
Used for text values.
name = "Aditi"Used for whole numbers.
age = 24Used for decimal numbers.
height_m = 1.68Represents True or False.
is_student = TrueCode
# variables_and_io.py
# Variables and core data types
name = "Aditi" # str
age = 24 # int
height_m = 1.68 # float
is_student = True # bool
print(name, age, height_m, is_student)
print(type(name), type(age), type(height_m), type(is_student))
# Taking input from the user
favorite_color = input("What's your favorite color? ")
print(f"{name} likes {favorite_color}.")
# input() always returns a string — even when the user types digits
birth_year_text = input("What year were you born? ")
birth_year = int(birth_year_text) # convert str -> int
current_year = 2026
approx_age = current_year - birth_year
print(f"You're approximately {approx_age} years old.")Expected Output
Click to see expected output
Aditi 24 1.68 True
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
What's your favorite color? blue
Aditi likes blue.
What year were you born? 2000
You're approximately 26 years old.The two input() lines pause the program and wait for you to type an answer and press Enter.
Understanding type()
The type() function lets you inspect the data type of any value.
name = "Aditi"
age = 24
height_m = 1.68
is_student = True
print(type(name))
print(type(age))
print(type(height_m))
print(type(is_student))Expected Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>Understanding User Input
The input() function pauses your program and waits for the user to type something.
name = input("What is your name? ")
print(name)If the user types:
RahulThen the variable name stores:
"Rahul"Converting Input to Numbers
One of the most important Python concepts:
input() always returns a string (str), even if the user types digits.
Example:
age_text = input("Enter your age: ")
print(type(age_text))If the user enters:
25The output is:
<class 'str'>To perform arithmetic, you must convert the string to a number:
age = int(age_text)For decimal numbers:
weight = float(input("Enter your weight: "))🔧 Under the Hood
Why doesn’t Python make you declare variable types?
Python uses dynamic typing: a variable is just a label attached to an object in memory, and that object carries its own type.
When you write:
age = 24Python creates an integer object 24 and points the name age at it.
Later, you could write:
age = "twenty-four"and Python would happily repoint age at a string instead. The variable name itself has no fixed type.
This is different from statically typed languages (like Java or C++), where you must declare:
int age;and the compiler enforces that age can only hold an integer.
Dynamic typing is a big part of why Python code is faster to write, but it also means type-related bugs (like the input() gotcha above) only show up when the code runs, not before.
Student Challenge
Reflection Question
input()always hands you back a string, no matter what the user types. Why do you think Python’s designers chose that behavior instead of automatically detecting whether the input “looks like” a number and converting it for you?
By the end of this lesson, you should be able to create variables, identify their data types, accept user input, convert strings into numbers, and combine variables in formatted output using f-strings.