Variables New
Variables New
1. Unpacking a List
values = [7, 8, 9]
x, y, z = values
print(x, y, z) # Output: 7 8 9
2. Unpacking a Tuple
coordinates = (4, 5, 6)
a, b, c = coordinates
print(a, b, c) # Output: 4 5 6
3. Unpacking with Different Data Structures
# Mixed data types
data = (1, [2, 3], "Python")
a, b, c = data
print(a, b, c) # Output: 1 [2, 3] Python
4. Assigning Using Expressions
a, b, c = 1 + 1, 2 * 2, 3 ** 2
print(a, b, c) # Output: 2 4 9
5. Using Default Values with Star * (Unpacking Operator)
When you don't know how many values you'll unpack:
a, *b, c = [1, 2, 3, 4, 5]
print(a) # Output: 1
print(b) # Output: [2, 3, 4]
print(c) # Output: 5
6. Swapping Multiple Variables
x, y, z = 1, 2, 3
x, y, z = z, x, y
print(x, y, z) # Output: 3 1 2
7. Using zip for Assignments
names = ["Alice", "Bob", "Charlie"]
scores = [85, 90, 95]
print(x, y) # Output: 10 20
print(a, b, c) # Output: hi [1, 2]
VARIABLES OUTPUT
# 1. Outputting a single variable
x = 10
print(x) # Output: 10
# 3. String concatenation
x = "Hello"
y = "World"
print(x + " " + y) # Output: Hello World
# 8. Printing a dictionary
person = {"name": "Alice", "age": 25}
print(person) # Output: {'name': 'Alice', 'age': 25}