Python Variables and Data Types Practice Questions
Correct
0%
Which of the following is a valid variable name in Python?
In Python, variable names (identifiers) must follow these rules:
Cannot start with a digit. (2variable is invalid)
Can contain letters, digits, and underscores (_). (first_name is valid)
Cannot include special characters like - (first-name is invalid)
Cannot use reserved keywords like class.
Therefore, Option 2: first_name is the correct choice.
What is the correct way to assign the value 10 to a variable x in Python?
In Python, the single equals sign = is used for assignment. This sets the value of the variable on the left to the value on the right.
Why the other options are wrong:
Option 1 (x == 10) is a comparison operator, not an assignment.
Option 2 (x := 10) is the walrus operator introduced in Python 3.8, used in expressions, not standard assignment.
Option 4 (x <- 10) is not valid in Python; it is used in some other languages like R.
Thus, Option 3: x = 10 is correct.
Which of the following is a numeric data type in Python?
Python has several numeric data types:
int → integers (e.g., 5, -10)
float → floating-point numbers (e.g., 3.14, -0.5)
complex → complex numbers (e.g., 2 + 3j)
Why the other options are wrong:
Option 1 (str) represents strings (text), not numbers.
Option 2 (bool) represents Boolean values (True or False), not numeric values.
Option 4 (list) represents sequences of items, not a single numeric type.
Therefore, Option 3: int is correct.
Which of the following statements about Python variables is correct?
Python is a dynamically typed language, which means you do not need to declare the type of a variable explicitly. The interpreter automatically determines the type based on the value assigned.
Why the other options are wrong:
Option 2: Variables can store any type; explicit type declaration is unnecessary.
Option 3: Variables can store numbers, strings, lists, booleans, and other data types.
Option 4: Variables in Python can be reassigned to new values at any time.
What will the following code output?
x = "Hello"
y = "World"
x = y
print(x)
Here, x = y assigns the value of y ("World") to x. After this assignment, x no longer holds "Hello"; it holds "World".
Why the other options are wrong:
Option 1 (Hello) → The original value of x is overwritten.
Option 3 (x) → Variables print their value, not the variable name.
Option 4 (y) → Only the value "World" is printed, not the variable y itself.
This demonstrates variable assignment and overwriting values in Python.
Which of the following statements about Python’s data types is correct?
In Python:
Mutable types can be changed after creation, e.g., list, dict, set.
Immutable types cannot be changed after creation, e.g., int, float, str, tuple.
Why the other options are wrong:
Option 1: Not all types are mutable; tuple and str are immutable.
Option 2: Python uses dynamic typing; type declarations are not required.
Option 3: This is the opposite; int is immutable, list is mutable.
Understanding mutable vs immutable types is important for predicting behavior of objects when passed to functions or assigned to multiple variables.
What will be the output of the following code?
x = 10
x = float(x)
print(type(x))
Here, the integer 10 is converted to a float using float(x). The type() function returns the type of the object after conversion.
Step by step:
Initially, x is an int.
After x = float(x), x becomes 10.0, which is a float.
type(x) therefore returns <class 'float'>.
Why the other options are wrong:
Option 1:x is no longer an integer after conversion.
Option 2: No string conversion is applied.
Option 3:x is not a boolean; bool() was not used.
This demonstrates type conversion and how Python’s dynamic typing allows changing the type of a variable at runtime.
Which of the following statements about Python’s None is correct?
In Python, None is a special constant that represents the absence of a value or a null value. It is commonly used:
To indicate a variable has no value.
As a default return value for functions that do not explicitly return anything.
Why the other options are wrong:
Option 1 (0) → None is not numeric; it is a distinct singleton object.
Option 2 ("") → An empty string is different from None.
Option 3 (bool) → While bool(None) evaluates to False, None is not a boolean type itself.
Understanding None is important for handling missing or optional data and controlling program logic.
Which of the following demonstrates Python’s dynamic typing?
Python is dynamically typed, meaning the type of a variable is determined at runtime and can change.
Example:
x = 10 # x is an int
x = "Hello" # x is now a str
Why the other options are wrong:
Option 1: Python does not require type declaration.
Option 3: Variables can store strings and other types.
Option 4: Variables can be reassigned freely.
This flexibility makes Python code concise and easy to work with, but developers must be careful to avoid type-related errors.
Which of the following statements about multiple assignment in Python is correct?
Python supports multiple assignment, which allows assigning values to multiple variables in a single line.
Option 2: Multiple assignment can assign different values to each variable, not just the same value.
Option 3: Variables do not need to be of the same type; Python is dynamically typed.
Multiple assignment is useful for swapping variables or unpacking sequences efficiently:
x, y = y, x
What will be the output of the following code?
a = [1, 2, 3]
b = a
print(a is b)
The is operator in Python checks object identity, i.e., whether two variables point to the same object in memory.
Here:
a is a list [1, 2, 3].
b = a assigns the same object reference to b.
a is b returns True because both variables reference the exact same list object.
Why the other options are wrong:
Option 1 (False) → Would be true if b were a copy of a, not the same object.
Option 2 (None) → is does not return None.
Option 4 (Error) → No error occurs; this is valid Python syntax.
This question highlights the difference between identity (is) and equality (==), which is crucial when working with mutable objects.
Which of the following statements about numeric types in Python is correct?
Python supports three main numeric types:
int → integers (e.g., 5, -10)
float → floating-point numbers (e.g., 3.14)
complex → complex numbers (e.g., 2 + 3j)
Python can perform arithmetic with mixed types:
x = 5 # int
y = 2.5 # float
z = x + y # result is float
Why the other options are wrong:
Option 1: Python supports complex numbers.
Option 2: Floats are not automatically converted to integers; the result depends on the operation.
Option 4: Numeric types can be used in expressions with other numeric types.
Understanding numeric types and mixed-type arithmetic is important for accurate calculations and avoiding type errors.
What will be the output of the following code?
x = True
y = False
print(x and y or not y)
This expression demonstrates Boolean operations and operator precedence:
and has higher precedence than or.
Step by step:
x and y → True and False → False
not y → not False → True
False or True → True
Why the other options are wrong:
Option 2 (False) → Incorrect because not y evaluates to True.
Option 3 (None) → Boolean expressions never return None.
Option 4 (Error) → This is valid syntax; no error occurs.
This helps learners understand operator precedence and logical operations in Python.
Which of the following statements about variable scope in Python is correct?
In Python:
Local variables are variables defined inside a function; they cannot be accessed outside the function.
Global variables are defined outside functions and can be accessed anywhere in the module.
To modify a global variable inside a function, it must be declared with the global keyword.
Example:
x = 10 # global
def func():
y = 5 # local
global x
x = 20
func()
print(x) # prints 20
Why the other options are wrong:
Option 1: Variables are local inside functions unless declared global.
Option 2: Local variables cannot be accessed outside the function.
Option 4: Python does support global variables.
Which of the following statements about Python’s id() function is correct?
In Python, the id() function returns the unique identifier of an object, which usually corresponds to its memory address. This is useful for:
Checking if two variables point to the same object (is operator).
Understanding mutable vs immutable behavior.
Example:
a = [1, 2]
b = a
print(id(a) == id(b)) # True, same object
Why the other options are wrong:
Option 1:id() returns the identifier, not the value.
Option 2:id() does not modify the object or its memory.
Option 4:id() works with any Python object, not just numeric types.
What will be the output of the following code?
a = b = []
a.append(1)
print(b)
Here, a = b = [] assigns the same list object to both a and b.
When a.append(1) is called, it modifies the list object in place.
Since b references the same list, printing b shows [1].
Why the other options are wrong:
Option 1: The list is not empty; it was modified via a.append().
Option 3: No nested lists are created; both variables reference the same object.
Option 4: No error occurs; this is valid syntax.
This demonstrates a common pitfall with multiple assignment and mutable objects in Python.
Which of the following demonstrates type coercion in Python?
Python automatically coerces numeric types in operations to avoid type errors.
In 5 + 2.5, the integer 5 is coerced to a float (5.0) before the addition.
The result is 7.5, a float.
Why the other options are wrong:
Option 1 (5 + 3) → Both operands are integers; no coercion occurs.
Option 2 (5.5 - 2.5) → Both operands are floats; no coercion occurs.
Option 3 (2 * 3) → Both operands are integers; no coercion occurs.
This is important for understanding mixed-type arithmetic and predicting output types in Python expressions.
Which of the following statements about mutable and immutable objects is correct?
In Python:
Immutable objects (e.g., int, str, tuple) cannot be changed after creation. Any operation that seems to modify them actually creates a new object.
Mutable objects (e.g., list, dict, set) can be modified in place without creating a new object.
Example:
t = (1, 2)
# t[0] = 5 # This would raise an error
l = [1, 2]
l.append(3) # Modifies the list in place
Understanding this distinction is crucial for predicting behavior when passing objects to functions or copying objects.
Which of the following is the correct way to check if a variable x is an integer in Python?
In Python, the isinstance() function is used to check if an object is an instance of a specific type.
Example:
x = 10
print(isinstance(x, int)) # True
Why the other options are wrong:
Option 1 (type(x) == "int") → type(x) returns a type object, not a string. Correct comparison would be type(x) == int.
Option 3 (x.isint()) → No such method exists for integers.
Option 4 (x == int) → This compares a value to a type, which is invalid.
Using isinstance() is preferred for type checking, especially when dealing with subclasses or multiple types.
Which of the following statements about constants in Python is correct?
Python does not enforce constants like some other languages.
Developers follow the naming convention of using uppercase variable names to indicate that a value should be treated as constant.
Example:
PI = 3.14159
GRAVITY = 9.8
These can still be reassigned, but the convention signals to other developers that they should not change these values.
Why the other options are wrong:
Option 1: No built-in constant type exists in Python.
Option 2: Python does not have a const keyword.
Option 3: Python does not prevent reassignment; it relies on developer discipline.
This is important for writing maintainable and readable code, especially in large projects.
Quick Recap of Python Variables and Data Types Concepts
If you are not clear on the concepts of Variables and Data Types, you can quickly review
them
here before
practicing the exercises. This recap highlights the essential points and logic to help you solve
problems
confidently.
What Are Variables in Python?
A variable in Python is a name that stores a value in memory so that it can be reused later in the program. You create a variable using the assignment operator (=):
full_name = "Daniel"
age = 28
product_price = 149.75
Python does not require specifying a data type while creating a variable. It automatically detects the type based on the assigned value — this feature is called dynamic typing.
To check the data type of any variable:
type(product_price)
Rules for Naming Variables in Python
To avoid errors and improve code readability, Python has variable naming rules and best practices:
Must start with a letter or underscore (_)
Cannot start with a number
Cannot contain spaces or special characters other than _
Variable names are case-sensitive (Count, count, and COUNT are different)
Use readable and meaningful names when possible
Examples:
user_age = 32 # Good
height_cm = 175 # Good and meaningful
x1 = 10 # Works but not descriptive
2score = 12 # Invalid
Understanding Python Data Types With Examples
Every value in Python has a data type. Key Python data types include:
Test Your Python Variables and Data Types Knowledge
Practicing Python Variables and Data Types? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Variables and Data Types
Welcome to our Python Variables and Data Types exercises on Solviyo. We created this set of exercises to help you get a clear understanding of one of the most essential building blocks in Python. Variables allow us to store and manipulate data, and understanding different data types is crucial for writing efficient and error-free code. Whether you are just starting out with Python or revisiting the basics, these exercises will guide you step by step and give you hands-on experience.
In Python, variables act as containers for data, and each variable has a specific data type that determines what kind of value it can hold. In these exercises, you will learn how to declare variables, assign values, and work with data types such as numbers, strings, lists, tuples, and dictionaries. We’ve included exercises that let you practice converting between data types, performing arithmetic operations, manipulating strings, and accessing elements from collections. This approach ensures you understand not only the syntax but also the practical use of each type.
We’ve structured these exercises so you can progress gradually. You’ll start with basic variable assignments and arithmetic operations, move on to string handling and formatting, and then explore more complex data structures like lists, tuples, and dictionaries. Each task is designed to reflect real-world scenarios, so you can see how variables and data types are used in everyday programming. This makes learning interactive and relevant, rather than just theoretical.
Mastering variables and data types is a critical step before tackling loops, functions, and object-oriented programming. By practicing these exercises, you’ll gain confidence in handling data, avoid common mistakes, and write code that is clean and efficient. We’ve also included explanations for each exercise, so you can understand the reasoning behind each solution and reinforce your learning.
Alongside these exercises, We recommend exploring related topics like Lists, Strings, and Conditional Statements. These interconnected concepts will strengthen your foundation and prepare you for more advanced Python topics, coding challenges, and technical interviews.
Start practicing Python Variables and Data Types exercises today on Solviyo. By dedicating time to these exercises, you’ll build a strong foundation, sharpen your problem-solving skills, and become a more confident Python programmer. This is your first step toward mastering Python and taking on more complex programming challenges with ease.
Need a Quick Refresher?
Jump back to the Python Cheat Sheet to review concepts before solving more challenges.