Learn Python Complete Series.
Lecture 3: Outline
- Python #Comments
- Multi line comments
- Python Variables
- Variable names, assigning value
- Print variable
Python provides the facility of comments to help in documentation. However, Comments are not part of the actual code but the guidelines for developer or user.
Python has a commenting capability for the purpose of in-code documentation.
Also, Comments are written with a # sign, and the rendered line is considered as comment.
The line with the # sign is a comment.
Moreover, Comments explain the code as well as make it more readable. It is also used to prevent the execution part of code because comments are not part of the actual code for execution. Therefore, The compiler skips the comments code to execute.
Multiple Line Comments:
Python doesn’t provide the syntax for multi-line comments. For multi-line comments, you need to add # with every line.
But multi-line string can be used as a comment by adding three quotes in your code. Add triple quotes at the start and end of the code to comment multi-line string. Python ignores the string literals that are not with a variable; the user can use a multi-line string with triple quotes.
A string that is not assigned to a variable, Python will read it, but due to comment code, it will skip that part of code.
Variables are used to store data value. As compared to other programming languages, Python has no such command or rules define to declare a variable. Variable is created when assigning a value to it.
Let’s see the example below;
Here x and y are variables that store the particular value.
No need to declare the data type even it can be changed later after it has been set up.
Have a look;
First the variable for x is 4 but then it changes to a “sally” now.
The string variables are always in single or double-quotes. Both work the same and properly.
The name of variable must be simple, short, descriptive, and readable. Following rules can define the variable names in Python.
- It starts with any letter, character, or the _ underscore.
- Not start with number
- Must contain alpha-numeric characters and underscores like A-z, 0-9, and _
- These are case-sensitive, so be careful about defining the same case variable. E.g. (time, Time and TIME are three different variables but same meanings)
Legal and illegal declaration
Allot a Multiple Variable’s value
Python gives an option of assigning values to multiple variables in one line.
You can use and assign values to several variables in one line. Python gives the facility to assign multiple variables in one line.
And allocate the same value to a multiple variable(s) like a=b=c=”value”
To print the variable print (statement) is a command. The print is in use to display the variable on the screen in Python.
print(“Python is ”+x)
Further, To combine characters use + sign between both variable, look the example below,
Moreover, The + sign works as a Mathematical Operator for numbers.
But combining a number with a string is not possible, Python gives you an error.