Multiple Assignment and Comments

In this tutorial you will learn:

  1. Multiple assginment
  2. Comments

1 Multiple assginment:

In the previous tutorial Variables and Inputs/Outputs we saw how to make a variable and assign to it a value using the assignement sign ‘=’.
In python we can create more than one variables on the same line, lets see an example:

0x, y, z = 22, 23, 24
1
2print(x, y, z)

OUTPUT: 22 23 24

As you see we have created 3 variables x, y and z then we have used the assginment sign ‘=’ to asign the values to the variables.
The first value (22) will be assigned to the x variable and the value (23) will be assigned to the (y) variable and so on, each variable has to be in the same order of the value that will be assigned to it.
We could also add multiple assginment statement ‘=’ on the same line using the semicolons to seperate each assignement:

0x = 22; y = 23; z = 24
1
2print(x, y, z, end=', ')

OUTPUT: 22, 23, 24

As you see, it is the same way that we used to create a variable and assign to it a value, the only difference here is that we have added 3 variables on the same line and to do that we have to add the semicolon ‘;’ not the comma ‘,’.
The semicolon will tell the python interpreter that this statement has ended.
In the next tutorial when you learn about operators you will learn how to make multi-line assignement, for now just learn how make multiple assginment on the same line.

2 Comments:

Comments are some text that we use to describe what our code does, this is very useful when you have alot of code, you will use comments to describe the work of each part of the code this will make your code easier to read by other developers if you are working with a team.
In python we create a comment by using the hash “#” symbol.
Anything that follow the hash “#” symbol will be considered as comment and it will be ignored by the interpreter.
Note that the hash “#” symbol allows you to write a one line comment, lets see an example:

0#here we using multiple assginment to assign values to our variables
1x, y, z = 22, 23, 24
2
3#we use the print function to print our variables
4print(x, y, z)

OUTPUT: 22 23 24

As you see we have used comments to describe each part of our code and when we run the code our comments are ignored by the interpreter, only the variables are printed.
Now lets see what will happen if we use the hash symbol to write multi-line comments:

0#here we using multiple assginment to 
1assign values to our variables
2x, y, z = 22, 23, 24
3
4#we use the print function to print our variables
5print(x, y, z)

When you run this code you will get an error because the hash symbol allows us to write only one line comment.
To write multi-line comments we can add hash symbol on each line:

0#here we using multiple assginment to 
1#assign values to our variables
2x, y, z = 22, 23, 24
3
4#we use the print function to print our variables
5print(x, y, z)

OUTPUT: 22 23 24

Or we can use triple quotes, either “’ or “”” :

0'''
1here we using multiple assginment to 
2assign values to our variables
3'''
4x, y, z = 22, 23, 24
5
6#we use the print function to print our variables
7print(x, y, z)

OUTPUT: 22 23 24

Exercices Section:

Exercice 1:

Find the correct syntax:

  1. x, y = 4, 5
  2. x; y; = 4; 5
  3. x, y; z = 4, 5, 6
  4. x = 4, y = 5, z = 6
  5. #this comment is
    a test comment.

Adnan

Read more posts by this author.