0% found this document useful (0 votes)
14 views6 pages

Python Exp 02

The document contains a series of Python programming exercises demonstrating various functionalities such as finding even and odd numbers, calculating factorials, checking for prime numbers, and performing file handling operations. It also includes examples of using list, set, and dictionary methods, as well as functional programming concepts like lambda, filter, map, and reduce. Each exercise is accompanied by code snippets and their respective outputs.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Python Exp 02

The document contains a series of Python programming exercises demonstrating various functionalities such as finding even and odd numbers, calculating factorials, checking for prime numbers, and performing file handling operations. It also includes examples of using list, set, and dictionary methods, as well as functional programming concepts like lambda, filter, map, and reduce. Each exercise is accompanied by code snippets and their respective outputs.

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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

practical :2

1.WAP to find even and odd number from given list using function
programe:
def find_even_odd(numbers):
return [n for n in numbers if n % 2 == 0], [n for n in numbers if n % 2 != 0]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even, odd = find_even_odd(numbers)
print("Even numbers:", even)
print("Odd numbers:", odd)

output:
Even numbers: [2, 4, 6, 8, 10]
Odd numbers: [1, 3, 5, 7, 9]

2.WAP to find factorial of each element in list and provide dictionary of element
and its factorial using function
programe:
import math
def factorial_dict(numbers):
return {n: math.factorial(n) for n in numbers}
numbers = [1, 2, 3, 4, 5]
result = factorial_dict(numbers)
print(result)

output:
{1: 1, 2: 2, 3: 6, 4: 24, 5: 120}

3.WAP to check whether given number is prime or not using function


programe:
def is_prime(n):
return n > 1 and all(n % i != 0 for i in range(2, int(n ** 0.5) + 1))
num = int(input("Enter a number: "))
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")

output:
Enter a number: 86
86 is not a prime number.

4.create three lists a list name , a list of age and a list of salaries ,generate
a print a list of tuple containinng name,age,salary from the three lists.from this
list genrete three tuples one containing all name another containin all age and
thord contaninig all salaries
programe:
names = ["varun"," karan","pratik","aryan",'prasanna']
ages = [25, 30, 35, 40, 45]
salaries = [50000, 60000, 70000, 80000, 90000]
employee_data = list(zip(names, ages, salaries))
print("List of tuples (name, age, salary):")
print(employee_data)
names = tuple(names)
ages = tuple(ages)
salaries = tuple(salaries)
print("\nTuple of names:", names)
print("Tuple of ages:", ages)
print("Tuple of salaries:", salaries)

output:
List of tuples (name, age, salary):
[('varun', 25, 50000), ('karan', 30, 60000), ('pratik', 35, 70000), ('aryan', 40,
80000), ('prasanna', 45, 90000)]
Tuple of names: ('varun', 'karan', 'pratik', 'aryan', 'prasanna')
Tuple of ages: (25, 30, 35, 40, 45)
Tuple of salaries: (50000, 60000, 70000, 80000, 90000)

5.WAP to understand different file handaling operations


programe:
# 1. Writing to a file (creating a file or overwriting an existing one)
with open("example.txt", "w") as file:
file.write("Hello, this is a sample file!\n")
file.write("File handling operations in Python are easy.\n")
# 2. Reading from a file
with open("example.txt", "r") as file:
content = file.read()
print("File Content (Read):")
print(content)
# 3. Appending to a file (adding content without overwriting)
with open("example.txt", "a") as file:
file.write("This line is appended to the file.\n")
# 4. Reading the updated file
with open("example.txt", "r") as file:
updated_content = file.read()
print("Updated File Content (Read):")
print(updated_content)
# 5. Reading specific lines from a file (reading line by line)
with open("example.txt", "r") as file:
print("Reading file line by line:")
for line in file:
print(line.strip())
# 6. Writing to a binary file
data = b"Hello, this is a binary file.\n"
with open("example_binary.bin", "wb") as file:
file.write(data)
# 7. Reading from a binary file
with open("example_binary.bin", "rb") as file:
binary_content = file.read()
print("Binary File Content (Read):")
print(binary_content)

output:
File Content (Read):
Hello, this is a sample file!
File handling operations in Python are easy.
Updated File Content (Read):
Hello, this is a sample file!
File handling operations in Python are easy.
This line is appended to the file.

Reading file line by line:


Hello, this is a sample file!
File handling operations in Python are easy.
This line is appended to the file.
Binary File Content (Read):
b'Hello, this is a binary file.\n'

6.WAP which uses lambda ,filter ,map and reduce function


programe
from functools import reduce
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers:", even_numbers)
squared_numbers = list(map(lambda x: x ** 2, numbers))
print("Squared numbers:", squared_numbers)
product_of_numbers = reduce(lambda x, y: x * y, numbers)
print("Product of all numbers:", product_of_numbers)

