Comments and Variables in Python
In this module we are going to discuss about
- What are the comments in python?
- When to use comments?
- How to write comments?
- Types of comments
- Docstring comments
- Variables in python
- Variable declaration
- Multiple Variable assignments in Python
Comments in python
What are the comments in python?
Comments are mainly used in programming for easy explanatory purpose for the users.It is an explanation or easy description of source code of the program.Through these comments,we can easily understand that particular block of the code.It is a programmer coherent statement.
NOTE – This is just a text and it will not be executed by the interpreter.
When to use comments in python?
These comments are used when there is a complex code.After a long period of time we can not easily understand the code.So by these comments we can know the purpose of that block of code in the program.Comments should be
- Precise and clear
- Short and relevant
- Specific to the block of code
- Least redundant
- Maintain language ethic
How to write comments?
Comments in python are written using the ‘#’ symbol.When ‘#’ symbol is encountered anywhere,everything after it will be omitted till the line ends.
Ex – ‘ #comments in robotic_electronics ‘
Types of comments in python
There are two types of comments in python.They are
- Single line comments
- Multiline comments
Single line Comments
In python,we use # special character to start the comment.Lets take an example to understand the usage.Here a single line is kept as a comment by using the # character.
Multi line comments
Here more than one line are used as comment and all the lines are predefined by using the # character.
These are written using 3 double quotes that act as comments.It has 3 cases.They are
- When we declare only docstring comment,then the compiler returns the docstring itself.
- When we declare docstring after the code,then the compiler returns both the output of the code and the docstring.
- When we declare docstring before the code, then then the compiler return only the output of the code.
Variables in Python
- In python, a variable is declared as soon as you assign a value to it.
- It does not need any commands, unlike other programming languages.
- Python does not have any command to declare a variable.
Here we are directly declaring the variable ‘ y ’ with the value 10 without defining the variable.So we can directly get the output by using the print() function.But in other programming languages, we should first define the variable and then declare it with a value.
Multiple Variable assignments
Python allows us to assign a single value to several variables simultaneously. There are 2 types for declaring multiple variables in python. They are
- Here all the 3 variables are declared with a single value and all the 3 variable are assigned to the same memory location.
- We can also declare different values to multiple variable in a single allocation.For example
This comes at the end of the session on comments and variables in python. For updates regarding programming and electronics topics please check out our website https://roboticelectronics.in/ For any queries regarding the article, you can reach out through the comment box.