Share @ LinkedIn Facebook  python, basics
Python Programming Guide For Beginners - Part 3

Python Programming Guide For Beginners - Part 3

Learning About Python Comments

Although comments do not change the outcome of a program, they still play an important role in any programming and not just Python. Comments are the way to improve the readability of a code, by explaining what we have done in code in simple english. In this guide, we will learn about comments in Python and their types.

A comment is text that doesn’t affect the outcome of a code, it is just a piece of text to let someone know what you have done in a program or what is being done in a block of code. This is especially helpful when someone else has written a code and you are analysing it for bug fixing or making a change in logic, by reading a comment you can understand the purpose of code much faster then by just going through the actual code.

Types of Comments in Python

There are two types of comments in Python.

  1. Single line comment
  2. Multiple line comment

Single line comment

In python we use # special character to start the comment. Lets take few examples to understand the usage.

In [1]:
# This is just a comment. Anything written here is ignored by Python

Multi-line comment:

To have a multi-line comment in Python, we use triple single quotes at the beginning and at the end of the comment

In [2]:
'''
This is a 
multi-line
comment
'''
Out[2]:
'\nThis is a \nmulti-line\ncomment\n'

Simple Program To Understand The Use Of Comments In Python

In [5]:
'''
We are writing a simple program here
First print statement.
This is a multiple line comment.
'''
print("Hello Guys")

# Second print statement
print("How are You all?")

print("Welcome to BeginnersBook") # Third print statement
Hello Guys
How are You all?
Welcome to BeginnersBook

Understanding Python Variables

Concept: Variables are used to store data, they take memory space based on the type of value we assigning to them. Creating variables in Python is simple, you just have write the variable name on the left side of = and the value on the right side.

Important Points To Understand About Variables - Identifiers

These are the generic rules that you need to follow while creating variables in python.

Variable name is known as identifier.

  1. The name of the variable must always start with either a letter or an underscore (_). For example: _str, str, num, _num are all valid name for the variables.
  2. The name of the variable cannot start with a number. For example: 9num is not a valid variable name.
  3. The name of the variable cannot have special characters, they can only have alphanumeric characters and underscore (A to Z, a to z, 0-9 or _ ).
  4. Variable name is case sensitive in Python which means num and NUM are two different variables in python.

Simple Program For Understanding Variables - Identifiers In Python

In [2]:
num = 100
str = "CoderzColumn"
print(num)
print(str)
100
CoderzColumn

Program For Multiple Assignment - Variables In Python

In [4]:
x = y = z = 99
print(x)
print(y)
print(z)
99
99
99
In [5]:
a, b, c = 5, 6, 7
print(a)
print(b)
print(c)
5
6
7

Understanding Concatenation Operation On The Variables

In [7]:
x = 10
y = 20
print(x + y)

p = "Hello"
q = "World"
print(p + " " + q)
30
Hello World


Dolly Solanki  Dolly Solanki