Variables & Data Types
Variables & Data Types
Create a variable named city and assign the name of your city (as a string) to it.
Print the variable.
Create a variable named current_year and assign the current year (as an integer) to
it. Print the variable.
Create a variable exam_score and assign a floating-point number (e.g., 92.5) to it.
Print the variable.
Create a variable is_weekend and assign a boolean value (True or False) indicating
if it's currently the weekend. Print the variable.
What is the data type of the value assigned to city in problem 1? Print its type
using the type() function.
What is the data type of the value assigned to exam_score in problem 3? Print its
type.
Create a variable num_apples with the integer value 5. Create another variable
num_oranges with the integer value 3. Create a third variable total_fruit that
stores the sum of num_apples and num_oranges. Print total_fruit.
You have a variable price_string = "199.99". Convert this variable to a float and
store it in a new variable called price_float. Print price_float.
You have a variable quantity = 15. Convert this integer variable to a string and
store it in quantity_string. Print quantity_string.
Which of the following are valid Python variable names? my_var, 1stPlace, _count,
user name, MAX_VALUE, class
Create a variable temperature and assign it the value 25. Then, re-assign it the
value 28.5. Print the variable after the re-assignment. What is its type now? Print
the type.
Create a variable message = "I love Python". Try to convert this string to an
integer using int(). What happens? (You don't need to write code that runs without
error here, just predict the outcome).
Create a variable a = 10 and b = 3. What is the result of a > b? Store this result
in a boolean variable called is_a_greater. Print is_a_greater and its type.
Create a variable item_name = "Laptop" and another item_id = 786. Print a message
like "Item: Lauptop, ID: 786" using these variables. (Hint: You might need str()
conversion).
Assign the float value 5.0 to a variable value_float. Assign the integer value 5 to
a variable value_int. Print the type of both variables.
Create a variable x = 5. Reassign x to the string value "Hello". Print x and its
type after the reassignment. (This demonstrates dynamic typing).
What is the data type of the value False? Create a variable test_result and assign
False to it. Print its type.
Create three variables p=10, q=10.0, r="10". Print the type of each variable. Are
they the same?
You have num_string = "5". Add the integer 3 to this variable. What do you need to
do first for this addition to work correctly? Write the code to perform the
conversion and then the addition, printing the final result.