Flow Of Execution
1. Sequential Statements
2. Selection /Conditional Statements
3. Iteration or Looping constructs
1.Sequential Statements:
In Python, sequential statements refer to the instructions that are executed one after another, in the order they appear in the code.
i. e., From the first statement till the last statement. With out any jump
print("Hello!")
print("Welcome to Python.")
In the above code, the first print
statement is executed first, followed by the second. This is how sequential execution works — each line runs step by step, from top to bottom.
This concept is important because it forms the foundation of how Python code is executed, ensuring that your instructions run in the correct order.
2. Selection /Conditional Statements
In Python, selection statements (also known as conditional statements) allow us to execute different actions based on conditions. These conditions check if something is True or False and then decide what code to run.
4 types:
1. if statement
2. else statements
3. elif statements
4. Nested if statements
2.1 if statement
The if
statement is used to test a condition. If the condition is True, the code inside the if
block is executed. If the condition is False, the code inside the if
block is skipped.
Syntax:
if condition:
# code to execute if condition is True
Program 1:
age = 20
if age >= 18:
print("You are eligible to vote.")
Explanation:
- In the above program, we are checking if
age
is greater than or equal to 18.
- Since the condition
age >= 18
is True (because age is 20), the program will print "You are eligible to vote."
Key Points:
- The
if
statement checks the condition, and if it is True, the code inside it runs. - If the condition is False, nothing happens (the code inside the
if
block is skipped).
2.2 if … else statement
The if...else
statement is used to check a condition, and based on whether the condition is True or False, the program will execute one block of code or another.
- If the condition is True, the code inside the
if
block will execute.
- If the condition is False, the code inside the
else
block will execute.
Syntax
if condition:
# code to execute if condition is True
else:
# code to execute if condition is False
Program 1:
age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
- In this example, the condition checks if
age
is greater than or equal to 18.
- Since
age = 16
, the conditionage >= 18
is False.
- As a result, the program will execute the code in the
else
block and print "You are not eligible to vote."
- The
if
block runs if the condition is True.
- The
else
block runs if the condition is False.
2.3 if.. elif… else Statement
Syntax
if condition:
Statements
elif condition:
Statements
else:
Statements
Eg1:
a=int(input("enter a Number"))
if a>0:
print("Number is Positive")
elif a<0:
print("Number is Negative")
else:
print("Number is Zero")
2.4 Nested if
Syntax:
if condition:
Statements
if condition:
Statements
elif condition:
Statements
else:
Statements
elif Condition:
Statements
else:
Statements
Eg1:
mark=int(input("Enter Your Mark"))
if mark>=80:
print("First Class")
if mark>=90:
print("Rank Holder ")
elif mark<=70 and mark>=36:
print("Second Class")
else:
print("Fail")
Area of Circle:3.14*r*r (or) 3.14*r ** 2
Program:
radius=int(input(“Enter the values of radius”))
circle_area=3.14*radius*radius
print(“Area of the circle is :”,circle_area )
3.Iteration/LOOP
1. for loop
Syntax:
for <Variable_name> in <Sequences/items in range>:
Body of for loop
else: #optional
Body of else
Range:
Syntax
range(start,stop,step)
Stop=stop-1
Command | Output |
range(10) | [0,1,2,3,4,5,6,7,8,9] |
range(1,11) | [1,2,3,4,5,6,7,8,9,10] |
range(5) | [0,1,2,3,4] |
range(3,7) | [3,4,5,6] |
range(0,30,5) | [0,5,10,15,20,25] |
range(2,25,3) | [2,5,8,11,14,17,20,23] |
range(0,-9,-1) | [0,-1,-2,-3,-4,-5,-6,-7,-8] |
range(0,-9,-2) | [0,-2,-4,-6,-8] |
(bckd) -9,-8,-7,-6,-5,-4,-3,-2-1,0,1,2,3,4 (frwd)
for i in range(10):
print(i)
#c Language
for(i=0;i>10;i++)
#python
for i in range(10):
print(i)
2. while loop
Syntax
while <test_expression>:
Body of while
else: #optional
Body of else
For example:
for (i=0,i>=10,i++)
Count=0
for count in count<=5:
print(count)
count+=1
count=0
while count<=5:
print(count)
count+=1
Nested Loops:
1. nested for
Syntax:
for <variable_name> in <sequences/items in range>:
Body of the statement
for <variable_name> in <sequences/items in range>:
Body of the for loop
2. Nested while
Syntax:
while <text_expression>:
Body of the statement
while <text_expression>:
Body of the statement
Sorting Condition:
Sometime the conditions being used in code are complex and repetitive.In such cases to make your program more readable,you can use named conditions you can store conditions in a name and then use that named conditional in the if statements
b,c=2,3
all=a==1 and b==2 and c==3
if all:
print(“Condition Fulfilled”)
if all:
print(“Condition fulfilled again”)
1. Write a python program to generate the table of a number using for loop.
2. Write a python program to find factorial number.
3. Write a python concept of nested for loop statement
Output
1
1 2
1 2 3
0 Comments