Python Notes1
Python Notes1
Conditional execution
In order to write useful programs, we almost always need the ability to check conditions and
change the behaviour of the program accordingly. Conditional statements give us this ability.
if statement
The if statement is used to test a particular condition and if the condition is true, it executes a
block of code known as if-block .The condition of if statement can be any valid logical expression
which can be either evaluated to true or false.
if (condition):
#statement1
#extra statements....
#statement2
Example:-
i = 10
if (i> 15):
print("10 is less than 15")
print("I am not in if")
I am not in if
As the condition present in the if statement is false. So, the block below the if statement is not
executed.
if-else
The if-else statement provides an else block combined with the if statement which is executed
in the false case of the condition.
If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.
Python Programming Unit-2 Notes
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
i = 20
if (i< 15):
print("I is smaller than 15")
print("I'm in if Block")
else:
print("I is greater than 15")
print("I'm in else Block")
print("I'm not in if and not in else Block")
I is greater than 15
I'm in else Block
I'm not in if and not in else Block
The block of code following the else statement is executed as the condition present in the if statement
is false after calling the statement which is not in block (without spaces).
Nested-if
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
Python Programming Unit-2 Notes
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i< 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
i is smaller than 15
i is smaller than 12 too
If-elif-else ladder
The if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is
bypassed. If none of the conditions is true, then the final else statement will be executed.
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
i = 15
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
else:
print("i is not present")
i is 15
Python Programming Unit-2 Notes
Looping statements:
In general, statements are executed sequentially: The first statement in a function is executed
first, followed by the second, and so on. There may be a situation when you need to execute a block
of code several number of times.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times.
while loop
In python, while loop is used to execute a block of statements repeatedly until a given a condition is
satisfied. And when the condition becomes false, the line immediately after the loop in program is
executed.
while (expression) :
statement(s)
All the statements indented by the same number of character spaces after a programming construct
are considered to be part of a single block of code. Python uses indentation as its method of grouping
statements.
Hello World
Hello World
Hello World
As discussed above, while loop executes the block until a condition is satisfied. When the condition
becomes false, the statement immediately after the loop is executed.
The else clause is only executed when your while condition becomes false. If you break out of the
loop, or if an exception is raised, it won’t be executed.
Hello World
In Else Block
Python Programming Unit-2 Notes
for loop
For loops are used for sequential traversal. For example: traversing a list or string or array etc.
In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). It can be used to iterate over a range
and iterators.
range() function
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1
(by default), and stops before a specified number.
Parameter Description
n=4
for i in range(0, n):
print(i)
0
1
2
3
0 3 6 9 12 15 18 21 24 27
If a user wants to decrement, then the user needs steps to be a negative number. For example:
Python Programming Unit-2 Notes
25 23 21 19 17 15 13 11 9 7 5 3
ABC
BCD
CDE
Inside Else Block
Nested loops
Python programming language allows to use one loop inside another loop.
1
22
333
4444
Python Programming Unit-2 Notes
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : n
Current Letter : t
Pass Statement: We use pass statement to write empty loops. Pass is also used for empty control
statement, function and classes.
# An empty loop
Last Letter : n
Python Programming Unit-2 Notes
Functions
Python Functions is a block of related statements designed to perform a computational,
logical, or evaluative task. The idea is to put some commonly or repeatedly done tasks together and
make a function so that instead of writing the same code again and again for different inputs, we can
do the function calls to reuse code contained in it over and over again.
Functions can be either built-in or user-defined. It helps the program to be concise, non-repetitive,
and organized.
deffunction_name(parameters):
statement(s)
return expression
Creating a function
We can create a Python function using the def keyword.
def fun():
print("Python Programming")
Calling a function
After creating a function we can call it by using the name of the function followed by
parenthesis containing parameters of that particular function.
def fun():
print("Python Programming")
Python Programming
Python Programming Unit-2 Notes
Types of Arguments
Python supports various types of arguments that can be passed at the time of the function call.
Let’s discuss each type in detail.
Default arguments
A default argument is a parameter that assumes a default value if a value is not provided in the
function call for that argument. The following example illustrates Default arguments.
x: 10
y: 50
Keyword arguments
The idea is to allow the caller to specify the argument name with values so that caller does not need
to remember the order of parameters.
# Keyword arguments
student(firstArg='Python', lastArg ='Practice')
student(lastArg ='Practice', firstArg='Python')
Python Practice
Python Practice