Python Exp 02
Python Exp 02
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}
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)
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.
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
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]
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)
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}
my_dict.clear()
print("After clear:", my_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)
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])