0% found this document useful (0 votes)
3 views

Lecture 1 - OOP Python

The document contains programming exercises that prompt users to input numbers and print them in reverse order, round decimal numbers, and sum them. It also introduces Object Oriented Programming (OOP) concepts, including classes, objects, attributes, and methods. Additionally, it provides examples of classes like Car and Flower, illustrating their attributes and methods.

Uploaded by

dr.yassin.ashraf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 1 - OOP Python

The document contains programming exercises that prompt users to input numbers and print them in reverse order, round decimal numbers, and sum them. It also introduces Object Oriented Programming (OOP) concepts, including classes, objects, attributes, and methods. Additionally, it provides examples of classes like Car and Flower, illustrating their attributes and methods.

Uploaded by

dr.yassin.ashraf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture 1

Problem 1

Write a program that prompts the user to enter two numbers and
prints them in reverse order.
Problem 1 - Solution
# Prompt the user to enter two numbers
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")

# Print them in reverse order


print("Numbers in reverse order:")
print(num2)
print(num1)
Problem 2
Write a program that prompts the user to enter a series of numbers
and prints them in reverse order.

Please enter the length of your series: 4


Please enter number 1 : 1
Please enter number 2 : 3
Please enter number 3 : 5
Please enter number 4 : 6
The numbers in reverse order are : 6 5 3 1
Problem 2 - Solution
# Prompt the user to enter the length of the series
length = int(input("Please enter the length of your series: "))

# Initialize an empty list to store numbers


numbers = []

# Loop to get user input


for i in range(length):
num = int(input(f"Please enter number {i+1}: "))
numbers.append(num)

# Print the numbers in reverse order


print("The numbers in reverse order are:", *reversed(numbers))
Problem 2 - Solution

# Print the numbers in reverse order


print("The numbers in reverse order are:",
*reversed(numbers)) ??

for item in
numbers[::-1]:
print (item)
Exercises
numbers = [10, 20, 30, 40, 50, 60, 70, 80]

print(numbers[1::3])
[20, 50, 80]

print(numbers[1:3])
[20, 30]

print(numbers[6::-1])
[70, 60, 50,40,30,20,10]

print(numbers[6:2:-1])
[70, 60, 50,40]
Problem 3
• Write a program that prompts the user to enter a decimal number
and outputs the number rounded to the nearest integer.
Problem 3 – Solution
# Prompt the user to enter a decimal number
num = float(input("Enter a decimal number: "))

# Round the number to the nearest integer


rounded_num = round(num)

# Output the result


print(f"The number rounded to the nearest integer is:
{rounded_num}")
Problem 4
• Modify the above program where the user enters five decimal
numbers, sum these numbers and round the sum to the nearest
integer.
Problem 4 - Solution
# Initialize sum variable
SUM =0

# Loop to get five decimal numbers from the user and sum them
for i in range(5):
num = float(input(f"Enter decimal number {i+1}: "))
SUM += num

# Round the sum to the nearest integer


rounded_sum = round(SUM)

# Output the result


print(f"The sum rounded to the nearest integer is: {rounded_sum}")
Problem 4 - Solution
# Initialize an array to store numbers
numbers = []

# Loop to get five decimal numbers from the user and


store them in an array
for i in range(5):
num = float(input(f"Enter decimal number {i+1}: "))
numbers.append(num)

# Sum the numbers


SUM = sum(numbers)
CAN YOU CALCULATE THE AVERAGE?
Object Oriented Programming
• Object oriented programming (OOP) is a
programming paradigm based on the concept of
objects.

• The object contains both data and code


1. Data  attributes
2. Code -> methods (actions that an object can
perform)
Object Oriented Programming

Class

Polymorphi
sm Object

OOP

Abstract Inherita
ion nce

Encapsulat
ion
Object Oriented Programming
• Class: the class is a
user defined data
structure that binds the
data members and methods
into a single unit.
Using a class, you can
create several objects.

• Object: an object is an
instance of the class.
It is used to refer to
the class attributes and
Object Oriented Programming
Class : Car
Object 1 Object 2

Attributes: Type : BMW Type : Audi


Type Color : Black Color : Re
Color YearOfProduction: YearOfProduction:
YearOfProduction 2023 2021

Methods: drive (110)


drive(speed) brake() drive (90)
brake() brake()
Object Oriented Programming

Class: Flower

Attributes :
Name
Color
Fragrance

Methods:
describe()

You might also like