0% found this document useful (0 votes)
22 views9 pages

LAB 7 Python

Uploaded by

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

LAB 7 Python

Uploaded by

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

INTODUCTION TO PYTHON

LAB REPORT # 07
NAME:
Umm E Salma

INSTRUCTOR:
Ma’am : Shakira Musa Baig

BATCH:
CS-SP-21

I’D:
CK-21-110092

DEPARTMENT:
BSCS(5th semester)

DATE:
18/5/2023
TASK#01
Write a program to check if a list is empty or not.

CODE:
print("This program is created by UMM E SALMA")
list = [2, 3, 6 ,8]
# Replace [] with your list

if not list:
print("The list is empty.")
else:
print("The list is not empty.")

OUTPUT:

TASK#02
Write a program that takes a list of numbers from the user and calculates the sum of all the numbers

CODE:
print("This program is created by UMM E SALMA...")
numberlist = []

# Prompt the user to enter the numbers


while True:
num = input("Enter a number ")
if num == "":
break
numberlist.append(int(num))

# Calculate the sum of the numbers


sum_of_numbers = sum(numberlist)

# Print the entered numbers and the sum


print("Entered numbers:", numberlist)
print("Sum of the numbers:",sum_of_numbers)

OUTPUT:

TASK#03
Write a program that takes a list of integers from the user and finds the largest and smallest numbers in
the list.

CODE:
print("This program is created bu UMM E SALMA")
number = []

# Prompt the user to enter the numbers


while True:
num = input("Enter an integer")
if num == "":
break
number.append(int(num))

# Find the largest and smallest numbers


largest = max(number)
smallest = min(number)

# Print the largest and smallest numbers


print("Largest number:", largest)
print("Smallest number:",smallest)
OUTPUT:

TASK#04
Write a program that takes a list of names from the user and prints the names in reverse order.

CODE:
print("This program is created by UMM E SALMA...")
name_list = []

# Prompt the user to enter the names


while True:
name = input("Enter a name (or press Enter to finish): ")
if name == "":
break
name_list.append(name)

# Print the names in reverse order


print("Names in reverse order:")
for name in reversed(name_list):
print(name)

OUTPUT:
TASK#05
Write a program to find the sum of all elements in a list

CODE:
print("This program is create by UMM E SALMA")
numberlist = [10, 20, 30, 40, 50]
# Replace [10, 20, 30, 40, 50] with your list of numbers

# Calculate the sum of the elements


sum_of_elements = sum(numberlist)

# Print the sum


print("Sum of the elements:", sum_of_elements)

OUTPUT:

TASK#06
Write a program that takes a list of integers as input from the user and determines if the list contains
any even numbers. If it does, the program should print "Even numbers are present in the list."
Otherwise, it should print "No even numbers found in the list."

CODE:
print("This program is created by Umm E Salma")
number = []

# Prompt the user to enter the numbers


while True:
num = input("Enter an integer")
if num == "":
break
number.append(int(num))

# Check if the list contains any even numbers


even_found = any(num % 2 == 0 for num in number)

# Print the result


if even_found:
print("Even numbers are present in the list.")
else:
print("No even numbers found in the list.")

OUTPUT:

TASK#07
Write a program that contains Student class which includes attributes for name, age, and
grade.
Assign grades) Write a program that reads a list of scores and then assigns gradesbased on the
following scheme:
The grade is A if score is >=best – 10.
The grade is B if score is >=best – 20.
The grade is C if score is >=best – 30.
The grade is D if score is >=best – 40.
The grade is F otherwise.

CODE:
print("This program is created by UMM E SALMA...")
class Student:
def __init__ (self, name, age, score):
self.name = name
self.age = age
self.score = score
self.grade = self.assign_grade()

def assign_grade(self):
best_score = max(scores)
if self.score >= best_score - 10:
return 'A'
elif self.score >= best_score - 20:
return 'B'
elif self.score >= best_score - 30:
return 'C'
elif self.score >= best_score - 40:
return 'D'
else:
return 'F'

# Read the list of scores


scores = []
while True:
score = input("Enter a score")
if score == "":
break
scores.append(int(score))

# Create student objects and assign grades


students = []
for i in range(len(scores)):
name = input("Enter the name of student {}: ".format(i+1))
age = input("Enter the age of student {}: ".format(i+1))
student = Student(name, age, scores[i])
students.append(student)

# Print the students' information and grades


for student in students:
print("Name:", student.name)
print("Age:", student.age)
print("Score:", student.score)
print("Grade:", student.grade)
print()

OUTPUT:

TASK#08
Write a program that reads a list of integers and displays them in the reverse order in which they were
read.

CODE:
list = []

# Read the list of integers


while True:
num = input("Enter an integer")
if num == "":
break
list.append(int(num))

# Display the integers in reverse order


print("Integers in reverse order:")
for num in reversed(list):
print(num)

OUTPUT:

You might also like