Fall 2024 - P3
3.1.2 Python Hacks
Get ready to test your knowledge on variables with this homework.
Hack 1: Types of Variables
Write a Python program that assigns a variable and changes its value type multiple times. Use the type() function to track and print the type of the variable after each reassignment.
# Step 1: Initialize the variable
# Your code here
# Step 2: Reassign a new type
# Your code here
# Step 3: Reassign to another type
# Your code here
# Step 4: Reassign to a boolean
# Your code here
Hack 2: Variable Swapping
Swap the values of two variables without using a third (temporary) variable.
# Step 1: Initialize variables
a = 5
b = 10
# Step 2: Swap values without using a temporary variable
# Your code here
# Step 3: Print the swapped values
print("a =", a)
print("b =", b)
Hack 3: Variables in Memory
Create a Python program that assigns the same value to two different variables, and then check if they point to the same memory address. Use the id() function for comparison.
# Step 1: Assign the same value to two variables
# Your code here
# Step 2: Compare memory addresses
# Your code here
# Step 3: Print whether they are stored at the same location
# Your code here