In this Python tutorial, you will learn about some essentials of Python. The topics that we are going to discuss are:

  • Statements
  • Expressions
  • Multiline Statements
  • Indentation
  • Comments
  • Identifiers
  • Keywords
  • Naming Convention

Statement

Python statements  are lines of codes or instructions that a Python interpreter can read and execute. For example, b = 10 is an assignment statement. Other kinds of statements are:

 if statement

for statement

while statement, etc.

What is an Expression?

An expression is also a Python statement with a logical sequence of objects & operators.

We can perform arithmetic operations, call functions and so on.

Examples

# Using Arithmetic expressions 
print((10 + 2) - 3)   

# Using functions in an expression 
print(max(2, 10))  

Multi-line statement

We have shown before how to write a single line statement. But there is also a way to write statements in multiple lines. A backward slash ( \ ) does the work for us. We call it ‘continuation character’ in this case.

Example:

b = 20
a = b + 3 + \
    4 +  6 + \
    7

We can use parenthesis (), curly braces {} & square brackets [] to write multiline statements.

a = (1+2+3
     +4+5)
print(a)

b = {'a': 2+3
       +4-2}

print(b)

c = [2+3,
     5+5+
     3]

print(c)

Output:

15
{'a': 7}
[5, 13]

Till now we have written a single statement in multiple lines but we can also write multiple statements in a single line using semicolons, as follows:

a = 2; b = 3; c = 4

Indentation

Indention is a sequence of definite spaces which are used in the beginning of python statements to define code block in python. The code block  is defined by curly braces {} in other programming languages like C, C++, JavaScript. Conventionally four (4) spaces are standard. Indentation is a must in python without indenting code properly you will end up seeing ‘IndentationError’.

indentation in python
def indentation():
    print("Inside First Block")
    for i in range(3):
        print("We are in 2nd block ")
    
    print("Out of Second Block")
    
indentation()
print("End of first block")

Output:

Inside First Block
We are in 2nd block 
We are in 2nd block 
We are in 2nd block 
Out of Second Block
End of first block

Comments

A Comment in programming is an short & specific explanation about source codes. Developers use comments to make code understandable to humans but compilers and interpreters ignore comments.

A Comment in Python starts with a hash mark (#) and whitespace character and sometimes using docstring [triple quotes “”” “””]

# This is a comment

There are two types of comments in python. 1. Single Line 2. Multi Line

Single Line: comments are written using (#)

# This program will print "Hello World"
print("Hello world")

Multiline: Comments are written using docstring (“”” “””) They are not actually comments they are documentation string but we consider them as comments.

"""
This function returns 
sum of two numbers.
"""
def function(a, b):
    return a + b
      
print(function(4,5))

Identifiers

Names of variables, functions, classes, modules and other objects are ‘identifiers’ in python. An identifier can contain a-z or A-Z, ‘_’ underscores and digits (0-9). Python does not allow punctuation characters (like @, $, and %) within identifiers.

var = 1

_var = 3
__var = 4

def func_name():
    return None

class ClassName:
    pass

Names in Bold Characters are identifiers. Remember that we can’t use keywords as identifiers.

Keywords

Keywords are reserved words in python. Each one of them has special meaning to do specific works. To see the all keywords of your python version call the help() function and then give ‘keywords’ command. In python 3.6 there are 33 keywords & in python 3.7 there are 35 keywords.

>>>help()
>>>help>keywords

Output:

Naming Conventions

There are some conventions of naming identifiers in python. These are important to make code human readable. Some of them are…

  1. All Kinds of Variables should be in lower case.
  2. Constant names must be fully capitalized.
  3. Words in names should be separate.
  4. Non-public instance variables should begin with a single underscore.
  5. If an instance name needs to be mangled, two underscores may begin its name.
  6. Function, Package & Module names should be all lower case.
  7. When you need multiple words, use an underscore to separate each word.
  8. Class names should be in CamelCase (ClassName) convention. Though,  python built-in classes are typically in lowercase.
  9. Exception classes should end in “Error”
  10. Method names should be all lower case
  11. Words in a method name should be separated by an underscore
  12. The non-public method should begin with a single underscore
  13. If a method name needs to be mangled, two underscores may begin its name.

 

+ posts

Author | Python-Django Developer

+ posts

Full-stack Developer (Python | Django | React | React-Native | Angular | Vue)