KEMBAR78
If, If-Else, If-Elif-Else Statements, For and While Loop | PDF | Control Flow | Computing
0% found this document useful (0 votes)
10 views17 pages

If, If-Else, If-Elif-Else Statements, For and While Loop

If elif statements
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

If, If-Else, If-Elif-Else Statements, For and While Loop

If elif statements
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

TYPES OF STATEMENTS

Statements are the instructions written in the program (source code) and are given to the
computer for executing any task.

1. Empty statement
Ex: pass statement

2. Single statement
Ex: x=10

3. Compound statement: It is a statement which comprises a group of statements.

FLOW OF CONTROL:

The order of execution of the statements in a program is known as flow of control.

In Python flow of control can be:


• Sequential: Statements in the program are executed in sequential order or in linear
fashion, one after another. Default flow of statements is sequential.
Statement 1 ---→ Statement 2 ---→ Statement 3
• Selection/Conditional: When the program uses a condition to decide which set of
statements is to be executed.
If condition is true, statement 2 will execute
If condition is false, statement 3 will execute

1. Statement1 -> Statement 2 (True)


2. Statement 1 -> Statement 3 (False)

• Iteration/Looping: When a certain set of statements needs to be executed multiple times.

The flow of control can be implemented using control structures/statements.


Python supports two types of control structures/statements:
• Selection/Conditional, also known as decision-making constructs.
if statement
if-else statement
if-elif-else statement
• Repetition/Iteration, also known as Loop
for loop
while loop

PYTHON SELECTION STATEMENTS/ Decision making constructs


The selection construct means the execution of statements depending upon a condition test. If a
condition evaluates true, a course of action (a set of statements) is followed otherwise another
course of action (a different set of statements) is followed.

IF STATEMENT:
This is the simplest decision-making statement in Python. It is used to decide if a particular block of
code needs to be executed or not. Whenever the given condition is True, then the block of code inside
it gets executed, else it does not.

THE SYNTAX FOR IF STATEMENT:


An if statement should be preceded by the keyword if and ended with a colon :. The condition of
an if statement can be written with or without round brackets.

Indentation – 4 spaces

Syntax:
if condition:

body of if
EXAMPLE:
a = 330
b = 200
if b > a:
print("b is greater than a")
EXPLANATION:
In this example we use two variables, a and b, which are used as part of the if statement to test
whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so
we print to screen that "b is greater than a".

INDENTATION:

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.
Other programming languages often use curly-brackets for this purpose.

EXAMPLE:

if statement, without indentation (will raise an error):

a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error

IF-ELSE STATEMENT:
The statements written within the else block get executed whenever the if condition is False.

THE SYNTAX FOR IF ELSE


STATEMENT:
else statement is preceded by the keyword 'else' and it also ends with a colon :. Unlike the if, the
else statement will not have any conditional statements.

if condition:
body of if

else:
body of else
if- else in Python Examples:
#program to check if a num1 is less than num2 num1, num2 = 6, 5
if (num1 < num2):
print("num1 is less than num2")

else:
print("num2 is less than num1")

Output: num2 is lesser than num1

EXPLANATION: In the above-given code, the if condition is False and hence the control shifts to
the else block. Hence, the statement written within the else block gets printed.

Example: if-else statement

if age>18:

print(“You are eligible for voting”)

else:

print(“You are not eligible for voting”)

Programs

Prog. 1:
Prog. 2:

# Program to check entered number by user is even or odd.

n=int(input('Enter a number '))


if n%2==0:
print('Number is even')
else:
print('Number is odd')

if-elif-else statement:

Syntax:

if condition:
body of if

elif condition:

body of elif

elif condition:

body of elif

else:
body of else
Prog. 2:

Prog. 3:
Complete the code for multiplication, division and modulus/remainder.

Iterative Statements/Loop

used to repeat a set of statements several times, and the process is known as iteration/repetition.

Loops in Python

1. for loop

2. while loop

for loop
• known as a counting loop.
• Also known as a definite loop, because statements are executed for a definite number of
times.

