0% found this document useful (0 votes)
336 views8 pages

If - Elif - Else Assignment

It gives information on if-else-elif statements and also gives information about the for and while statements

Uploaded by

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

If - Elif - Else Assignment

It gives information on if-else-elif statements and also gives information about the for and while statements

Uploaded by

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

while loop with else

Same as that of for loop, we can have an optional else block with while loop as
well.

The else part is executed if the condition in the while loop evaluates to False.

The while loop can be terminated with a break statement. In such case,
the else part is ignored. Hence, a while loop's else part runs if no break occurs and
the condition is false.

Here is an example to illustrate this.

counter = 0

while counter < 3:

print("Inside loop")

counter = counter + 1

else:

print("Inside else")

for loop with else


A for loop can have an optional else block as well. The else part is executed if the items
in the sequence used in for loop exhausts.

break statement can be used to stop a for loop. In such case, the else part is ignored.

Hence, a for loop's else part runs if no break occurs.

Here is an example to illustrate this.

digits = [0, 1, 5]
for i in digits:

print(i)

else:

print("No items left.")

Python Nested if statements


We can have a if...elif...else statement inside
another if...elif...else statement. This is called nesting in computer
programming.

Any number of these statements can be nested inside one another.


Indentation is the only way to figure out the level of nesting. This can get
confusing, so must be avoided if we can.

Python Nested if Example


1. num = float(input("Enter a number: "))
2. if num >= 0:
3. if num == 0:
4. print("Zero")
5. else:
6. print("Positive number")
7. else:
8. print("Negative number")
Flowchart of if...elif...else
.

The elif is short for else if. It allows us to check for multiple expressions.

If the condition for if is False, it checks the condition of the next elif block and so
on.
If all the conditions are False, body of else is executed.

Only one block among the several if...elif...else blocks is executed according to the
condition.

The if block can have only one else block. But it can have multiple elif blocks.

Syntax of if...elif...else

if test expression:

Body of if

elif test expression:

Body of elif

else:

Body of else

What is pass statement in Python?


In Python programming, pass is a null statement. The difference between
a comment and pass statement in Python is that, while the interpreter ignores
a comment entirely, pass is not ignored.

However, nothing happens when pass is executed. It results into no operation


(NOP).

Syntax of pass
pass

sequence = {'p', 'a', 's', 's'}

for val in sequence:

pass

The infinite loop


We can create an infinite loop using while statement. If the condition of while
loop is always True, we get an infinite loop.

Infinite loop using while

1. # An example of infinite loop


2. # press Ctrl + c to exit from the loop
3.
4. while True:
5. num = int(input("Enter an integer: "))
6. print("The double of",num,"is",2 * num)
Python Program to Remove Punctuations From a String

Sometimes, we may wish to break a sentence into a list of words.

In such cases, we may first want to clean up the string and remove all the
punctuation marks. Here is an example of how it is done.

Ex:

# define punctuation

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user

# my_str = input("Enter a string: ")

# remove punctuation from the string

no_punct = ""

for char in my_str:

if char not in punctuations:

no_punct = no_punct + char

# display the unpunctuated string

print(no_punct)
Ex:

Python program to display calendar of given month of the year

# import module
import calendar

yy = 2014
mm = 11

# To ask month and year from the user


# yy = int(input("Enter year: "))
# mm = int(input("Enter month: "))

# display the calendar


print(calendar.month(yy, mm))
Ex:
# Python Program to find numbers divisible by thirteen from a list using
anonymous function

# Take a list of numbers


my_list = [12, 65, 54, 39, 102, 339, 221,]

# use anonymous function to filter


result = list(filter(lambda x: (x % 13 == 0), my_list))

# display the result


print("Numbers divisible by 13 are",result)

EX:
Find ASCII Value of Character

c = input("Enter a character: ")

print("The ASCII value of '" + c + "' is",ord(c))

Reference:
https://www.programiz.com/python-programming/looping-technique

You might also like