The document discusses Python control structures including if-else statements, indentation, and loops. It provides examples of if statements, if-else statements, nested if statements, and elif statements to test conditions and execute code blocks accordingly. It also discusses for loops and the range() function to iterate through sequences and generate number sequences to repeat code. Loops simplify problems by allowing code to repeat without rewriting and help traverse data structures.
2. Python If-else statements
? Decision making is the most important aspect of almost all the
programming languages. As the name implies, decision making
allows us to run a particular block of code for a particular decision.
Here, the decisions are made on the validity of the particular
conditions. Condition checking is the backbone of decision making.
? 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.
3. Statement Description
If Statement The if statement is used to test a
specific condition. If the condition
is true, a block of code (if-block)
will be executed.
If - else Statement The if-else statement is similar to
if statement except the fact that,
it also provides the block of the
code for the false case of the
condition to be checked. If the
condition provided in the if
statement is false, then the else
statement will be executed.
Nested if Statement Nested if statements enable us to
use if ? else statement inside an
outer if statement.
4. The if statement
The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if-block. The condition of if statement can be any valid logical
expression which can be either evaluated to true or false.
Syntax
if expression:
statement
Example 1
num = int(input("enter the number?"))
if num%2 == 0:
print("Number is even")
Output:
enter the number?
10 Number is even
5. Program to print the largest of the three numbers
a = int(input("Enter a? "));
b = int(input("Enter b? "));
c = int(input("Enter c? "));
if a>b and a>c:
print("a is largest");
if b>a and b>c:
print("b is largest");
if c>a and c>b:
print("c is largest");
Output:
Enter a? 100 Enter b? 120 Enter c? 130 c is largest
6. Using Else
Program to check whether a person is eligible to
vote or not.
age = int (input("Enter your age? "))
if age>=18:
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
Output:
? Enter your age? 90 You are eligible to vote !!
7. The elif statement
? The elif statement enables us to check multiple conditions and execute the specific block of
statements depending upon the true condition among them. We can have any number of elif
statements in our program depending upon our need. However, using elif is optional.
The syntax of the elif statement is given below.
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
8. Example
marks = int(input("Enter the marks? "))
if marks > 85 and marks <= 100:
print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
9. Python Loops
? The flow of the programs written in any programming language is sequential by
default. Sometimes we may need to alter the flow of the program. The execution of a
specific code may need to be repeated several numbers of times.
Why we use loops in python?
? The looping simplifies the complex problems into the easy ones. It enables us to alter
the flow of the program so that instead of writing the same code again and again, we
can repeat the same code for a finite number of times.
For example, if we need to print the first 10 natural numbers then, instead of using
the print statement 10 times, we can print inside a loop which runs up to 10 iterations.
Advantages of loops
There are the following advantages of loops in Python.
It provides code re-usability.
Using loops, we do not need to write the same code again and again.
Using loops, we can traverse over the elements of data structures (array or linked lists).
10. Loops
For Loop
The for loop in Python is used to iterate the statements or a part of the program several times. It
is frequently used to traverse the data structures like list, tuple, or dictionary.
The syntax of for loop in python is given below.
for iterating_var in sequence:
statement(s)
Iterating string using for loop
str = "Python"
for i in str:
print(i)
11. Output
P
Y
T
H
O
N
Program to print the sum of the given list.
list = [10,30,23,43,65,12]
sum = 0
for i in list:
sum = sum+i
print("The sum is:",sum)
Output:
The sum is: 183
12. For loop Using range() function
The range() function
The range() function is used to generate the sequence of the
numbers. If we pass the range(10), it will generate the numbers from 0 to
9. The syntax of the range() function is given below.
Syntax:
range(start,stop,step size)
? The start represents the beginning of the iteration.
? The stop represents that the loop will iterate till stop-1. The range(1,5) will
generate numbers 1 to 4 iterations. It is optional.
? The step size is used to skip the specific numbers from the iteration. It is
optional to use. By default, the step size is 1. It is optional.
13. Program to print numbers in sequence.
for i in range(10):
print(i,end = ' ')
Output:
0 1 2 3 4 5 6 7 8 9
What does end do:
The print() function inserts a new line at the end, by
default. In Python 2, it can be suppressed by putting ','
at the end. In Python 3, "end =' '" appends space
instead of newline.
14. Program to print table of given number.
n = int(input("Enter the number "))
for i in range(1,11):
c = n*i
print(n,"*",i,"=",c)
Output:
Enter the number 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100