Syntax:

for <loop counter/variable> in <sequence/elements in range>:


statements
Example:

for i in range(1,6):
print(i)

Example:

WAP to print numbers from 1 to 10 using for loop.

for j in [1,2,3,4,5,6,7,8,9,10]:
print(j)

range() function- It is used to generate a sequence of numbers.


commonly used in loops.
built-in function of Python.

Syntax: range([start],stop,[step])
start- integer starting from which the sequence of integers is to be returned, and optional, by default
its value is 0.
stop - integer before which the sequence of integers is to be returned. The range of integers ends at
stop – 1 and is compulsory.
step - an integer value which determines the increment between each integer in the sequence.
optional, by default its value is 1.

Example:
range(10)
0,1,2,3,4,5,6,7,8,9
range(8)
0,1,2,3,4,5,6,7
range(6)
0,1,2,3,4,5
range(1,10)
1,2,3,4,5,6,7,8,9
range(1,10,2)
1,3,5,7,9
range(10,1,-1)
10,9,8,7,6,5,4,3,2
range(10,-1,-1)
10,9,8,7,6,5,4,3,2,1,0
Prog:
WAP to print numbers from 10 to 1 using for loop.

for i in range(1,11):
print(i)

Prog:
WAP to print numbers from 10 to 1 using for loop.

for i in range(10,0,-1):
print(i)

Prog. :
WAP to print even numbers from 0 to 10 using for loop.

for i in range(0,11,2):
print(i)

Prog. :
WAP to print odd numbers from 1 to 10 using for loop.

for i in range(1,10,2):
print(i)

Prog. :

WAP to print Hello message 20 times on the screen using for loop.

for i in range(20): # 0,1,2,...........19


print('Hello')

Prog. :

WAP to print sum of first n numbers using for loop.

n=int(input("Enter the number upto which you want sum:"))


sum1=0
for i in range(1,n+1): # 1,2,3,4,5
sum1=sum1+i

print("Sum of first",n,"numbers:",sum1)
Output:

Enter the number upto which you want sum:5


Sum of first 5 numbers: 15

Prog. :

WAP to print sum of first any n numbers using for loop.

n=int(input("Enter how many numbers you want to add:"))


sum1=0
for i in range(1,n+1):
num=int(input('Enter any number'))
sum1=sum1+num

print("Sum of any", n,"numbers:",sum1)

Output:

Enter how many numbers you want to add:3


Enter any number12
Enter any number45
Enter any number32
Sum of any 3 numbers: 89

Prog. :

WAP to print multiplication table of a number.

n=int(input('Enter a number whose table you want to print'))


for i in range(1,11):
table=n*i
print(n,'*',i,'=',table)

Output:
Enter a number whose table you want to print5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

while loop

• known as conditional loop.


• Also known as indefinite loop, because statements are executed until a logical condition
remains true.

Syntax:

while <condition>:
body of while loop
else: #else is optional
body of else

Prog.:
WAP to print numbers from 1 to 10 using for/while loop.

i=1
while i<=10:
print(i)
i=i+1 # i+=1

for i in range(1,11,1): #1, 2,…..10


print(i)

Prog:
WAP to print even numbers from 0 to 10 using for/while loop.

0,2,4,6,8,10

for i in range(0,11,2):
print(i)

i=0
while i<=10: # while i<11
print(i)
i=i+2 # i+=2

Prog:
WAP to print odd numbers from 1 to 10 using for loop.
1,3,5,7,9

for i in range(1,10,2):
print(i)
else:
print(‘Out of the loop’)

i=1
while i<=10:
print(i)
i=i+2

Using the else statement with loops


• If the else statement is used with a for loop, the else statement is executed when the loop
has exhausted iterating the list.

• If the else statement is used with a while loop, the else statement is executed when the
condition becomes false.

Use of else with for loop and while loop

print('Use of else with for loop')


for ch in 'Goa':
print(ch)
else: # optional
print('Out of loop')

print('Use of else with while loop')


