Basics
Mon 21 July 2025
# Cell 1
a = 10
# Cell 2
b = 20
# Cell 3
c = a + b
print(c)
# Cell 4
for i in range(5):
print(i)
# Cell 5
def square(x):
return x * x
class Person:
pass
p1 = Person()
print(type(p1))
<class '__main__.Person'>
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
s1 = Student("Sara", 20)
print(s1.name, s1.age)
class Dog:
def __init__(self, breed):
self.breed = breed
def bark(self):
print("Woof! I am a", self.breed)
d = Dog("Labrador")
d.bark()
class Book:
def __init__(self, title):
self.title = title
def __str__(self):
return f"Book: {self.title}"
b = Book("Harry Potter")
print(b)
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("I am an animal.")
class Cat(Animal):
def speak(self):
print(f"{self.name} says Meow!")
c = Cat("Luna")
c.speak()
class Parent:
def __init__(self):
print("Parent init")
class Child(Parent):
def __init__(self):
super().__init__()
print("Child init")
c = Child()
Parent init
Child init
class Account:
def __init__(self, balance):
self.__balance = balance # private
def show_balance(self):
print("Balance:", self.__balance)
acc = Account(1000)
acc.show_balance()
class BankAccount:
def __init__(self, name, balance):
self.name = name
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient funds")
def show(self):
print(f"{self.name} has ₹{self.__balance}")
user1 = BankAccount("Sara", 5000)
user1.show()
user1.deposit(2000)
user1.show()
user1.withdraw(3000)
user1.show()
class MathOps:
@staticmethod
def add(a, b):
return a + b
print(MathOps.add(5, 7))
class Student:
school = "CodeHigh"
@classmethod
def get_school(cls):
return cls.school
print(Student.get_school())
class Calculator:
def add(self, a: int, b: int) -> int:
"""Returns the sum of two numbers."""
return a + b
calc = Calculator()
print(calc.add(2, 3))
class Vehicle:
def __init__(self, name):
self.name = name
def drive(self):
print(f"{self.name} is driving.")
class Car(Vehicle):
def drive(self):
print(f"{self.name} (car) is zooming!")
class Bike(Vehicle):
def drive(self):
print(f"{self.name} (bike) is vrooming!")
v = Vehicle("Generic")
v.drive()
c = Car("Suzuki")
c.drive()
b = Bike("Yamaha")
b.drive()
class Student:
def __init__(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_name(self, new_name):
self.__name = new_name
s = Student("Sara")
print(s.get_name())
s.set_name("Queen Sara")
print(s.get_name())
class Book:
def __init__(self, title):
self.title = title
self.available = True
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def show_books(self):
for book in self.books:
status = "Available" if book.available else "Issued"
print(f"{book.title} - {status}")
def issue_book(self, title):
for book in self.books:
if book.title == title and book.available:
book.available = False
print(f"{title} has been issued.")
return
print(f"{title} is not available.")
lib = Library()
book1 = Book("Harry Potter")
book2 = Book("Sherlock Holmes")
lib.add_book(book1)
lib.add_book(book2)
lib.show_books()
lib.issue_book("Harry Potter")
lib.show_books()
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
employees = [
Employee("Sara", 70000),
Employee("Ali", 65000),
Employee("Afia", 72000)
]
for emp in employees:
print(f"{emp.name} earns ₹{emp.salary}")
Score: 50
Category: basics