Comments in Python

What are Comments?

Comments are the piece of code that is generally ignored by the Interpreter, it helps the programmer to keep track of the code, organize it properly, and Maintain the code collectively in a team. It’s helpful and considered as a good practice to comment on your code because.

Advantages of using Comments?

hide
  1. Using comments in programs makes our code more understandable.
  2. It makes the program more readable which helps us remember why certain blocks of code were written.
  3. Prevents the execution of some lines or write a quick pseudo-code for the program.
  4. When you can’t use a function name to explain something.
  5. Warning of consequences of the particular block of code.

There are 2 types of comments in Python, Single Line Comments and Multiline Comments.

Single Line Comments

In python single comments are denoted by “#”, like the example given below. The interpreter ignores all the text after #

#this line is a comment

print('Hello') # anything after hash is a comment like this

These were 2 Examples of Single Line comments in python, it’s too easy, isn’t it?

Multi-line Commnets

Python doesn’t offer a separate way to write multiline comments. However, there are other ways to get around this issue. We can use # at the beginning of each line of comment on multiple lines.

# it is a
# multiline
# comment

String Literals for Multi-line Comments

Even though there is no unique way to write multiline comments in Python, we know that the Python
interpreter ignores the string literals that are not assigned to a variable.
So, we can even write a single-line comment as:

#this is a comment
'this is an unassigned string as a comment '

Here, we can see that the second line of the program is a string but is not assigned to any variable or
function.

So, the interpreter ignores the string. In a similar way, we can use multiline strings (triple quotes) to write multiline comments. The quotation character can either be or

'''
I am a
multiline comment!
'''
print("Hello World!")

"""
I am a also
multiline comment!
"""

Here, the multiline string isn’t assigned to any variable, so it is ignored by the interpreter. Even though it is not technically a multiline comment, it can be used as one.

Variables in Python

What are Variables ?

Variables in Programming act like containers that can store different types of data or any data value in them, a variable in a python program gives data to the computer for processing.

Features of Variables in Python

hide
  1. Variables play an important role while writing a code.
  2. Variabes are the containers which stores some values
  3. All the variables consists of some values assigned to it.

How to Create Variables in Python?

In python, a variable can be created by just naming it and assigning some value to it, a variable is created the moment you first assign a value to it.

x = "Your Name"
print(x)

# This is a string (Data Type) ..you will know more about datatypes later

To understand the importance of variables lets see an example

print(" Mr Character is a really enthusiastic person he is a good singer he lives in xyz he also played a lots of roles in different plays xyz....and more ")

# If this was the condition and like this more varibles are to be considerd
  
# This is also example of a variable

We just wrote two lines of code but what if you have to write a characters name in a story in 50 or 60 lines of code or more then after some time you realised that you need to change the name then would you prefer to change 40 times or just one time so here comes the use of variables.

You would have placed the variable in the place of the character name then you can change the value in the variable then the name will be changed everywhere.

Rules for Creating Variables in Python

Conditions for creating a variable in Python

hide
  1. A variable name must start with a letter or the underscore character
  2. A variable name cannot start with a number
  3. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  4. Variable names are case-sensitive (age, Age and AGE are three different variables)
#These are the correct ways of writing variables.
somename   = "anyname"
some_name  = "anyname"
_some_name = "anyname"
someName   = "anyname"
SOMENAME   = "anyname"
somename2  = "anyname"

# These are the incorrect ways of writing variables.
some name = "anyname"
1somename = "anyname"
some+Name = "anyname"
some-Name = "anyname"

Data Types in Python

What is a Datatype ?

In previous lessons, we stored data in variables. There, some text was in quotes and some were not in quotes. As computer don’t know how to put something under a type of data.
For example, if you write text and number then You should tell the computer which one is text and which integer

MyVar1 = "Some Text" # This is a text or String
print(MyVar1)

MyVar2 = 12 # This is an integer
print(MyVar2)

Different Data Types in Python

Data TypeTo ChangeDenoted by
Stringstr()” “
Integerint()None
Floatfloat()None
Listslist()[“a”,”b”]
Tuplestuple()(“a”,”b”)
Dictionarydict(name = “somename”,work =”somework”){“a”:”b”,”c”:”d”}
Booleanbool(1)True / False
Setsset((“a”,”b”)){“a”,”b”,”c”}
#Different Data Types stored in some variables
somestr   = "anyname"
someint   = 12
somefloat = 15.25
sometuple = ('abc', 'xyz', 'mno')
somedict  = {"abc":"bcd","cde":"def"}
somebool  = true
someset   = {'abc', 'xyz', 'mno'}

Now let’s dig all the data types one by one.

Strings in Python

Strings Literals

  • To make a string in python you need to include the text between quotes
  • You can Use either single quotes or double quotes. Like of the example given below
# Can use either single or double quotes
print("Using double quotes")
print('Using single quotes')

Single Line – Strings in Variables

  • You can easily assign string values to variables
  • Create a variable and set its value to a string with using single or multiple quote as discussed earlier
# Can use either single or double quotes
dblQuoteVar = "Using double quotes"
singleQtVar = "Using single quotes"
print(dblQuoteVar)
print("and")
print(singleQtVar)

Multi-Line – Strings in Variables

  • These were the single line strings stored in a variable(s).
  • Now we will How to store multiline strings as variables.
multilineString = """
This is a multiline string
This is using three double quotes
we can also three single quotes
\n
"""
print(multilineString)
# or
multiline_single = '''
This is a multiline string
This is using three double quotes
we can also three single quotes
'''
print(multiline_single)

Note: “\n” character is used to give a line break, A new line starts after “\n” and hence its also known as New Line Character.