0% found this document useful (0 votes)
4 views21 pages

CONTROL STATEMENTS If and For Loop

The document explains Python nested if statements and for loops, detailing their syntax and usage. It includes examples of how to implement these structures in various programming tasks, such as checking input ranges and calculating factorials. Additionally, it covers the use of the range function and control statements like continue and break within for loops.

Uploaded by

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

CONTROL STATEMENTS If and For Loop

The document explains Python nested if statements and for loops, detailing their syntax and usage. It includes examples of how to implement these structures in various programming tasks, such as checking input ranges and calculating factorials. Additionally, it covers the use of the range function and control statements like continue and break within for loops.

Uploaded by

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

CONTROL

STATEMENT
S
Python Nested if
statements
Python Nested if statements
• We can have an 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 it
can be.
Syntax : If …elif…else
if [boolean expression]:
[statements]
elif [boolean expresion]:
[statements]
elif [boolean expresion]:
[statements]
else:
[statements]
WAP program to check the range of the input numbers from the
user
WAP TO FIND THE AMOUNT OF PURCHASE OF
GOODS
WAP TO FIND THE SHIPPING COST OF ONLINE ITEMS TO US AND
AU
FOR

LOOP
A for loop is used for iterating over a sequence (that
is either a list, a tuple, a dictionary, a set, or a
string).
• This is less like the for keyword in other
programming languages, and works more like an
iterator method as found in other object-orientated
programming languages.
• With the for loop we can execute a set of
statements, once for each item in a list, tuple, set
etc.
FOR LOOP SYNTAX AND FLOWCHART

for x in sequence:
statement1
statement2
...
statementN
Examples of Python For
Loop
1.For Loop with List

This code uses a for loop to iterate over a list of strings, printing each item in the list
on a new line. The loop assigns each item to the variable I and continues until all
items in the list have been processed.
2. For Loop with String

This code uses a for loop to iterate over a string and print each character on a new
line. The loop assigns each character to the variable i and continues until all
characters in the string have been processed.
3. For Loop with a step size

This code uses a for loop in conjunction with the range() function to
generate a sequence of numbers starting from 0, up to (but not including)
10, and with a step size of 2. For each number in the sequence, the loop
prints its value using the print() function. The output will show the numbers
0, 2, 4, 6, and 8.
5. For Loop in Python with Range Statement
The Python range() function is used to generate a sequence of numbers. Depending on
how many arguments the user is passing to the function, the user can decide where that
series of numbers will begin and end as well as how big the difference will be between one
number and the next.range() takes mainly three arguments.
•start: integer starting from which the sequence of integers is to be returned
•stop: integer before which the sequence of integers is to be returned.
The range of integers ends at a stop – 1.
•step: integer value which determines the increment between each integer in the
sequence.
WAP to print and find the sum of numbers input from the
user
6. For Loop in Python with Continue Statement

Python continue Statement returns the control to the beginning of the loop.
7. For Loop in Python with Break Statement
8. For Loop in Python with Else Statement
WAP TO FIND THE FACTORIAL OF A NUMBER GIVEN BY
USER
WAP to find the power of a given base and exponent.
The input of base and exponent sould be given by user
WAP to Iterate numbers in reverse order.
WAP to calculate the cube of all numbers from 1 to a
given number by user
Write a program to add the elements of the two
different lists

You might also like