output:
Even numbers: [2, 4, 6, 8, 10]
Squared numbers: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Product of all numbers: 3628800

7.WAP to perform list method using function


append, clear,copy ,count, extend, index,insert,pop,remove,reserve,sort
programe:
def list_operations():
my_list = [1, 2, 3, 4, 5]

my_list.append(6)
print("After append:", my_list)

my_list.clear()
print("After clear:", my_list)

my_list = [1, 2, 3, 4, 5]
copied_list = my_list.copy()
print("After copy:", copied_list)

count_of_3 = my_list.count(3)
print("Count of 3:", count_of_3)

my_list.extend([6, 7, 8])
print("After extend:", my_list)

index_of_4 = my_list.index(4)
print("Index of 4:", index_of_4)

my_list.insert(2, 9)
print("After insert:", my_list)

popped_element = my_list.pop()
print("After pop:", my_list, "Popped element:", popped_element)

my_list.remove(9)
print("After remove:", my_list)

my_list.reverse()
print("After reverse:", my_list)

my_list.sort()
print("After sort:", my_list)

list_operations()

output:
After append: [1, 2, 3, 4, 5, 6]
After clear: []
After copy: [1, 2, 3, 4, 5]
Count of 3: 1
After extend: [1, 2, 3, 4, 5, 6, 7, 8]
Index of 4: 3
After insert: [1, 2, 9, 3, 4, 5, 6, 7, 8]
After pop: [1, 2, 9, 3, 4, 5, 6, 7] Popped element: 8
After remove: [1, 2, 3, 4, 5, 6, 7]
After reverse: [7, 6, 5, 4, 3, 2, 1]
After sort: [1, 2, 3, 4, 5, 6, 7]

8. WAP to perform set method using function


add,clear,copy,difference ,discard,inttersection,pop,remove,symmetric_difference,u
nion,update
programe:
def set_operations():
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

set1.add(6)
print("After add:", set1)

set1.clear()
print("After clear:", set1)

set1 = {1, 2, 3, 4, 5}
copied_set = set1.copy()
print("After copy:", copied_set)

difference_set = set1.difference(set2)
print("Difference (set1 - set2):", difference_set)

set1.discard(3)
print("After discard (remove 3 if exists):", set1)

intersection_set = set1.intersection(set2)
print("Intersection:", intersection_set)

set1.pop()
print("After pop:", set1)

set1.remove(2)
print("After remove (remove 2):", set1)

symmetric_diff = set1.symmetric_difference(set2)
print("Symmetric Difference:", symmetric_diff)

union_set = set1.union(set2)
print("Union:", union_set)

set1.update([10, 11, 12])


print("After update:", set1)

set_operations()

output:
After add: {1, 2, 3, 4, 5, 6}
After clear: set()
After copy: {1, 2, 3, 4, 5}
Difference (set1 - set2): {1, 2, 3}
After discard (remove 3 if exists): {1, 2, 4, 5}
Intersection: {4, 5}
After pop: {2, 4, 5}
After remove (remove 2): {4, 5}
Symmetric Difference: {6, 7, 8}
Union: {4, 5, 6, 7, 8}
After update: {4, 5, 10, 11, 12}

9.WAP to perform dictionary method using function


clear,copy, fromkey,get,items,keys,pop,popitem,setdefult,update,values
programe:
def dictionary_operations():
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

my_dict.clear()
print("After clear:", my_dict)

my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}


copied_dict = my_dict.copy()
print("After copy:", copied_dict)

new_dict = dict.fromkeys(['x', 'y', 'z'], 0)


print("After fromkeys:", new_dict)

value_of_b = my_dict.get('b')
print("Value of 'b' using get:", value_of_b)

items = my_dict.items()
print("Items:", items)
keys = my_dict.keys()
print("Keys:", keys)

popped_value = my_dict.pop('a')
print("After pop ('a'):", my_dict, "Popped value:", popped_value)

popped_item = my_dict.popitem()
print("After popitem:", my_dict, "Popped item:", popped_item)

my_dict = {'a': 1, 'b': 2}


default_value = my_dict.setdefault('c', 3)
print("After setdefault:", my_dict, "Returned value:", default_value)

my_dict.update({'d': 4, 'e': 5})


print("After update:", my_dict)

values = my_dict.values()
print("Values:", values)

dictionary_operations()

output:
After clear: {}
After copy: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
After fromkeys: {'x': 0, 'y': 0, 'z': 0}
Value of 'b' using get: 2
Items: dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
Keys: dict_keys(['a', 'b', 'c', 'd'])
After pop ('a'): {'b': 2, 'c': 3, 'd': 4} Popped value: 1
After popitem: {'b': 2, 'c': 3} Popped item: ('d', 4)
After setdefault: {'a': 1, 'b': 2, 'c': 3} Returned value: 3
After update: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
Values: dict_values([1, 2, 3, 4, 5])

You might also like