In [1]:
import turtle
In [2]:
import math 

class Circle:
    # Construct a circle object 
    def __init__(self, radius = 1):
        self.radius = radius

    def getPerimeter(self):
        return 2 * self.radius * math.pi

    def getArea(self):
        return self.radius * self.radius * math.pi
          
    def setRadius(self, radius):
        self.radius = radius
        

def main():
    # Create a circle with radius 1
    circle1 = Circle()
    print("The area of the circle of radius",
        circle1.radius, "is", circle1.getArea())

    # Create a circle with radius 25
    circle2 = Circle(25)
    print("The area of the circle of radius",
        circle2.radius, "is", circle2.getArea())

    # Create a circle with radius 125
    circle3 = Circle(125)
    print("The area of the circle of radius",
        circle3.radius, "is", circle3.getArea())

    # Modify circle radius
    circle2.radius = 100
    print("The area of the circle of radius", 
        circle2.radius, "is", circle2.getArea())

main() # Call the main function
        
The area of the circle of radius 1 is 3.141592653589793
The area of the circle of radius 25 is 1963.4954084936207
The area of the circle of radius 125 is 49087.385212340516
The area of the circle of radius 100 is 31415.926535897932
In [81]:
class Point:
    """ Point class for representing and manipulating x,y coordinates. """

    def __init__(self, initX, initY):
        """ Create a new point at the given coordinates. """
        self.x = initX
        self.y = initY

    def getX(self):
        return self.x
         
    def getY(self):
        return self.y


p = Point(7, 6)
print(p.getX())
print(p.getY())
print(p)
7
6
<__main__.Point object at 0x0000020E063606A0>
In [10]:
class TV:
    def __init__(self):
        self.channel = 1  # Default channel is 1
        self.volumeLevel = 1  # Default volume level is 1
        self.on = False  # By default TV is off
  
    def turnOn(self):
        self.on = True
  
    def turnOff(self):
        self.on = False
  
    def getChannel(self):
        return self.channel

    def setChannel(self, channel):
        if self.on and 1 <= self.channel <= 120:
            self.channel = channel
  
    def getVolumeLevel(self):
        return self.volumeLevel

    def setVolume(self, volumeLevel):
        if self.on and \
              1 <= self.volumeLevel <= 7:
            self.volumeLevel = volumeLevel
  
    def channelUp(self):
        if self.on and self.channel < 120:
            self.channel += 1
  
    def channelDown(self):
        if self.on and self.channel > 1:
            self.channel -= 1
  
    def volumeUp(self):
        if self.on and self.volumeLevel < 7:
            self.volumeLevel += 1
  
    def volumeDown(self):
        if self.on and self.volumeLevel > 1:
            self.volumeLevel -= 1
In [11]:
def main():
    tv1 = TV()
    tv1.turnOn()
    tv1.setChannel(30)
    tv1.setVolume(3)
    
    tv2 = TV()
    tv2.turnOn()
    tv2.channelUp()
    tv2.channelUp()
    tv2.volumeUp()
    
    print("tv1's channel is", tv1.getChannel(), 
        "and volume level is", tv1.getVolumeLevel())
    print("tv2's channel is", tv2.getChannel(),
        "and volume level is", tv2.getVolumeLevel())

main() # Call the main function
tv1's channel is 30 and volume level is 3
tv2's channel is 3 and volume level is 2
In [13]:
from datetime import datetime
d = datetime.now()
print("Current year is " + str(d.year))
print("Current month is " + str(d.month))
print("Current day of month is " + str(d.day))
print("Current hour is " + str(d.hour))
print("Current minute is " + str(d.minute))
print("Current second is " + str(d.second))
Current year is 2022
Current month is 11
Current day of month is 22
Current hour is 16
Current minute is 11
Current second is 15
In [39]:
import math 

class Circle1:
    # Construct a circle object 
    def __init__(self, radius = 1):
        self.__radius = radius
        self.__color = "blue"

    def getRadius(self):
        return self.__radius
    
    def setRadius(self,radius):
        self.__radius = radius

    def getPerimeter(self):
        return 2 * self.__radius * math.pi

    def getArea(self):
        return self.__radius * self.__radius * math.pi
    
    def getColor(self):
        return self.__color
In [40]:
c1 = Circle1(100)
In [41]:
c1.getArea()
Out[41]:
31415.926535897932
In [42]:
c1.__radius 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [42], in <cell line: 1>()
----> 1 c1.__radius

AttributeError: 'Circle1' object has no attribute '__radius'
In [43]:
c1.color
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [43], in <cell line: 1>()
----> 1 c1.color

