Data Abstraction
SUMMARY
Data abstraction in Python involves simplifying complex data and operations by focusing on the essential details while hiding unnecessary complexities. It allows you to create custom data types and structures to represent real-world objects, making your code more organized and readable. By defining abstract data types, you can encapsulate data and behavior, enabling better organization and modular programming. Data abstraction promotes code reusability and makes it easier to manage and maintain large programs by providing a higher-level perspective on data and its manipulation.
Homework:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def print_people(people):
for person in people:
print(f"{person.name} is {person.age} years old")
def get_oldest_person(people_dict):
if not people_dict:
return None
oldest_person = max(people_dict, key=lambda k: people_dict[k])
return oldest_person
# Example usage:
# Creating a list of Person objects
people_list = [
Person("Aditi", 15),
Person("Eshika", 15),
Person("Cindy", 15),
]
# Calling the print_people function
print_people(people_list)
# Creating a dictionary of people and their ages
people_dict = {
"Samhita": 16,
"Nithika": 15,
"Anusha": 15,
}
# Calling the get_oldest_person function
oldest_person = get_oldest_person(people_dict)
if oldest_person:
print(f"The oldest person is {oldest_person} with age {people_dict[oldest_person]}")
else:
print("No people in the dictionary.")
Aditi is 15 years old
Eshika is 15 years old
Cindy is 15 years old
The oldest person is Samhita with age 16
Popcorn Hacks
age = 15
print(age)
birthday = True
print(birthday)
age = 16
name = "Aditi"
print (name , "is" , age , "years old")
friend1 = "Eshika"
friend2 = "Cindy"
friend_list = [name, friend1, friend2]
print(friend_list [2] , "is better than" , friend_list [1])
15
True
Aditi is 16 years old
Cindy is better than Eshika
import json
string = ["Aditi", "Cindy", "Eshika", "Samhita"]
json_obj = json.dumps(string)
print(json_obj)
["Aditi", "Cindy", "Eshika", "Samhita"]
name = "Aditi"
age = 15
print("Name:", name)
print("Age:", age)
Name: Aditi
Age: 15
index = int(input("Input the Fibonacci sequence index():")) - 1
num1 = 0
num2 = 1
for i in range(index):
tempnum1 = num2
num2 = num1 + num2
num1 = tempnum1
print(num1)
KeyboardInterrupt