Create variables using meaningful names and assignment.
๐ฆ Data Types & I/O
Learn how Python stores values, identifies their types, accepts keyboard input, converts data and produces clear formatted output.
name = input("Name: ")
age = int(input("Age: "))
next_age = age + 1
print(f"Hello, {name}!")
print(f"Next year: {next_age}")
Next year: 21
By the End of This Level
You will be able to store, inspect, receive, convert and display data correctly.
Distinguish numeric, text, Boolean and empty values.
Use input() and convert text into required data types.
Produce readable output with commas and f-strings.
Variables and Assignment
A variable is a meaningful name that refers to a value. Python creates the variable when an assignment statement runs.
In student_name = "Venu", the assignment operator = evaluates the value on the right and associates it with the name on the left.
= assigns a value; it does not mean โis mathematically equal to.โ
Create and Update Variables
course = "Python"
lessons = 12
progress = 2
progress = progress + 1
print(course)
print("Completed:", progress)Python Completed: 3
Start Correctly
name
student_name
marks2Begin with a letter or underscore; digits may appear later.
Use Valid Characters
total_marks
course1
_countUse letters, digits and underscoresโnever spaces or hyphens.
Avoid Keywords
# Invalid names
class = 10
if = TrueReserved Python keywords cannot be variable names.
Choose Meaningful Names
student_age = 20
total_price = 499.0Clear names explain the value without needing extra comments.
Python Numeric Data Types
Python provides different numeric types because whole numbers, decimal values and complex values represent different kinds of information.
int
Whole numbers without a decimal point, such as 25, -7 and 0.
float
Numbers with a decimal point, such as 72.5 and -0.25.
complex
Numbers containing real and imaginary parts, such as 3 + 2j.
bool
Logical values True and False; Boolean is related to integers.
| Value | Type | Typical use |
|---|---|---|
42 | int | Count, age, score |
85.75 | float | Average, price, measurement |
2 + 3j | complex | Scientific and engineering calculations |
True | bool | Conditions and yes/no states |
Strings, Boolean Values and None
๐ค String โ str
A string stores text inside matching quotes.
name = "CodeBhavya"
grade = 'A'
message = "Learn and build"๐ฆ Boolean โ bool
A Boolean records whether something is true or false.
is_registered = True
is_completed = Falseโญ None โ NoneType
None represents the intentional absence of a value.
result = None
selected_course = None"25" is a string because it is inside quotes, while 25 is an integer. Their appearance is similar, but their behavior is different.
Check a Value with type()
The built-in type() function reports the data type of a value or variable.
Inspect Several Values
age = 20
average = 87.5
name = "Venu"
is_ready = True
print(type(age))
print(type(average))
print(type(name))
print(type(is_ready))<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
Keyboard Input with input()
input() displays an optional prompt, waits for the user, and always returns the entered data as a string.
input("Age: ")20"20"Receive and Display a Name
name = input("Enter your name: ")
city = input("Enter your city: ")
print("Student:", name)
print("City:", city)Enter your name: Venu Enter your city: Hyderabad Student: Venu City: Hyderabad
Even when the user types digits, input() returns text. Convert the value before performing numeric calculations.
Type Conversion
Type conversion creates a value of another compatible type. Common conversion functions are int(), float(), str() and bool().
| Function | Purpose | Example | Result |
|---|---|---|---|
int() | Convert to integer | int("25") | 25 |
float() | Convert to decimal number | float("7.5") | 7.5 |
str() | Convert to text | str(100) | "100" |
bool() | Convert to logical value | bool(1) | True |
Calculate Using Converted Input
length = float(input("Length: "))
width = float(input("Width: "))
area = length * width
print("Area =", area)Length: 8 Width: 5 Area = 40.0
age = int("20")
price = float("49.5")The text contains a compatible numeric value.
age = int("twenty")
count = int("4.5")The text cannot be directly represented as an integer.
Formatted Output
Use commas for simple labelled output and f-strings when values must appear naturally inside a message.
name = "Venu"
score = 92
print("Student:", name)
print("Score:", score)print() automatically places a space between comma-separated values.
name = "Venu"
score = 92
print(f"{name} scored {score}")Prefix the string with f and place expressions inside braces.
Student Result Card
name = input("Name: ")
marks = float(input("Marks: "))
print(f"Student: {name}")
print(f"Marks: {marks}")
print(f"Passed: {marks >= 40}")Name: Venu Marks: 78.5 Student: Venu Marks: 78.5 Passed: True
Common Data and Input Mistakes
"Age: " + 20A string and integer cannot be joined with + without conversion.
int("twenty")The text does not contain a valid integer representation.
age = input()print(age + "1")Entering 20 produces 201 because both values are strings.
๐ Data Types & I/O โ Quick Revision
Review the complete Level 2 lesson before attempting the quiz and programming problems.
A variable is created when a value is assigned using =.
int stores whole numbers and float stores decimal numbers.
str stores text inside matching quotes.
bool has the values True and False.
None represents the intentional absence of a value.
type() reports the data type of a value.
input() always returns a string.
Use int() or float() before numeric calculations.
f-strings insert values using braces such as f"Age: {age}".
A failed incompatible conversion normally raises a ValueError.
Level 2 Quick Quiz
Select one answer for every question, then check your score.
๐ฏ 5 Programming Problems โ Data Types & I/O
Solve every problem in the browser workspace. The runner uses the displayed sample input, so you can practise input(), conversion, variables, calculations and output.
A problem counts as Solved when its output passes before the official solution is opened.
Read a name and display the exact two-line welcome message.
VenuExpected output: Hello, Venu! and Welcome to CodeBhavya.input(), then use an f-string.Python Code Editor
Program Output
Run your program to see the output.
Test Case
name = input()
print(f"Hello, {name}!")
print("Welcome to CodeBhavya.")Read the current age as an integer and display the age next year.
20Expected output: Next age = 21input() using int() before adding 1.Python Code Editor
Program Output
Run your program to see the output.
Test Case
age = int(input())
next_age = age + 1
print("Next age =", next_age)Read integer length and width, calculate the area and display it.
8, 5Expected output: Area = 40int(), then multiply them.Python Code Editor
Program Output
Run your program to see the output.
Test Case
length = int(input())
width = int(input())
area = length * width
print("Area =", area)Read a decimal price and integer quantity, then display the total amount.
249.5, 2Expected output: Total = 499float() for price and int() for quantity.Python Code Editor
Program Output
Run your program to see the output.
Test Case
price = float(input())
quantity = int(input())
total = price * quantity
print("Total =", total)Read Celsius temperature, convert it and display the Fahrenheit value.
25Expected output: Fahrenheit = 77celsius * 9 / 5 + 32.Python Code Editor
Program Output
Run your program to see the output.
Test Case
celsius = float(input())
fahrenheit = celsius * 9 / 5 + 32
print("Fahrenheit =", fahrenheit)๐ฏ Give Every Value the Right Journey
Receive raw text using input().
Choose int, float or another suitable type.
Calculate using clearly named variables.
Display a labelled result with print() or an f-string.
๐ฌ Input, Conversion and Output โ Visual Flow
Follow user data from the keyboard until Python displays the calculated result.
โจ๏ธ How Python Processes User Input
input() ReadsโConvert TypeโProcess ValueโDisplay Result1. User Enters Data
The user types a value using the keyboard, such as 20 for age.
๐ Program Tracing โ Student Input
Move through the program one statement at a time and observe input consumption, variable types, calculations and output.
Click Next to begin tracing.
โ
๐ค Data Types & I/O โ Interview Questions
Answer aloud before opening each explanation.
What is dynamic typing in Python?
Python determines a variable's type from its current value at runtime. The same variable name can later refer to a value of another type.
What is the difference between int and float?
int represents whole numbers, while float represents floating-point numbers that can contain a fractional part.
Why must numeric keyboard input often be converted?
input() returns a string. Numeric calculations require a compatible numeric type such as int or float.
What is the difference between implicit and explicit conversion?
Implicit conversion is performed automatically by Python in compatible operations; explicit conversion is requested by the programmer using functions such as int() or float().
What does None represent?
None is a special singleton value representing no value or a value that has not yet been assigned meaningfully.
Why are f-strings useful?
F-strings place variables and expressions directly inside readable string templates using braces, making formatted output concise and clear.
๐ก Data Types & I/O โ Extra Tips
- 01
Name variables according to meaning: prefer
total_marksoverx. - 02
Use
type()while learning or debugging unexpected data behavior. - 03
Convert input immediately when the rest of the program expects a number.
- 04
Use
float()when valid input may contain a decimal point. - 05
Keep input, processing and output as three clear stages in beginner programs.
- 06
Test conversions with normal, boundary and invalid values to understand failures.
โ๏ธ Data Types & I/O โ Extra Practice Questions
- Create variables for a student's name, roll number, percentage and pass status; display their types.
- Read two integers and display their sum, difference and product.
- Read length and breadth as decimal values and display a rectangle's area and perimeter.
- Read a name and department, then create a two-line profile using f-strings.
- Convert the string
"125"to an integer and add25. - Predict the results of
type("10"),type(10)andtype(10.0). - Explain why
input() + input()may join digits instead of adding numbers. - Correct the error in
total = "Price: " + 499using two different approaches. - Read minutes and convert them into hours and remaining minutes.
- Build a bill program that reads item name, price and quantity and prints a formatted summary.
Data Types & I/O Complete
When you can choose suitable types, convert input correctly and solve the five programs without help, mark this level complete and continue.