Python Variables and Data Types Practice Questions with Solutions

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

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

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

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

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

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

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

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

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

9. Python Program to Swap Two Variables

Problem Statement

Write a Python program to swap the values of two variables without using a third variable.

Python Solution

a = 10
b = 20

a, b = b, a

print("a =", a)
print("b =", b)

Sample Output

a = 20
b = 10

Explanation

Python allows swapping variables in a single line using multiple assignment, eliminating the need for a temporary variable.

Concepts Covered

  • Variable Swapping
  • Multiple Assignment

10. Python Program to Find the Data Type of User Input

Problem Statement

Write a Python program to accept input from the user and display its data type before and after type conversion.

Python Solution

value = input("Enter a number: ")

print("Before Conversion:", type(value))

number = int(value)

print("After Conversion:", type(number))

Sample Output

Enter a number: 25
Before Conversion: <class 'str'>
After Conversion: <class 'int'>

Explanation

The input() function always returns a string. You must convert it to the required data type using functions like int() or float().

Concepts Covered

  • input()
  • Type Conversion
  • type()

11. Python Program to Check Whether a Variable is an Integer or Float

Problem Statement

Write a Python program to check whether a variable contains an integer or a floating-point number.

Python Solution

number = 25.5

if isinstance(number, int):
    print("Integer")
elif isinstance(number, float):
    print("Float")

Sample Output

Float

Explanation

The isinstance() function checks whether a variable belongs to a specific data type.

Concepts Covered

  • isinstance()
  • int
  • float

12. Python Program to Convert All Basic Data Types

Problem Statement

Write a Python program to convert one value into different Python data types.

Python Solution

value = "50"

print(int(value))
print(float(value))
print(str(value))
print(bool(value))

Sample Output

50
50.0
50
True

Explanation

Python provides built-in functions such as int(), float(), str(), and bool() for converting data from one type to another.

Concepts Covered

  • int()
  • float()
  • str()
  • bool()
  • Type Casting

13. Python Program to Display Values and Their Data Types

Problem Statement

Write a Python program to create variables of different data types and display both their values and data types.

Python Solution

name = "Alice"
age = 22
height = 5.6
is_student = True

print(name, type(name))
print(age, type(age))
print(height, type(height))
print(is_student, type(is_student))

Sample Output

Alice <class 'str'>
22 <class 'int'>
5.6 <class 'float'>
True <class 'bool'>

Explanation

This program demonstrates how different values are stored using different Python data types and how to verify them using the type() function.

Concepts Covered

  • String
  • Integer
  • Float
  • Boolean
  • type()

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

Frequently Asked Questions (FAQs)

1. What is a variable in Python?

A variable in Python is a name used to store data in memory. It allows you to save values such as numbers, text, or lists and use them later in your program.


2. What are data types in Python?

Data types define the type of value stored in a variable. Common Python data types include int, float, str, bool, list, tuple, set, dict, and NoneType.


3. How do I declare a variable in Python?

You can create a variable by assigning a value using the assignment operator (=). Python automatically determines the data type.

name = "John"
age = 25
salary = 45000.50

4. How can I check the data type of a variable?

Use the built-in type() function to check the data type of any variable.

age = 25
print(type(age))

Output

<class 'int'>

5. Can I change the data type of a variable in Python?

Yes. Python allows type conversion using functions like int(), float(), str(), and bool().

num = "100"
number = int(num)

print(number)
print(type(number))

6. What is the difference between int and float?

  • int stores whole numbers without decimal points.
  • float stores numbers with decimal values.

Example:

age = 25        # int
price = 99.99   # float

7. Why are variables and data types important in Python?

Variables and data types are the foundation of Python programming. They help store, organize, and manipulate data efficiently, making them essential for web development, data analysis, automation, machine learning, artificial intelligence, and software development.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top