Python

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

UNIT - 2

PYTHON
Programming

 Describing list of Control


statements(Conditional and iterative),
if, if else, if..elif..else, Nested if statements, for
loop(range(), with else)
Python if Statement Syntax
if test expression: Example: Python if Statement
statement(s) # If the number is positive, we print an appropriate
message
num = 3
if num > 0:
print(num, "is a positive number.") print("This
is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.") print("This
is also always printed.")

   Flowchart of if statement
Syntax of if...else
if test expression:
Body of if
else: Example of if...else
Body of else # Program checks if the number is positive
or negative # And displays an appropriate
message
num = 3
# Try these two variations as well. # num =
-5 # num = 0
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")

Python if..else Flowchart


Syntax of if...elif...else
if test expression: Example of if...elif...else
Body of if 'In this program, we check if the number is
''
elif test expression: positive or negative or zero and display an
Body of elif appropriate message''‘
else:
num = 3.4
Body of else
# Try these two variations as well:
# num = 0
# num = -4.5
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

Flowchart of if...elif...else
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. They can get confusing, so they must be avoided unless necessary.

Python Nested if Example


'In this program, we input a number check if the number is positive or negative
''

or zero and display an appropriate message This time we use nested if


statement''‘
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
                                                     

What is for loop in Python?


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.
Syntax of for Loop
for val in sequence:
Body of for

Example: Python for Loop


#Program to find the sum of all numbers stored in a list #
List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print("The sum is", sum)

Flowchart of for Loop


The range() function
We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10
numbers).
We can also define the start, stop and step size as range(start, stop,step_size). step_size defaults to 1 if not provided.

print(range(10)) We can use the range() function in for loops to iterate through a


print(list(range(10))) sequence of numbers. It can be combined with the len() function
print(list(range(2, 8))) to iterate through a sequence using indexing. Here is an example.
print(list(range(2, 20, 3))) # Program to iterate through a list using indexing
genre = ['pop', 'rock', 'jazz']
# iterate over the list using index
for i in range(len(genre)):
print("I like", genre[i])
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.
The break keyword can be used to stop a for loop. In such cases, the else part is ignored.
Hence, a for loop's else part runs if no break occurs.

digits = [0, 1, 5]
for i in digits:
print(i)
else: program to display student's marks from record
print("No items left.") student_name = 'Soyuj'
marks = {'James': 90, 'Jules': 55, 'Arthur': 77}
for student in marks:
if student == student_name:
print(marks[student])
break
else:
print('No entry with that name found.')

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy