Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

python 2

The document outlines a series of Python programming exercises, each demonstrating different programming concepts such as classes, static methods, and recursion. It includes code snippets for various tasks like checking even/odd numbers, finding the youngest person among three, counting uppercase and lowercase letters in a string, calculating factorials, and summing the digits of a five-digit number. Each exercise is accompanied by expected outputs and structured to facilitate learning in a skill lab setting.

Uploaded by

Sneha Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

python 2

The document outlines a series of Python programming exercises, each demonstrating different programming concepts such as classes, static methods, and recursion. It includes code snippets for various tasks like checking even/odd numbers, finding the youngest person among three, counting uppercase and lowercase letters in a string, calculating factorials, and summing the digits of a five-digit number. Each exercise is accompanied by expected outputs and structured to facilitate learning in a skill lab setting.

Uploaded by

Sneha Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

NAME: Gurav Shruti

ROLL NO: 13
SUBJECT: SKILL LAB – PYTHON PROGRAMMING
EXPERIMENT NO 3

1. WAP to implement classes, object, static method and inner class.


class OC:
@staticmethod
def static_method():
print("This is a static method.")
class IC:
def __init__(self, name):
self.name = name
def greet(self):
print(f"Hello, {self.name}!")
OC.static_method()
inner_obj = OC.IC("Mehvish")
inner_obj.greet()

Output :-

2. If an integer is given throught the keyboard, find whether its even or odd
class EvenOdd:
def __init__(self, number):
self.number = number
class EvenOdd:
def __init__(self, number):
self.number = number
def check_even_odd(self):
if self.number % 2 == 0:
return "Even"
else:
return "Odd"
num = int(input("Enter an integer: "))
obj = EvenOdd(num)

result = obj.check_even_odd()
print(f"The number {num} is {result}.")

Output :

3. If ages of ryan, shyan and ajay are given as an input through keyboard, wap to
determine the youngest of three.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

class YoungestFinder:
def __init__(self):
self.people = []
def add_person(self, person):
self.people.append(person)

def find_youngest(self):
youngest = self.people[0]
for person in self.people:
if person.age < youngest.age:
youngest = person
return youngest

if __name__ == "__main__":
ryan_age = int(input("Enter Ryan's age: "))
shyan_age = int(input("Enter Shyan's age: "))
ajay_age = int(input("Enter Ajay's age: "))

# instances of Person
ryan = Person("Ryan", ryan_age)
shyan = Person("Shyan", shyan_age)
ajay = Person("Ajay", ajay_age)

youngest_finder = YoungestFinder()
youngest_finder.add_person(ryan)
youngest_finder.add_person(shyan)
youngest_finder.add_person(ajay)

youngest = youngest_finder.find_youngest()
print(f"The youngest is {youngest.name} with age {youngest.age}.")

Output :

4. WAP that defines a function count_lower_upper() that accepts a string and


calculates the number of uppercase and lowercase alphabets.It should return
these values as a dictionary. call this function for sample strings.
class StringAnalyzer:
def __init__(self, input_string):
self.input_string = input_string
#INITIALIZE
def count_lower_upper(self):
lower_count = 0
upper_count = 0
#Iteration
for char in self.input_string:
if char.islower():
lower_count += 1 #Count lower case
elif char.isupper():
upper_count += 1 #Count upper case
return {'lowercase': lower_count, 'uppercase': upper_count}
sample_strings = [
"Python Programming",
"Khan Mehvish",
"Skill Lab"
]
for string in sample_strings:
analyzer = StringAnalyzer(string)
result = analyzer.count_lower_upper()
print(f"String: '{string}' => {result}")

OUTPUT :-

5. WAP to find fact value of any number entered through the keyboard.
class Factorial:
def __init__(self, number):
self.number = number

def fact(self):
fact = 1
for i in range(1, self.number + 1):
fact *= i
return fact

num = int(input("Enter a number: "))


fact_obj = Factorial(num)
result = fact_obj.fact()
print(f"The factorial of {num} is: {result}")

OUTPUT :-

6. WAP that prints square root an dcube root of numbers from 1 to 10, up to 4
decimal places. Ensure that the output is displayed in separate lines, with
number center-justified and square root right-justified.
import math
class NumberRoots:
def __init__(self, number):
self.number = number
def square_root(self):
return round(math.sqrt(self.number), 4)
def cube_root(self):
return round(self.number ** (1/3), 4)
def display(self):
print(f"{self.number:^10} {self.square_root():>15}
{self.cube_root():>15}")
def main():
print(f"{'Number':^10} {'Square Root':>15} {'Cube Root':>15}")
for num in range(1, 11):
nr = NumberRoots(num)
nr.display()
if __name__ == "__main__":
main()

OUTPUT :-

7. A 5-digit positive integer is entered through the keyboard write a recursive


function to calculate sum of digits of 5-digit number
class DigitSum:
def sum_of_digits(self, num):
if num == 0:
return 0
return num % 10 + self.sum_of_digits(num // 10)
def main():
number = int(input("Enter a 5-digit positive integer: "))

if 10000 <= number <= 99999:


digit_sum = DigitSum()
result = digit_sum.sum_of_digits(number)
print(f"The sum of the digits of {number} is: {result}")
else:
print("Please enter a valid 5-digit number.")
main()

OUTPUT :-

You might also like