27.
For Loop and Continue Statement
Here's an example code:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
if fruit == "banana":
continue
print(fruit)
# Output:
# apple
# cherry
28. While Loop and Break Statement
Here's an example code:
i=0
while i < 10:
if i == 5:
break
print(i)
i += 1
# Output:
#0
#1
#2
#3
#4
29. Boolean Expressions
Here are the evaluations of the given Boolean expressions:
- (5 > 4) and (3 == 5): False
- not(5 > 4): False
- (5 > 4) or (3 == 5): True
- not((5 > 4) or (3 == 5)): False
- (True and True) and (True == False): False
- (not False) or (not True): True
30. Importing Modules
There are several ways to import modules in Python:
1. Importing a module: import math
2. Importing specific functions: from math import sin, cos
3. Importing all functions: from math import *
4. Importing a module with an alias: import math as m
31. Range Function
The range function generates a sequence of numbers.
- range(10): generates numbers from 0 to 9
- range(0, 10): generates numbers from 0 to 9
- range(0, 10, 1): generates numbers from 0 to 9 with a step size of 1
32. Senior Citizen Program
Here's a Python program:
def senior_citizen(name, year_of_birth):
current_year = 2024
age = current_year - year_of_birth
if age >= 60:
return f"{name} is a senior citizen."
else:
return f"{name} is not a senior citizen."
name = input("Enter your name: ")
year_of_birth = int(input("Enter your year of birth: "))
print(senior_citizen(name, year_of_birth))
33. Sum of N Numbers Program
def sum_of_n_numbers():
n = int(input("Enter the number of values: "))
total = 0
for i in range(n):
num = float(input("Enter number {}: ".format(i+1)))
total += num
print("The sum is: ", total)
sum_of_n_numbers()
34. Break and Continue Statements
for i in range(10):
if i == 5:
break
print(i)
for i in range(10):
if i == 5:
continue
print(i)
35. Sys.Exit()
import sys
print("Exiting the program...")
sys.exit()
36. Looping Control Statements
1. break: exits the loop
2. continue: skips the current iteration
3. pass: does nothing
37. Factorial and Binomial Coefficient Functions
import math
def factorial(n):
return math.factorial(n)
def binomial_coefficient(n, r):
return math.comb(n, r)
n = int(input("Enter the value of n: "))
r = int(input("Enter the value of r: "))
print("Factorial of {} is: {}".format(n, factorial(n)))
print("Binomial coefficient of {} and {} is: {}".format(n, r, binomial_coefficient(n, r)))
38. Student Details Program
class Student:
def __init__(self, name, usn, marks):
self.name = name
self.usn = usn
self.marks = marks
def display_details(self):
print("Name: ", self.name)
print("USN: ", self.usn)
print("Marks: ", self.marks)
name = input("Enter the student's name: ")
usn = input("Enter the student's USN: ")
marks = float(input("Enter the student's marks: "))
student = Student(name, usn, marks)
student.display_details()