0% found this document useful (0 votes)
6 views9 pages

Practicle No1 String Function

The document contains practical exercises on various Python functions including string, list, tuple, set, dictionary, and class methods. It provides examples of operations such as appending, counting, indexing, and manipulating data structures. Additionally, it includes a module for generating Fibonacci series and demonstrates the use of classes for calculations.

Uploaded by

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

Practicle No1 String Function

The document contains practical exercises on various Python functions including string, list, tuple, set, dictionary, and class methods. It provides examples of operations such as appending, counting, indexing, and manipulating data structures. Additionally, it includes a module for generating Fibonacci series and demonstrates the use of classes for calculations.

Uploaded by

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

Practicle No1 string function

*****************************************************************
s = input("Enter The String")
print(s.lower())
print(s.upper())
print(s.strip())
print(s.lstrip())
print(s.rstrip())
print(s.replace("Vivek","Nikam"))
print(s.find("Nikam"))
print(s.index("Vivek"))
print(s.count("l"))
print(s.startswith(" He"))
print(s.endswith("! "))
print(s.split(","))
print(".".join(["a", "b", "c"]))
print(s.capitalize())
print(s.title())
print(s.swapcase())
print(s.isalpha())
print("18".isdigit())
print("vivek18".isalnum())
print("VIVEK".isdecimal())
Practicle No2 list function
*******************************************************************
lst = input("Enter list: ").split()

lst.append(input("Append item: ")); print("After append:", lst)

temp = lst.copy(); temp.clear(); print("After clear:", temp)

print("Count item:", lst.count(input("Item to count: ")))

print("Index item:", lst.index(input("Item to find index of: ")))

lst.insert(int(input("Insert at index: ")), input("Insert value: ")); print("After


insert:", lst)

lst.remove(input("Item to remove: ")); print("After remove:", lst)

lst.pop(int(input("Index to pop: "))); print("After pop:", lst)

lst.sort(); print("After sort:", lst)

lst.reverse(); print("After reverse:", lst)

lst.extend(input("Enter items to extend with (space-separated): ").split());


print("After extend:", lst)
Practicle No3 tuples
*******************************************************************
my_tuple = ('a', 'b', 'c', 'b', 'd')
index_of_b = my_tuple.index('b')

print(index_of_b)

count_of_two = my_tuple.count('b')
print(count_of_two)

print(max(my_tuple))

print(min(my_tuple))

print(len(my_tuple))

rev = tuple(reversed(my_tuple))
print(rev)
Practicle No 4 sets function
*******************************************************************
num = {1, 2, 3, 4, 5}
num.add(4)
print(num)

another_set = (7, 9)
num.update(another_set)
print(num)

num.remove(2)
print(num)

num.discard(2)
num.discard(5) # No error
print(num)

num.clear()
print(num)

set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 8, 9, 0}
union_set = set1.union(set2)
print(union_set)

intersection_set = set1.intersection(set2)
print(intersection_set)

difference_set = set2.difference(set1)
print(difference_set)

symmetric_diff_set = set1.symmetric_difference(set2)
print(symmetric_diff_set)

print(set2.issubset(set1))

print(set1.issubset(set2))
Constructor for many object
************************************************************
class Student:
def __init__(self, roll_no, name, age, grade): # Fixed __init__ here
self.roll_no = roll_no
self.name = name
self.age = age
self.grade = grade

def display(self):
print(f"Roll No: {self.roll_no}")
print(f"Name: {self.name}")
print(f"Age: {self.age}")
print(f"Grade: {self.grade}")
print("-----------------------------")

n = int(input("Enter number of students: "))


students = []

for i in range(n):
print(f"\nEnter data for student {i+1}:")
roll_no = input("Enter roll number: ")
name = input("Enter student name: ")
age = input("Enter the age: ")
grade = input("Enter grade: ")

student = Student(roll_no, name, age, grade)


students.append(student)

print("\n-----Student Information-----")
for s in students:
s.display()
Practicle dic function
************************************************************
my_dict = {
'name': 'Alice ',
'age': 25,
'location': 'New York',
'job': 'Engineer'
}

name = my_dict.get('name')
print(f"Name: {name}")

email = my_dict.get('email', 'Not Available')


print(f"Email: {email}")

keys = my_dict.keys()
print(f"Keys in dictionary: {keys}")

values = my_dict.values()
print(f"Values in dictionary: {values}")

items = my_dict.items()
print(f"Key-Value pairs: {items}")

removed_value = my_dict.pop('location')
print(f"Removed value: {removed_value}")

last_item = my_dict.popitem()
print(f"Last item removed: {last_item}")

my_dict.update({'age': 26, 'email': '[email protected]'})


print(f"Updated dictionary: {my_dict}")

location = my_dict.setdefault('location', 'Unknown')


print(f"Location: {location}")

my_dict.clear()
print(f"Dictionary after clear: {my_dict}")

my_dict = {'name': 'Alice', 'age': 25, 'location': 'New York'}

copy_dict = my_dict.copy()
print(f"Copied dictionary: {copy_dict}")
new_dict = dict.fromkeys(['name', 'age', 'location'], 'Not Provided')
print(f"New dictionary with fromkeys: {new_dict}")

is_name_in_dict = 'name' in my_dict


is_salary_in_dict = 'salary' in my_dict
print(f"Is 'name' in dictionary? {is_name_in_dict}")
print(f"Is 'salary' in dictionary? {is_salary_in_dict}")

print("Iterating through dictionary:")


for key, value in my_dict.items():
print(f"{key}: {value}")
factorial
*******************************
n=int(input("enter a number"))
fact=1
while n>0:
fact=fact*n
n=n-1
print("factorial is ",fact)

Single class and object


*******************************************
class Calculate:
def __init__(self, a, b):
self.a = a
self.b = b

def add(self):
print("Addition :-", self.a + self.b)

num1 = int(input("Enter the 1st no:- "))


num2 = int(input("Enter the 2nd no:- "))
c = Calculate(num1, num2)
c.add()
practicle - module
**********************************************************
📁 File 1: fibonacci_module.py (The Module)
# fibonacci_module.py

def generate_fibonacci(n):
"""Generate a Fibonacci series of n terms."""
if n <= 0:
return []

series = [0]
if n == 1:
return series

series.append(1)
for i in range(2, n):
next_term = series[-1] + series[-2]
series.append(next_term)

return series

📁 File 2: main.py (The Main Script)


# main.py

import fibonacci_module # Import the module

def main():
terms = int(input("Enter the number of terms: "))
series = fibonacci_module.generate_fibonacci(terms)

print(f"Fibonacci series up to {terms} terms:")


for num in series:
print(num, end=" ")

if __name__ == "__main__":
main()

You might also like