Python Lab Manual 314004 1-15 practical_011518
Python Lab Manual 314004 1-15 practical_011518
Download PyCharm:
https://www.jetbrains.com/pycharm/download/#section=windows .
2. State the steps involved in executing the program using Script Mode.
Executing a Python program in script mode involves the following steps:
• Write the Code:
Create a text file using a text editor or an IDE and write your Python code. Save the file
with a .py extension, which signifies a Python script.
• Open a Terminal or Command Prompt:
Navigate to the directory where you saved your Python script using the cd command.
• Execute the Script:
Use the python command followed by the name of your script file.
For example: python myscript.py
• View Output: The Python interpreter will execute your script, and any output produced by
the script will be displayed in the terminal or command prompt window.
https://www.youtube.com/watch?v=swmxFmfHNDY
Bitwise Operators in Python | Right-shift, Left-shift, AND, OR, NOT, XOR | Python
https://www.youtube.com/watch?v=Yk3Jwm5YuFs
2. Write a program to check the largest number among the three numbers
Membership Operators:
• in: Returns True if a value is found in a sequence (e.g., list, tuple, string)
• not in: Returns True if a value is not found in a sequence
Identity Operators:
• is: Returns True if two variables refer to the same object
• is not: Returns True if two variables do not refer to the same object
6. Write a program that takes the marks of 5 subjects and displays the grades.
1. Write a Python program that takes a number and checks whether it is a palindrome or not.
2. Write a Python program to print all even numbers between 1 to 100 using while loop.
4. Change the following Python code from using a while loop to for loop:
x=1
while x<10:
print x
x+=1
def my_empty_function():
pass # Placeholder for future implementation
my_empty_function()
• reverse: An optional argument. If set to True, the list is sorted in descending order. By
default, it is False, which sorts the list in ascending order.
Example :
my_list = [3, 1, 4, 1, 5, 9]
# Using core functions
print(len(my_list)) # Output: 6
print(sorted(my_list)) # Output: [1, 1, 3, 4, 5, 9]
print(min(my_list)) # Output: 1
print(max(my_list)) # Output: 9
# Using list methods
my_list.append(2)
print(my_list) # Output: [3, 1, 4, 1, 5, 9, 2]
my_list.insert(2, 7)
print(my_list) # Output: [3, 1, 7, 4, 1, 5, 9, 2]
my_list.remove(1) # Removes the first occurrence of 1
print(my_list) # Output: [3, 7, 4, 1, 5, 9, 2]
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4, 5, 7, 9]
my_list.reverse()
print(my_list) # Output: [9, 7, 5, 4, 3, 2, 1]
The & operator or intersection() method is the most efficient way to find common elements
between two lists.
2. Write syntax to copy specific elements existing tuple into new tuple.
original_tuple = (10, 20, 30, 40, 50)
indices_to_copy = [0, 2, 4]
new_tuple = tuple(original_tuple[i] for i in indices_to_copy)
print(new_tuple) # Output: (10, 30, 50)
Note :
new_tuple = tuple(original tuple[i] for i in indices_to_copy):
• This is the core of the code. It's a list comprehension:
o for i in indices_to_copy: Iterates through each index in the indices_to_copy list.
o original_tuple[i]: Accesses the element at the current index i within the original_tuple.
• The resulting list of selected elements is then converted into a tuple using the tuple()
constructor.
4. Create a tuple and find the minimum and maximum number from it.
6. Print the number in words for Example: 1234 => One Two Three Four
Practical No. 10: Write python program to perform following operations on the
Set: Create set, Access Set, Update Set, Delete Set
A set is an unordered collection of items. Every element is unique (no duplicates) and must be
immutable (which cannot be changed). A set is created by placing all the items (elements) inside
curly braces {}, separated by comma or by using the built-in function set(). It can have any number
of items and they may be of different types (integer, float, tuple, string etc.).
• Intersection: Returns common elements present in both sets. It can be performed using
the & operator or the intersection() method.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2 # or set1.intersection(set2)
# intersection_set will be {3}
• Difference: Returns elements present in the first set but not in the second set. It can be
performed using the - operator or the difference() method.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1 - set2 # or set1.difference(set2)
# difference_set will be {1, 2}
• Symmetric Difference: Returns elements present in either the first set or the second set, but
not in both. It can be performed using the ^ operator or the symmetric_difference() method.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1 ^ set2
# symmetric_difference_set will be {1, 2, 4, 5}
• isdisjoint(): Checks if two sets have no elements in common. Returns True if they are
disjoint, False otherwise.
set1 = {1, 2, 3}
set2 = {4, 5, 6}
disjoint_check = set1.isdisjoint(set2)
# disjoint_check will be True
These set operations offer efficient ways to manipulate and analyze data stored in sets, leveraging
their inherent properties of uniqueness and unordered nature.
3. Write a Python program to create a set, add member(s) in a set and remove one item
from set.
4. Write a Python program to find maximum and the minimum value in a set.
Practical No. 11: Write python program to perform following functions on Set:
Union, Intersection, Difference, Symmetric Difference
Practical No. 12: Write python program to perform following operations on the
Dictionary: Create, Access, Update, Delete, Looping through Dictionary, Create
Dictionary from list
Python dictionary is a container of key-value pairs. It is mutable and can contain mixed types. A
dictionary is an unordered collection. Python dictionaries are called associative arrays or hash
tables in other languages. The keys in a dictionary must be immutable objects like strings or
numbers. They must also be unique within a dictionary.
https://sparkbyexamples.com/python/how-to-sort-dictionary-by-value-in-python/
2. Write a Python script to concatenate following dictionaries to create a new one.
Sample Dictionary: dic1 = {1:10, 2:20} dic2 = {3:30, 4:40} dic3 = {5:50,6:60}
https://www.w3resource.com/python-exercises/dictionary/python-data-type-dictionary-
exercise-19.php
3. Write a Python program to perform following operations on set: intersection of sets, union
of sets, set difference, symmetric difference, clear a set.
See in SET practical
4. Write a Python program to combine two dictionary adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
Practical No. 13: Write a user define function to implement following features:
Function without argument, Function with argument, Function returning value
Functions are the most important aspect of an application. A function can be defined as the
organized block of reusable code which can be called whenever required.
1. Write a Python function that takes a number as a parameter and check the number is prime
or not. For more practice : https://www.w3resource.com/python-exercises/
3. Write a Python function to calculate the factorial of a number (a non-negative integer). The
function accepts the number as an argument.
4. Write a Python function that accepts a string and calculate the number of upper case letters
and lower case letters.
Practical No. 14: Write a user define function to implement for following problem:
Function positional/required argument, Function with keyword argument,
Function with default argument, Function with variable length argument
numbers = [1, 2, 3, 4, 5]
result = reduce(multiply, numbers)
print(result)