Python Complete Series – 03

Learn Python Complete Series.

Lecture 3: Outline

  • Python #Comments
  • Multi line comments
  • Python Variables
  • Variable names, assigning value
  • Print variable

#Comments

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.

Example;

The line with the # sign is a comment.

Python Complete Series - 03 1 Top10.Digital

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.

Example;

Python Complete Series - 03 2 Top10.Digital

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.

Example;

Python Complete Series - 03 3 Top10.Digital

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.

Python Variables

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;

Python Complete Series - 03 4 Top10.Digital

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;

Python Complete Series - 03 5 Top10.Digital

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.

Python Complete Series - 03 6 Top10.Digital

Variable Names:

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)

Example;

Legal and illegal declaration

Python Complete Series - 03 7 Top10.Digital

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.

Example;

Python Complete Series - 03 8 Top10.Digital

And allocate the same value to a multiple variable(s) like a=b=c=”value”

Python Complete Series - 03 9 Top10.Digital

Output Variables

To print the variable print (statement) is a command. The print is in use to display the variable on the screen in Python.

Example;

x=’’awesom’’

print(“Python is ”+x)

learn Python

Further, To combine characters use + sign between both variable, look the example below,

Python tutorial

Moreover, The + sign works as a Mathematical Operator for numbers.

Python

But combining a number with a string is not possible, Python gives you an error.

Python