Variables are used to store data in a Python program, while data types define the kind of value a variable can hold. Python variables and Data types practice Questions with Solutions Understanding variables and data types is the first step toward writing efficient Python programs. In this practice set, you’ll learn how to create variables, identify data types, assign values, and perform basic type conversions through beginner-friendly questions with solutions.
1. Python Program to Create and Print Variables
Difficulty: Foundation
Problem Statement
Write a Python program to create three variables: name, age, and city. Assign appropriate values and print them.
Expected Output
Name: Alex
Age: 21
City: New York
Python Solution
name = "Alex"
age = 21
city = "New York"
print("Name:", name)
print("Age:", age)
print("City:", city)
Explanation
Variables are used to store data in Python. Here, name stores a string, age stores an integer, and city stores another string.
Concepts Covered
- Variables
- String
- Integer
2. Python Program to Check the Data Type of Variables
Difficulty: Foundation
Problem Statement
Write a Python program to create variables of different data types and display their data types using the type() function.
Expected Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
Python Solution
name = "Alex"
age = 21
height = 5.9
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Explanation
The type() function returns the data type of a variable.
Concepts Covered
- type()
- String
- Integer
- Float
- Boolean
3. Python Program to Swap Two Variables
Difficulty: Foundation
Problem Statement
Write a Python program to swap the values of two variables without using a third variable.
Expected Output
Before Swapping
a = 10
b = 20
After Swapping
a = 20
b = 10
Python Solution
a = 10
b = 20
print("Before Swapping")
print("a =", a)
print("b =", b)
a, b = b, a
print("\nAfter Swapping")
print("a =", a)
print("b =", b)
Explanation
Python allows multiple assignment, making swapping simple without an extra variable.
Concepts Covered
- Variable assignment
- Multiple assignment
4. Python Program to Assign Multiple Values to Variables
Difficulty: Foundation
Problem Statement
Write a Python program to assign three values to three variables in a single statement and print them.
Expected Output
10
20
30
Python Solution
a, b, c = 10, 20, 30
print(a)
print(b)
print(c)
Explanation
Python supports assigning multiple values in a single line.
Concepts Covered
- Multiple assignment
5. Python Program to Assign the Same Value to Multiple Variables
Difficulty: Practice
Problem Statement
Write a Python program to assign the value 100 to three variables and display them.
Expected Output
100
100
100
Python Solution
x = y = z = 100
print(x)
print(y)
print(z)
Explanation
A single value can be assigned to multiple variables.
Concepts Covered
- Multiple variables
- Assignment operator
6. Python Program to Convert Data Types
Difficulty: Practice
Problem Statement
Write a Python program to convert an integer into a float and a string.
Expected Output
25
25.0
25
Python Solution
num = 25
print(num)
print(float(num))
print(str(num))
Explanation
Python provides built-in functions such as float() and str() to convert data types.
Concepts Covered
- Type conversion
- float()
- str()
7. Python Program to Find the Data Type After Type Conversion
Difficulty: Practice
Problem Statement
Write a Python program to convert an integer into a float and display its data type.
Expected Output
<class 'float'>
Python Solution
num = 50
num = float(num)
print(type(num))
Explanation
After conversion, the variable becomes a float.
Concepts Covered
- Data type conversion
- type()
8. Python Program to Store Different Types of Data
Difficulty: Challenge
Problem Statement
Create variables to store the following information and print them.
- Student Name
- Roll Number
- Percentage
- Passed (True/False)
Expected Output
Student Name: Emma
Roll Number: 15
Percentage: 89.5
Passed: True
Python Solution
student_name = "Emma"
roll_number = 15
percentage = 89.5
passed = True
print("Student Name:", student_name)
print("Roll Number:", roll_number)
print("Percentage:", percentage)
print("Passed:", passed)
Explanation
Different types of data can be stored using appropriate Python data types.
Concepts Covered
- String
- Integer
- Float
- Boolean
Chapter Summary
After completing this chapter, you have learned:
- Creating variables
- Python data types
- Using
type() - Multiple assignment
- Swapping variables
- Assigning one value to multiple variables
- Type conversion
- Storing different types of data
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