i=1
s=0
while i<=10:
s=s+1
i=i+1
else:
print('Sum',s)
Infinite loop

A loop becomes an infinite loop if a condition never becomes false. Such a loop never ends.

Ex: When i=i+1 is not provided in a while loop.

pass statement

It is used when a statement is required syntactically, but you do not want any command or code to
execute.

The pass statement is a null operation; nothing happens when it executes.

The pass statement is used as a placeholder for future code.


When the pass statement is executed, nothing happens, but you avoid getting an error when empty
code is not allowed.
Empty code is not allowed in loops or in if statements.

Example:

for I in range(1,11):

pass

WAP to check whether an entered number is a prime no. or a composite number.

Prime number: 2,3,5,7,11,13


Numbers which are not prime, they are composite numbers.

Code:

n=int(input("Enter any number:"))


flag=0

for i in range(2,n):
if n%i==0:
flag=1
break # it is a statement that is used inside a loop. And is used to terminate the loop.
if flag==0:
print('Prime Number')
else:
print('Composite Number')
WAP TO PRINT FACTORIAL OF A GIVEN NUMBER.

FACTORIAL
Example:
N=6

6*5*4*3*2*1

Code:
n=int(input('ENTER THE NUMBER WHOSE FACTORIAL YOU WANT TO PRINT:'))
fact=1
for i in range(1,n+1):
fact=fact*i

print('Factorial of',n,'is:',fact)

WAP TO PRINT N TERMS OF FIBONACCI SERIES.

FIBONACCI SERIES
0 1 1 2 3 5 8 13

Code:
n=int(input('ENTER THE NUMBER OF TERMS YOU WANT TO PRINT:'))
first=-1
second=1
for i in range(1,n+1):
third=first+second
print(third,end=' ')
first,second=second,third

Prog.
WAP to print GCD of two numbers.

GCD/HCF- Greatest Common Divisor/Highest Common Factor

GCD
Example:

12-2,2,3
24-2,2,2,3

GCD-2*2*3=12
Code:
num1=int(input('Enter first number:'))
num2=int(input('Enter second number'))

while(num2!=0):
temp=num2
num2=num1%num2
num1=temp

gcd=num1
print('GCD:',gcd)

Prog.
WAP to print LCM of two numbers.

Code:

#smallest positive integer that is divisible by both numbers.


num1=int(input('Enter first number'))
num2=int(input('Enter second number'))
a=num1
b=num2

while(num2!=0):
temp=num2
num2=num1%num2
num1=temp
gcd=num1
lcm=(a*b)//gcd
print('LCM is:',lcm)

WAP to determine whether a number is a palindrome number or not.


A palindrome number is a number that is same after reverse.

Example: 121, 323 etc.

num=int(input('Enter a number:'))
n=num
rev=0
while num!=0:
temp=num%10
rev=rev*10+temp
num=num//10
if n==rev:
print('Palindrome Number')
else:
print('Not a Palindrome number')

WAP to determine whether a number is a perfect number or not.

Any number can be perfect number in Python, if the sum of its positive divisors excluding the
number itself is equal to that number.

For example, 6 is a perfect number in Python because 6 is divisible by 1, 2, 3 and 6. So, the sum of
these values are: 1+2+3 = 6

n = int(input("Enter any number: "))


sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")

WAP to determine whether a number is an armstrong number or not.

An Armstrong number of three digits is a number such that the sum of the cubes of its digits is
equal to the number itself.

371= 3**3+7**3+1**3=371.

153=1**3+5**3+3**3= 153.

152=1**3+5**3+2**3=134.

num=int(input('Enter a number:'))
n=num
arm=0
while num!=0:
temp=num%10
arm=arm+(temp**3)
num=num//10
if n==arm:
print('Armstrong Number')
else:
print('Not an Armstrong number')

WAP to print sum of digits of a number.

234=2+3+4=9

num=int(input('Enter a number:'))
n=num
s=0
while num!=0:
temp=num%10
s=s+temp
num=num//10
print('Sum of digits of',n,':',s)

You might also like