Multiple Assignment

An introduction to the use of Variables in Python

Multiple Assignment

With Python, you can assign one single value to several variables at the same time. This lets you initialize several variables at once, which you can reassign later in the program yourself, or through user input.

Introduction

Through multiple assignment, you can set the variables x, y, and z to the value of the integer 0:

x = y = z = 0
print(x)
print(y)
print(z)
Show Output
0
0
0

In this example, all three of the variables (x, y, and z) are assigned to the same memory location. They are each equal to the value of 0.

Python also allows you to assign several values to several variables within the same line. Each of these values can be of a different data type:

j, k, l = "shark", 2.05, 15
print(j)
print(k)
print(l)
Show Output
shark
2.05
15

In the example above, the variable j was assigned to the string "shark", the variable k was assigned to the float 2.05, and the variable l was assigned to the integer 15.

This approach to assigning multiple variables to multiple values in one line can keep your lines of code down, but make sure you are not compromising readability for fewer lines of code.

Multiple Assignment

Write a program that stores two variables. It should then print the variable values before changing them both in a single statement, it should then output the changes.

Write a program that stores one variable for each data type. It should then print the variable values before changing them all in a single statement. Once changed the variables should be outputted again.

8.4 Multiple Assignment Task
Submit your completed task...
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.