25.final assesment

1. What is the boolean ouput of the code below

# two nested lists
l_one = [1,2,[3,4]]
l_two = [1,2,{'k1':4}]

# True or False?
l_one[2][0] >= l_two[2]['k1']

Answer

False

2. what will be output of the below program

class test:
    def __init__(self,a="Hello World"):
        self.a=a

    def display(self):
        print(self.a)
obj=test()
obj.display()

Answer

Hello World

3. Find the volume and surface area of a cylinder (height=2, radius=3)by defining the cylinder class.

The volume of a cylinder is given as $$ πr^2h $$

The surface area of the Cylinder is given as $$ 2πr^2 + 2πrh $$

Answer

class Cylinder:

    def __init__(self,height=1,radius=1):
        self.height = height
        self.radius = radius

    def volume(self):
        return self.height*3.14*(self.radius)**2

    def surface_area(self):
        top = 3.14 * (self.radius)**2
        return (2*top) + (2*3.14*self.radius*self.height)

c = Cylinder(2,3)

c.volume()
>> 56.52
c.surface_area()
>> 94.2

4. Write a function that asks for an integer and prints the square of it. Use a while loop with a try, except, else block to account for incorrect inputs.

Answer

def ask():
    while True:
        try:
            num1 = int(input('Please enter a number: '))
            result = num1**2
        except:
            print('An error occurred! Please try again!')
            continue
        else:
            print(f'Thank you. Your number squared is: {result}')
            break

5. Create a generator that generates the squares of numbers up to some number N.

Answer

def gensquares(N):
    for i in range(N):
        yield i**2

list(gensquares(10))
>> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

6. Create a generator that yields "n" random numbers between a low and high number (that are inputs).

Use the random library.

Answer

import random

def rand_num(low,high,n):

    for i in range(n):
        yield random.randint(low, high)

list(rand_num(1,10,12))
>> [7, 5, 6, 3, 1, 2, 8, 6, 4, 4, 1, 10]

7. Write a Python program that matches a string that has an a followed by one or more b's.

print(text_match("ab")) -- Found a match!
print(text_match("abc")) -- Found a match!

Answer

import re
def text_match(text):
        patterns = 'ab+'
        if re.search(patterns,  text):
                return 'Found a match!'
        else:
                return('Not matched!')

print(text_match("ab"))
>> Found a match!
print(text_match("abc"))
>> Found a match!

8. Define Line class methods to accept coordinates as a pair of tuples and return the slope and distance of the line.

Slope of the line is given as $$ \frac{y2 -y1}{x2 -x1} $$

Distance of the line is given as
$$ \sqrt{((x2 -x1)^2 + (y2 -y1)^2)} $$

coordinate1 = (3,2) coordinate2 = (8,10)

Answer

class Line:

    def __init__(self,coor1,coor2):
        self.coor1 = coor1
        self.coor2 = coor2

    def distance(self):
        # tuple unpacking
        x1,y1 = self.coor1
        x2,y2 = self.coor2
        return ((x2-x1)**2 + (y2-y1)**2)**0.5

    def slope(self):
        x1,y1 = self.coor1
        x2,y2 = self.coor2
        return (y2-y1)/(x2-x1)

coordinate1 = (3,2)
coordinate2 = (8,10)

li = Line(coordinate1,coordinate2)

li.distance()
>> 9.433981132056603

li.slope()
>> 1.6

9. Write a Python program to determine whether a given year is a leap year.

Answer

def leap_year(y):
    if y % 400 == 0:
        return True
    if y % 100 == 0:
        return False
    if y % 4 == 0:
        return True
    else:
        return False

print(leap_year(1900))
>> False
print(leap_year(2004))
>> True

10. Write a Python program to get the current time.

Answer

import datetime
print(datetime.datetime.now().time())