Python (Part 2)
Python (Part 2)
(Part-2)
• Decision making:
– if statement
– if...else statement
– if…elif…else statement
The if statement
• It is used to execute one or more statement
depending upon whether condition is true or
not.
• Syntax:-
num=1
if condition: if num==1:
statements print(‘one’)
Indentation
• In Python, the body of the if statement is indicated by the
indentation. Body starts with an indentation and the first
unindented line marks the end.
• It refers to spaces that are used in the beginning of a statement.
The statements with same indentation belong to same group called
a suite.
• By default, Python uses 4 spaces but it can be increased or
decreased by the programmers.
if x==1:
print(‘a’)
print(‘b’)
If y==2:
print(‘c’)
print(‘d’)
print(‘end’)
print (‘’end)
The if…else Statement
• The if..else statement evaluates test expression and
will execute body of if only when test condition is
True. If the condition is False, body of else is
executed. Indentation is used to separate the blocks.
• Syntax:- Example:
If condition: num = 3
Satement1 if num >= 0:
print("Positive or Zero")
else: else:
Statement2 print("Negative number")
if...elif...else Statement
if condition1: Example:
Statement1 num = 3.4
if num > 0:
elif condition2: print("Positive number")
Statement2 elif num == 0:
print("Zero")
elif condition3: else:
Statement3 print("Negative number")
else:
Body of else
Problems
• WAP to input the number and check it is even or odd.
• WAP to check a number is positive or negative.
• WAP to check greatest among two numbers.
• WAP to check a year leap year or not.
• WAP to check greatest among three numbers.
• WAP to check a three digit number is palindrome or not.
• WAP to input the cost price and selling price of an item
and check for profit or loss. Also calculate it.
The While loop
• The while loop is used to iterate over a block of code as
long as the test expression (condition) is true.
• Syntax:
while condition:
Body of while
• Working:
In while loop, test expression is checked first. The body
of the loop is entered only if the condition evaluates to
True. After one iteration, the test expression is checked
again. This process continues until the condition
evaluates to False.
• The body of the while loop is determined through
indentation.
Flow Chart: Example:
Output:
while loop with else
(val is the variable that takes the value of the item inside the
sequence on each iteration)
• Loop continues until we reach the last item in the sequence.
The body of for loop is separated from the rest of the code
using indentation.
Flow Chart:
Example:
Output:
range() function
Example: Output:
for loop with else
Ex:
for i in range(10):
if i%2==0:
pass
else:
print(i)
print(‘end of loop’)
Python List
• In Python programming, a list is created by placing all the
items (elements) inside a square bracket [ ], separated by
commas.
• It can have any number of items and they may be of different
types (integer, float, string etc.).
• # empty list
my_list = []
• # list of integers
my_list = [1, 2, 3]
• # list with mixed datatypes
my_list = [1, "Hello", 3.4]
• # nested list
my_list = ["mouse", [8, 4, 6], ['a']]
Accessing List Elements?
• List Index
The for loop in Python is used to iterate over a sequence (list, tuple, string) or
other iterable objects. Iterating over a sequence is called traversal.
Here, val is the variable that takes the value of the item
inside the sequence on each iteration.
The len(a) function returns the number of elements in the array ‘a’ into n.
Type Codes to Create Array
Example: Output:
#Accessing array elements
#Slicing Arrays
Change or add elements in the array
We can add one item to a list using append() method or add several items
using extend() method.
Insert at particular location
Example:
Output:
Remove/delete elements
We can delete one or more items from an array using Python's del statement.
Example: Output:
We can use the remove() method to remove the given item, and pop() method to
remove an item at the given index.
Problems: