Let’s get the basics of Python done !!

Let’s get the basics of Python done !!

Let’s get the basics of Python done !!

Did you liked it ??
+1
0
+1
0
+1
0
+1
0

In our previous blog we saw basics ad some theoretical knowledge of Python. In this blog let’s get some more basics clear.

Python Keywords

Keywords are the reserved words in python. We can’t use a keyword as a variable name, function name or any other identifier. Keywords are case sensitive.

# Get all keywords of python 3.6

import keyword

print(keyword.kwlist)

print("\nTotal number of keywords: ", len(keyword.kwlist))

Identifiers

Identifier is the name given to the entities like class, function, variables, etc. in python. It helps differentiating one entity from another.

Rules for writing identifier:

  1. Identifier can be combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
  2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
  3. Keywords cannot be used as identifiers.
abc12 = 12;
global = 1

Python Comments

Comments are lines that exists in computer programs that are ignored by compilers and interpreters.

Including comments in programs makes code more readable for humans as it provides some information or explanation about what each part of a program is doing.

In general, it is a good idea to write comments while you are writing or updating a program as it is easy to forget you though process later on, and comments written later may be less useful in the long term.

In python, we use hash(#) symbol to start writing a comment.

#Print Hello, word to console
print("Hello World")

Multi Line Comments

If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the beginning of each line.

#This is long comment
#and it extends
#Multiple lines

Another way of doing this is to use triple quotes, either ”’ or “””

"""This is also a 
perfect example of
multi-line comment"""

Python Indentation

  1. Most of the programming language like C,C++,Java use braces { } to define a block of code. Python uses indentation.
  2. A code block (body of a function, loop, etc.) starts with indentation and ends with the first unintended line. The amount of indentation is up to you, but it must be consistent throughout the block.
  3. Generally four whitespaces are used for indentation and is preferred over tabs.
for i in range(10): 
    print(i)

Indentation can be ignored in line continuation. But it’s a good idea to always indent. It makes the code more readable.

if True:
    print("Machine Learing")
    c = "AAIC"
if True: print("Machine Learing"); c = "AAIX"

Statements

Instructions that a Python interpreter can execute are called statements.

a = 1 #single statement

Multi-Line Statement

In python, end of a statement is marked by a newline character. But we can make a statement extend ove multiple lines with the line continuation character ().

a = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8
print (a)

 

a = 10; b = 20; c =30 #put multiple statements in a single line using ;

 

#another way is to use paranthesis
a = (1 + 2 + 3 + 
    4 + 5 + 6 +
    7 + 8)
print (a)

So with this we have covered some little basics part of the Python. In the next article we are going to study about Data types and variables.

So stay tuned!!!

Did you liked it ??
+1
0
+1
0
+1
0
+1
0

Leave a Reply

Your email address will not be published. Required fields are marked *