In Python-25-35
In Python-25-35
Conditional statement-
a. if statement
b. if- else statement
c. nested-if statement
d. Elif statement
if condition:
# Statements to execute if
# condition is true
age = 18
if age >= 18
print("You are eligible to vote.")
If-else statement
In if else statement if the condition is True, one set of statement are executed. If it is
False, another set of statement are executed. The syntax is shown.
Syntax:
If expression
Statement 1
Else:
Statement 2
For example:
num = 5
if (num > 10)
else
Nested-if statement
A nested if statement is an if statement placed inside another if statement. This
allows for more complex decision-making by evaluating additional conditions based
on the outcome of previous conditions.
Syntax:
if test expression 1
# executes when condition 1 is true
body of if statement
if test expression 2
# executes when condition 2 is true
Body of nested-if else
body of nested-if
Else
body of if-else statement
Example
age = 20
citizenship = "USA"
if citizenship == "USA":
else
else
For example:
a = 20
if (a == 20) :
# First if statement
if (a < 25) :
print (“a is smaller than 25”)
else
print (“a is greater than 25”)
else
print (“a is not equal to 20”)
Output: a is smaller than 25
elif statement
An elif statement in programming is used to check multiple conditions in an if
statement. It stands for "else if" and allows you to specify additional conditions if the
initial if condition evaluates to False.
Syntax:
if expression 1:
Statement 1
elif expression 2:
…
…
else:
Statement
Example
x = 10
if x > 10:
print("x is greater than 10")
elif x == 10:
print("x is equal to 10")
else:
print("x is less than 10")
Q1. Write a Python program to check if the input year is a leap year or not.
year = int (input (“Enter year :”))
if (year % 4) == 0 :
if (year % 100) == 0 :
if (year % 400) == 0 :
print (year, ‘is leap year’)
else :
print (year, ‘is not leap year’)
else :
print (year, ‘is leap year’)
else :
print (year, ‘is leap year’)
Output :
Enter a year: 2016
1985 is not leap year
Q3.Write a program to find whether a number is even or odd.
# Get user input
number = int(input("Enter a number: "))
Define loop
A loop is a programming construct that allows a set of instructions to be executed
repeatedly based on a condition. Loops are fundamental in programming because
they enable automation of repetitive tasks without the need to write the same code
multiple times.
Purpose: The purpose of loops is to repeat the same, or similar, code a number of
times. This number of times could be specified to a certain number, or the number
of times could be dictated by a certain condition being met.
While Statements-
While is very simple method of implementing iterations or loops. If a set of
statement needs to be executed again and again, multiple numbers of times, it is
called iterations.
Syntax;
while expression:
statement(s)
Example:
N= int(input(“enter n: “))
i=1
print(“printing number upto”, n
while (i)
i= i+1
output enter n: 5
1
2
3
4
5
Example 2
i=1
while i <= 5:
print(i)
i += 1
For loop:
Similar to while loop, the body of loop is executed many times or iterated. In
the first in executing for loop, the loop variable is assigned with the first
values of the list specified with list_name. After that the body of the loop is
executed.
A for loop in Python is a control flow statement that is used to repeatedly
execute a group of statements as long as the condition is satisfied.
Syntax-
for variable name inlist_ name
body of loop
Example 1-
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
Output-
apple
banana
cherry
Example 2-
i=2
for 1 in ‘welcome’:
print(i,’ letter :’,1)
i+1=1
Output-
1. Letter : w
2. Letter : e
3. Letter : l
4. Letter : c
5. Letter : o
6. Letter : m
7. Letter : e
Nested loop
Break statement:
The break statement is used to exit from the loop immediately and transfer the
control execution to the statement just after the loop.
Syntax:
for variable in iterable:
# code to be executed
if condition:
break
Example:
fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
for fruit in fruits:
if fruit == 'date':
break
print(fruit)
Output:
apple
banana
cherry
How it Works-
Here's a step-by-step breakdown of how the break statement works:
1. The loop iterates over the sequence, executing the code inside the loop.
2. When the break statement is encountered, the loop is terminated
immediately.
3. The program continues executing the code after the loop.
Continue statement-
The continue statement is used to execute from the start of the loop, therefore
skipping the execution of the remaining part of the loop.
The continue statement in Python is used to skip the current iteration of a loop and
move on to the next iteration. When a statement is encountered inside a loop, the
current iteration is terminated, and the loop continues with the next iteration.
Syntax:
for variable in inerrable:
# code to be executed
if condition:
continue
Example:
if fruit == 'date':
continue
print(fruit)
How it Works
1. The loop iterates over the sequence, executing the code inside the loop.
2. When the continue statement is encountered, the current iteration is
terminated.
3. The loop continues with the next iteration, skipping the remaining code in
the current iteration.
Example:
for i in range(5):
if i == 2:
pass # Placeholder for future code
print(i)
# Output:
0
1
2
3
4
2. Using continue
The continue statement skips the current iteration and jumps to the next one.
Example:
for i in range(5):
if i == 2:
continue # Skip when i is 2
print(i)
# Output:
0
1
3
4
Q1. What will be the output after the following statements?
x, y = 0, 1
while y < 10:
print(y, end=‘ ’)
x, y = y, x + y
Answer
112358
Q2. What will be the output after the following statements?
x=1
while x < 4:
x += 1 y = 1
while y < 3:
print(y, end=‘ ’)
y += 1
Answer
121212
Q3. What will be the output after the following statements?
x = 70
if x <= 30 or x >= 100:
print(‘true’)
elif x <= 50 and x == 50:
print(‘not true’)
elif x >= 150 or x <= 75:
print(‘false’)
else:
print(‘not false’)
Answer
False
Q4. What will be the output after the following statements?
x = 40
y = 25
if x + y >= 100:
print(‘true’)
elif x + y == 50:
print(‘not true’)
elif x + y <= 90:
print(‘false’)
else:
print(‘not false’)
Answer
False
Q5. What will be the output after the following statements?
x, y = 2, 5
while y – x < 5:
print(x*y, end=‘ ’)
x += 3 y += 4
Answer
10 45
Q5. What will be the output after the following statements?
x = 15
if x > 15:
print(0)
elif x == 15:
print(1)
else:
print(2)
Answer
1
Q6 what will be the output after the following statements?
x = 15
if x > 10 and x <= 15:
print(‘true’)
elif x > 15 and x < 25:
print(‘not true’)
elif x > 25 and x < 35:
print(‘false’) else:
print(‘not false’)
Answer
True
Q7. Write a Python program to convert time from 12 hour to 24-hour format.
# Function to convert the time format
def convert24(str1):
# checking if last two elements of time
# is AM and first two elements are 12
if str1[– 2:] == “AM” and str1[:2] == “12”:
return “00” + str1[2:– 2]
# remove the AM
elif str1[-2:] == “AM”:
return str1[:– 2]
# checking if last two elements of time is PM and first two elements
are 12
elif str1[– 2:] == “PM” and str1[:2] == “12”:
return str1[:– 2]
else:
# add 12 to hours and remove PM
return str(int(str1[:2]) + 12) + str1[2:8]
# Driver Code
print(convert24(“08:05:45 PM”))