Lecture 1 - OOP Python
Lecture 1 - OOP Python
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: ")
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: "))
# 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
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
Class: Flower
Attributes :
Name
Color
Fragrance
Methods:
describe()