sparsh0106 /
URA302---Python-Programming
Code Issues Pull requests Actions Projects Security Insights
URA302---Python-Programming / URA302_(Lab_Assignment_3).ipynb
sparsh0106 Created using Colab 34b2b9d · last month
554 lines (554 loc) · 14.3 KB
Open in Colab
Q1. Write a Python program to calculate the difference between a given number
and 17 with the help of function. If the number is greater than 17, return twice the
absolute difference.
In [ ]: def difference(n):
if n <= 17:
return 17 - n
else:
return (n - 17) * 2
print(difference(22))
print(difference(14))
10
3
Q2. Write a Python program for a function. to test whether a number is within 100
to 1000 or 2000.
In [ ]:
def near_thousand(n):
return (100 <= n <= 1000) or (n == 2000)
print(near_thousand(1000))
print(near_thousand(900))
print(near_thousand(800))
print(near_thousand(2200))
print(near_thousand(2000))
True
True
True
False
True
Q3. Write a Python program to reverse a string.
In [ ]: def rev_str(a):
return a[::-1]
print(rev_str('sparsh'))
print(rev_str('lmfao'))
hsraps
oafml
Q4. Write a Python function that accepts a string and counts the number of upper
and lower case letters.
In [ ]: def count(a):
upr = 0
lwr = 0
for i in a:
if i.isupper():
upr += 1
if i.islower():
lwr += 1
return upr, lwr
print(count("Sparsh"))
print(count("sPARSH"))
(1, 5)
(5, 1)
Q5. Write a Python function that takes a list and returns a new list with distinct
elements from the first list.
In [ ]: def distinct(l):
new_L = []
for i in l:
if i not in new_L:
new_L.append(i)
return new_L
print(distinct([1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5]))
[1, 2, 3, 4, 5]
Q6. Write a Python program to print the even numbers from a given list. Sample List
: [1, 2, 3, 4, 5, 6, 7, 8, 9]
In [ ]: def even(l):
even_l = [i for i in l if i%2 == 0]
return even_l
print(even([1, 2, 3, 4, 5, 6, 7, 8, 9]))
[2, 4, 6, 8]
Q7. Write a Python program to access a function inside a function
In [ ]: def add(n):
if n == 0:
return 0
else:
return n + add(n-1)
print(add(5))
15
Q8. Define a Python function student(). Using function attributes display the names
of all arguments.
In [2]: def student(name,age):
print(name,age)
student("Sparsh",17)
student("Anahita", 18)
Sparsh 17
Anahita 18
Q9. Write a Python class named Student with two attributes: student_id,
student_name. Add a new attribute: student_class. Create a function to display all
attributes and their values in the Student class
In [8]: class Student:
def __init__(self, student_id, student_name):
self.student_id = student_id
self.student_name = student_name
self.student_age = None
def display_attributes(self):
print(f"Student ID: {self.student_id}")
print(f"Student Name: {self.student_name}")
print(f"Student Age: {self.student_age}")
student = Student(102323080, "Sparsh Agarwal")
student.age = 17
student.display_attributes()
Student ID: 102323080
Student Name: Sparsh Agarwal
URA302---Python-Programming
Student Age: None / URA302_(Lab_Assignment_3).ipynb Top
Q10. Write a Python class named Student with two instances student1, student2 and
Preview Code Blame Raw
assign values to the instances' attributes. Print all the attributes of the student1,
student2 instances with their values in the given format.
In [9]: class Student:
def __init__(self, student_id, student_name, student_age):
self.student_id = student_id
self.student_name = student_name
self.student_age = student_age
def display(self):
print(f"Student ID: {self.student_id}")
print(f"Student Name: {self.student_name}")
print(f"Student Age: {self.student_age}")
print()
student1 = Student(102323080, "Sparsh", 17)
student2 = Student(123456789, "Anahita", 18)
print("Details of student1:")
student1.display()
print("Details of student2:")
student2.display()
Details of student1:
Student ID: 102323080
Student ID: 102323080
Student Name: Sparsh
Student Age: 17
Details of student2:
Student ID: 123456789
Student Name: Anahita
Student Age: 18
Q11. Write a Python class named Circle constructed from a radius and two methods
that will compute the area and the perimeter of a circle.
In [10]: import math
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
def perimeter(self):
return 2 * math.pi * self.radius
circle = Circle(5)
print(f"Area of the circle: {circle.area()}")
print(f"Perimeter of the circle: {circle.perimeter()}")
Area of the circle: 78.53981633974483
Perimeter of the circle: 31.41592653589793
Q12. Write a Python class that has two methods: get_String and print_String ,
get_String accept a string from the user and print_String prints the string in upper
case.
In [12]: class nig:
def __init__(self):
self.user_string = ""
def get_String(self):
self.user_string = input("Enter a string: ")
def print_String(self):