0% found this document useful (0 votes)
6 views2 pages

Variables New

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Variables New

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Variable Name

Python variable names must adhere to the following rules:

● They must start with a letter (a-z, A-Z) or an underscore (_).


● The rest of the name can include letters, numbers, and underscores.
● Variable names are case-sensitive (e.g., myVariable and myvariable are different).
● Avoid using Python reserved words (like for, while, True, etc.) as variable names.

1. Assigning values to multiple variables


a, b, c = 1, 2, 3
print(a, b, c) # Output: 1 2 3
2. Assigning the same value to multiple variables
x = y = z = 0
print(x, y, z) # Output: 0 0 0
3. Using a tuple for assignment
coordinates = (10, 20)
x, y = coordinates
print(x, y) # Output: 10 20
4. Using a list for assignment
numbers = [1, 2, 3]
a, b, c = numbers
print(a, b, c) # Output: 1 2 3
5. Swapping variables
a, b = 5, 10
a, b = b, a
print(a, b) # Output: 10 5

1. Assigning values to multiple variables


a, b, c = 1, 2, 3
print(a, b, c) # Output: 1 2 3
2. Assigning the same value to multiple variables
x = y = z = 0
print(x, y, z) # Output: 0 0 0
3. Using a tuple for assignment
coordinates = (10, 20)
x, y = coordinates
print(x, y) # Output: 10 20
4. Using a list for assignment
numbers = [1, 2, 3]
a, b, c = numbers
print(a, b, c) # Output: 1 2 3
5. Swapping variables
a, b = 5, 10
a, b = b, a
print(a, b) # Output: 10 5
These techniques make variable assignment flexible and concise.

Here are more examples of multiple variable assignments in Python:

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]

for name, score in zip(names, scores):


print(f"{name} scored {score}")
# Output:
# Alice scored 85
# Bob scored 90
# Charlie scored 95
8. Unpacking Strings
a, b, c = "XYZ"
print(a, b, c) # Output: X Y Z
9. Unpacking Ranges
a, b, c, d, e = range(5)
print(a, b, c, d, e) # Output: 0 1 2 3 4
10. Combining Multiple Assignments
x, y = 10, 20
a, b, c = "hi", [1, 2], (3, 4)

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

# 2. Outputting multiple variables


x = 10
y = 20
print(x, y) # Output: 10 20

# 3. String concatenation
x = "Hello"
y = "World"
print(x + " " + y) # Output: Hello World

# 4. f-string formatted output


x = 10
y = 20
print(f"x = {x}, y = {y}") # Output: x = 10, y = 20

# 5. String formatting using .format()


x = 10
y = 20
print("x = {}, y = {}".format(x, y)) # Output: x = 10, y = 20

# 6. Printing a variable and its type


x = 10
print(f"x = {x}, type = {type(x)}") # Output: x = 10, type = <class 'int'>

# 7. Printing a list of variables


numbers = [1, 2, 3, 4]
print(numbers) # Output: [1, 2, 3, 4]

# 8. Printing a dictionary
person = {"name": "Alice", "age": 25}
print(person) # Output: {'name': 'Alice', 'age': 25}

# 9. Using end parameter with print()


x = 10
y = 20
print(x, end=", ")
print(y) # Output: 10, 20

You might also like