UNIT-1(Lesson 4)
Expressions,Statements,Indentations
Expression
An expression is a combination of values(Constants), variables and operators.
Operators are symbols that represent particular actions. Operands participate in the action
performed by the operator.
Any expression evaluates to a single value, which becomes the value of the
expression.
Instructions that a Python interpreter can execute are called statements. For
example, a = 1 is an assignment statement.
Example 1 :
a = 10 # This is an assignment statement
b = 10 # This is an assignment statement
print(a + b) # a + b is an expression and print(a + b) is a statement
Example 2:
b = 10 # This is an assignment statement
c = 10 # This is an assignment statement
a = b * 5 + c # b * 5 + c is an expression, we are assigning this expression to the variable a then a = b
* 5 + c is an assignment statement
print(a)
In both the examples we have used simple arithmetic operators like + and * which performs
arithmetic addition and multiplication respectively. The operator + works as a concatenation
operator if the operands are strings(text).
Understanding Statements
A statement in Python is a logical instruction which can be read and executed by Python
interpreter.
In Python, we have different kinds of statements :
• Print statements
• Assignment statements
• Selective statements
• Iterative statements
• Function declaration statements
The purpose of Python's assignment statement is to associate variables with values in your
program.
An assignment statement contains at least one equal sign (=), to the left hand side of = we have
a variable and to the right hand side we have an expression.
For example, Let us consider the following code:
a = 15
print(a)
Here, a = 15 is a simple assignment statement and print(a) is a print statement used to display the
value of a.
Types of Assignment Statements
There are two types of assignment statements in Python. They are:
1. Basic assignment statements
2. Augmented assignment statements
The simple syntax for a basic assignment statement is:
variable_name = expression
In the example given below we are assigning a string value "CodeTantra" to a
variable called name.
name = "CodeTantra"
Augmented Assignment Statement
Augmented assignment is a combination of an arithmetic or a binary operation and
an assignment operation in a single statement.
We can combine arithmetic operators in assignments to form an augmented
assignment statement.
The combined operations of the augmented assignments are represented using the
following operators : +=, -=, *=, /=, %=
Let us consider a simple example:
a += b
The above statement is a shorthand for the below simple statement.
a=a+b
What is an Indentation?
Python uses white spaces to delimit control flow block.
The first line of the function definition is called the header, the rest is called the
body.
The header has to end with a colon (:) and the body has to be indented.
By convention, the indentation is always four spaces or one Tab space.
Interpreter returns Indentation Error when we are not providing correct
Indentation.
Indentation in Python:
if(i == 10): # Indentation started with :
print("i is 10") # this print statement is in if block(providing tab space at front of
print statement)
else: # else is in out of if block
print("i is not 10") #print statement is in else block
A simple program in Python
We can run a Python program in two ways. They are:
• Interactive mode
• Scripting mode
1. Interactive mode(REPLRead Evaluate Print Loop)
• Interactive mode is a command line shell which gives immediate feedback for each statement,
while running previously fed statements in active memory.
• As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole.
• Interactive mode is great for quickly and conveniently running single lines or blocks of code.
• Interactive mode is a good way to play around and try variations on syntax.
• Debugging is easy in the interactive mode
2. Scripting mode
To execute a Python program in the scripting mode, the following steps are to be
followed:
• Write group of Python statements in any one of the editor like: Notepad,
Notepad++, editplus, nano, gedit, IDLE.
• Create and save the file with extension of “.py” or “.pyw”
After that we can execute the python file by using python IDE.