Python Class 3 Assignment Solution
Python Class 3 Assignment Solution
Q1. Write a Python program to find those numbers which are divisible by 7 and
multiples of 5, between 1500 and 2700 (both included).
Output:
1505,1540,1575,1610,1645,1680,1715,1750,1785,1820,1855,1890,1925,1960,1
995,2030,2065,2100,2135,2170,2205,2240,2275,2310,2345,2380,2415,2450,24
ills
85,2520,2555,2590,2625,2660,2695
Solution:
nl=[]
Sk
for x in range(1500, 2701):
if (x%7==0) and (x%5==0):
nl.append(str(x)) a
print (','.join(nl))
at
Q2. Write a Python program that accepts a word from the user and reverses it.
D
Solution:
ro
print(word[char], end="")
print("\n")
Q3. Write a Python program that prints all the numbers from 0 to 6 except 3 and
6. Note : Use 'continue' statement.
Expected Output : 0 1 2 4 5
Solution:
for x in range(6):
if (x == 3 or x==6):
continue
print(x,end=' ')
print("\n")
ills
Q4. Write a Python program that prints each item and its corresponding type from
the following list.
Sk
INPUT = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12], {"class":'V',
"section":'A'}]
OUTPUT: a
Type of 1452 is <class 'int'>
Type of 11.23 is <class 'float'>
at
Type of (1+2j) is <class 'complex'>
D
Solution:
datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12],
{"class":'V', "section":'A'}]
for item in datalist:
print ("Type of ",item, " is ", type(item))
Q5. Write a Python program to check the validity of passwords input by users.
Validation :
At least 1 letter between [a-z] and 1 letter between [A-Z].
At least 1 number between [0-9].
At least 1 character from [$#@].
Minimum length 6 characters.
ills
Maximum length 16 characters.
Sk
Solution:
import re
a
p= input("Input your password")
at
x = True
while x:
D
if (len(p)<6 or len(p)>12):
break
w
ills
if x:
print("Not a Valid Password")
Sk
Q6. Write a Python program to get the Fibonacci series between 0 and 50.
Note : The Fibonacci Sequence is the series of numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, ....
a
Every next number is found by adding up the two numbers before it.
Expected Output : 1 1 2 3 5 8 13 21 34
at
D
Solution:
x,y=0,1
while y<50:
w
print(y)
ro
x,y = y,x+y
G
OUTPUT:
k is a consonant.
Solution:
print("%s is a vowel." % l)
elif l == 'y':
ills
print("Sometimes letter y stand for vowel, sometimes stand for
consonant.")
else:
Sk
print("%s is a consonant." % l)
a
Q8. Write a Python program that takes a string as input and replaces all
occurrences of a given character with another character.
at
INPUT: Enter a string: We study at GrowDataSkills
D
Solution:
G
replaced_string = ""
if char == old_char:
replaced_string += new_char
else:
replaced_string += char
return replaced_string
ills
input_string = input("Enter a string: ")
Sk
new_char = input("Enter the replacement character: ")
a
Q9: Write a Python function to reverse a list at a specific location(
at
INPUT: [10,20,30,40,50,60,70,80]
start_pos = 2
D
end_pos = 4
OUTPUT: Reverse elements of the said list between index position 2 and 4
w
Q10. Write a Python program that takes a string as input and checks if it is a
G
Solution:
def is_palindrome(input_string):
if is_palindrome(input_string):
print("It is a palindrome.")
ills
else:
Sk
Q11. Write a Python program that takes a sentence as input and capitalizes the
first letter of each word.
a
INPUT: Enter a sentence: we are growdataskills
at
OUTPUT: Capitalized sentence: We Are Growdataskills
Solution:
D
def capitalize_words(sentence):
words = sentence.split()
w
INPUT:
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
OUTPUT: Common elements: [3, 4, 5]
ills
Solution:
Sk
common_elements = [item for item in list1 if item in list2]
return common_elements
list1 = [1, 2, 3, 4, 5]
a
at
list2 = [3, 4, 5, 6, 7]
Python functions
w
OUTPUT: 24
Solution:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(n))
ills
Q. Write a Python function that accepts a string and counts the number of upper
and lower case letters.
Sk
INPUT: The quick Brow Fox'
OUTPUT: a
No. of Upper case characters : 3
No. of Lower case Characters : 13
at
Solution:
D
def string_test(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}
w
for c in s:
ro
if c.isupper():
d["UPPER_CASE"]+=1
G
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
ills
Q. Write a Python function to check whether a number falls within a given
range(3,9)
INPUT:5
Sk
OUTPUT: 5 is in the range
Solution:
def test_range(n):
a
at
if n in range(3,9):
else :
test_range(5)
ro
prime number.
Solution:
def is_prime(n):
if n <= 1:
return False
if n % i == 0:
ills
return False
return True
Sk
num = int(input("Enter an integer: "))
if is_prime(num):
Q. Write a Python function that takes a list of numbers as input and returns the
average of the numbers.
w
INPUT: [1,2,3,4,5,6,7,8,9,10]
ro
OUTPUT: 5.5
Solution:
G
def compute_average(numbers):
if not numbers:
return 0
print(compute_average(input_numbers))
Q. Write a Python function that takes a list as input and returns a new list
containing only the unique elements from the input list.
ills
INPUT: [1,2,3,4,1,2,0,0,1]
OUTPUT: [0, 1, 2, 3, 4]
Solution:
Sk
def find_unique_elements(input_list):
return list(set(input_list))
a
at
input_list = [1,2,3,4,1,2,0,0,1]
Q. Write a Python function that takes two strings as input and checks if they are
w
INPUT:
Solution:
if are_anagrams(string1, string2):
ills
else:
Sk
Q. Write a Python function that takes a list and an element as input and returns
the number of occurrences of that element in the list.
INPUT:
a
at
input_list = [ 1,2,3,4,2,2,3,4,5,9,2,6]
OUTPUT: Occurrences: 4
Solution:
w
return input_list.count(element)
G
input_list = [ 1,2,3,4,2,2,3,4,5,9,2,6]
INPUT:[(1, 3), (2, 1), (3, 2), (4, 5), (5, 4)]
OUTPUT: Sorted list of tuples: [(2, 1), (3, 2), (1, 3), (5, 4), (4, 5)]
Solution:
def sort_list_of_tuples_by_second_element(input_list):
ills
return sorted(input_list, key=lambda x: x[1])
Sk
input_list = [(1, 3), (2, 1), (3, 2), (4, 5), (5, 4)]
INPUT: [3, 5, 2, 8, 9, 5, 1]
def find_second_largest(nums):
ro
nums_set = set(nums)
G
if len(nums_set) < 2:
return None
nums_set.remove(max(nums_set))
return max(nums_set)
input_nums = [3, 5, 2, 8, 9, 5, 1]
print("Second largest element:", find_second_largest(input_nums))
Q.Write a Python lambda function that takes a list of numbers and an exponent
n as input and returns a new list with each element raised to the power of n.
INPUT:
input_numbers = [1, 2, 3, 4, 5]
ills
exponent = 3
Sk
Solution:
a=list(map(lambda x: x ** n, input_numbers))
print(a)
a
at
Q. Write a Python function that takes a list of integers as input and returns a new
list containing only the odd numbers.
D
a= list(filter(lambda x: x % 2 != 0, input_numbers))
ro
print(a)
G
OUTPUT:
ills
Solution:
import math
Sk
class Circle:
self.radius = radius a
at
def calculate_circle_area(self):
D
def calculate_circle_perimeter(self):
ro
circle = Circle(radius)
area = circle.calculate_circle_area()
perimeter = circle.calculate_circle_perimeter()
print("Area of the circle:", area)
Q. Write a Python program to create a person class. Include attributes like name,
country and date of birth. Implement a method to determine the person's age.
SAMPLE OUTPUT:
ills
Person 1:
Sk
Country: France
Age: 60
a
at
Person 2:
Country: Canada
Age: 40
ro
Person 3:
Country: USA
Age: 23
Solution:
class Person:
self.name = name
ills
self.country = country
self.date_of_birth = date_of_birth
Sk
def calculate_age(self):
today = date.today()
return age
w
# Example usage
ro
print("Person 1:")
print("Name:", person1.name)
print("Country:", person1.country)
print("Age:", person1.calculate_age())
print("\nPerson 2:")
ills
print("Name:", person2.name)
print("Country:", person2.country)
Sk
print("Date of Birth:", person2.date_of_birth)
print("Age:", person2.calculate_age())
print("\nPerson 3:")
a
at
print("Name:", person3.name)
D
print("Country:", person3.country)
print("Age:", person3.calculate_age())
ro
SAMPLE INPUT:7,5
SAMPLE OUTPUT:
7 + 5 = 12
7-5=2
7 * 5 = 35
7/5 = 1.0
Solution:
class Calculator:
ills
return x + y
Sk
return x - y
return x * y a
def divide(self, x, y):
at
return x / y
D
if __name__ == "__main__":
w
calculator = Calculator()
ro
num1 = 7
num2 = 5
G
SAMPLE INPUT:
Circle(5)
ills
Triangle(3, 4, 5)
Square(6)
Sk
SAMPLE OUTPUT:
Circle: a
Area: 78.53981633974483
at
Perimeter: 31.41592653589793
D
Triangle:
Area: 6.0
w
Perimeter: 12
ro
Square:
Area: 36
G
Perimeter: 24
Solution:
import math
class Shape:
def area(self):
pass
def perimeter(self):
pass
ills
class Circle(Shape):
Sk
def __init__(self, radius):
self.radius = radius
def area(self): a
return math.pi * self.radius * self.radius
at
def perimeter(self):
D
class Triangle(Shape):
ro
self.side1 = side1
G
self.side2 = side2
self.side3 = side3
def area(self):
def perimeter(self):
class Square(Shape):
ills
def __init__(self, side):
self.side = side
Sk
def area(self):
def perimeter(self): a
return 4 * self.side
at
D
if __name__ == "__main__":
circle = Circle(5)
ro
triangle = Triangle(3, 4, 5)
square = Square(6)
G
print("Circle:")
print("Area:", circle.area())
print("Perimeter:", circle.perimeter())
print("\nTriangle:")
print("Area:", triangle.area())
print("Perimeter:", triangle.perimeter())
ills
print("\nSquare:")
Sk
print("Area:", square.area())
print("Perimeter:", square.perimeter())
a
at
D
w
ro
G