AttributeError: 'Circle1' object has no attribute 'color'
In [44]:
c1.getColor()
Out[44]:
'blue'
In [45]:
c1.getRadius()
Out[45]:
100
In [46]:
c1.__radius
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [46], in <cell line: 1>()
----> 1 c1.__radius

AttributeError: 'Circle1' object has no attribute '__radius'
In [47]:
c1.setRadius(200)
In [48]:
c1.getRadius()
Out[48]:
200
In [49]:
datetime.now()
Out[49]:
datetime.datetime(2022, 11, 22, 16, 46, 17, 274156)
In [50]:
class Loan:
    def __init__(self, annualInterestRate = 2.5, 
                 numberOfYears = 1, loanAmount = 1000, borrower = " "):
        self.__annualInterestRate = annualInterestRate
        self.__numberOfYears = numberOfYears
        self.__loanAmount = loanAmount
        self.__borrower = borrower

    def getAnnualInterestRate(self):
        return self.__annualInterestRate

    def getNumberOfYears(self):
        return self.__numberOfYears

    def getLoanAmount(self):
        return self.__loanAmount

    def getBorrower(self):
        return self.__borrower

    def setAnnualInterestRate(self, annualInterestRate):
        self.__annualInterestRate = annualInterestRate

    def setNumberOfYears(self, numberOfYears):
        self.__numberOfYears = numberOfYears

    def setLoanAmount(self, loanAmount):
        self.__loanAmount = loanAmount

    def setBorrower(self, borrower):
        self.__borrower = borrower

    def getMonthlyPayment(self):
        monthlyInterestRate = self.__annualInterestRate / 1200
        monthlyPayment = \
          self.__loanAmount * monthlyInterestRate / (1 - (1 / 
          (1 + monthlyInterestRate) ** (self.__numberOfYears * 12)))
        return monthlyPayment

    def getTotalPayment(self):
        totalPayment = self.getMonthlyPayment() * \
            self.__numberOfYears * 12
        return totalPayment   
In [52]:
def main():
    # Enter yearly interest rate
    annualInterestRate = eval(input
        ("Enter yearly interest rate, for example, 7.25: "))

    # Enter number of years
    numberOfYears = eval(input(
        "Enter number of years as an integer: "))

    # Enter loan amount
    loanAmount = eval(input(
        "Enter loan amount, for example, 120000.95: "))

    # Enter a borrwer
    borrower = input("Enter a borrower's name: ")

    # Create a Loan object
    loan = Loan(annualInterestRate, numberOfYears, 
        loanAmount, borrower)

    # Display loan date, monthly payment, and total payment
    print("The loan is for", loan.getBorrower())
    print("The monthly payment is", format(
        loan.getMonthlyPayment(), '.2f'))
    print("The total payment is", format(
        loan.getTotalPayment(), '.2f'))

main() # Call the main function
Enter yearly interest rate, for example, 7.25: 12
Enter number of years as an integer: 10
Enter loan amount, for example, 120000.95: 1000000
Enter a borrower's name: Boby
The loan is for Boby
The monthly payment is 14347.09
The total payment is 1721651.38
In [53]:
class BMI:
    def __init__(self, name, age, weight, height):
        self.__name = name
        self.__age = age
        self.__weight = weight
        self.__height = height
  
    def getBMI(self):
        KILOGRAMS_PER_POUND = 0.45359237
        METERS_PER_INCH = 0.0254  
        bmi = self.__weight * KILOGRAMS_PER_POUND /  \
           ((self.__height * METERS_PER_INCH) * \
           (self.__height * METERS_PER_INCH))
        return round(bmi * 100) / 100
  
    def getStatus(self):
        bmi = self.getBMI()
        if bmi < 18.5:
            return "Underweight"
        elif bmi < 25:
            return "Normal"
        elif bmi < 30:
            return "Overweight"
        else:
            return "Obese"
  
    def getName(self):
        return self.__name
  
    def getAge(self):
        return self.__age
  
    def getWeight(self):
        return self.__weight
  
    def getHeight(self):
        return self.__height
In [54]:
def main():
    bmi1 = BMI("John Doe", 18, 145, 70)
    print("The BMI for", bmi1.getName(), "is",
        bmi1.getBMI(), bmi1.getStatus())
    
    bmi2 = BMI("Peter King", 50, 215, 70)
    print("The BMI for", bmi2.getName(), "is",
        bmi2.getBMI(), bmi2.getStatus())

main() # Call the main function
The BMI for John Doe is 20.81 Normal
The BMI for Peter King is 30.85 Obese
In [ ]: