PRACTICAL1 Merged
PRACTICAL1 Merged
PRACTICAL : - 1
Understand and apply the basics of Python
1.1 Write a Python Program to find those numbers which are divisible by 7
and multiple of 5,between 1500 and 2700
Code:
print( "Numbers divisible by 7 and multiple of 5: ")
for i in range(1500,2700):
if (i%5==0) and (i%7==0):
print(i)
Output:
Page No.1
PEN : 220840131131
1.2 Write a Python program to construct the following pattern, using nested
for loop.
*
**
***
****
*****
****
***
**
*
Code:
for i in range(1,6):
print("* "*i)
for j in range(4,0,-1):
print("* "*j)
Output:
Page No.2
PEN : 220840131131
1.3 Write a Python program that accepts a word from user and reverse it (without using
the reverse function)
Code:
word = input(“enter the word : “)
reverse_word = word[::-1]
print(“The reverse word is “,reverse_word)
Output:
Page No.3
PEN : 220840131131
1.5 Write a Python program to find reverse of given number using user
defined function
Code:
def reverse_number(num):
reversed_num = 0
while num > 0:
remainder = num % 10
reversed_num = (reversed_num * 10) + remainder
num = num // 10
return reversed_num
number = int(input("Enter a number: "))
print("Reverse of the number:", reverse_number(number))
Output:
Page No.4
PEN : 220840131131
Output:
Output:
Page No.5
PEN : 220840131131
Page No.6
PEN:220840131131
PRACTICAL :- 2
Understand and use different methods of List.
Theory of Lists:
• Lists are fundamental data structures in Python.
• They are ordered, mutable collections that can store elements of various data types
(integers, strings, booleans, etc.) within square brackets [].
• Elements are accessed and manipulated using zero-based indexing, where the first
• element has an index of 0, the second has an index of 1, and so on..
1. Declaration:
In Python, you can declare a list using square brackets []. An empty list can be created as well.
Here's an example:
Code:
my_list = [1,2,3,4]
print(my_list)
Output:
2. Append:
The append() method adds an element to the end of the list. It modifies the original list.
Code:
my_list = [1,2,3,4]
my_list.append(5)
print(my_list)
Output:
Page No.7
PEN:220840131131
3. Extend:
The extend() method adds all the elements from an iterable (like another list) to the end of the
current list. It also modifies the original list.
Code:
my_list = [1,2,3,4]
my_list.extend(5,6)
print(my_list)
Output:
4. Index:
The index() method returns the index (position) of the first occurrence of a specified value in
the list.
Code:
my_list = [1,2,3,4]
my_list.index(3)
Output:
5. Count:
The count() method returns the number of elements with a specified value in the list.
Code:
my_list = [1,2,3,4]
my_list.count(3)
Output:
Page No.8
PEN:220840131131
6. Copy
The copy() method returns a shallow copy of the list. A new list is created with the same
elements as the original list. Changes to the copy won't affect the original and vice versa Python
Code:
my_list = [1,2,3,4]
my_list1 = my_list.copy()
my_list1.append(5)
print(my_list)
print(my_list1)
Output:
7. Clear:
The clear() method removes all elements from the list.
Code:
my_list = [1, 2, 3, 4]
my_list.clear()
print(my_list)
Output:
8. Pop:
The pop() method removes and returns the element at a specified index (position) in the list.
By default, it removes the last element (index -1).
Code:
my_list = [1, 2, 3, 4]
removed_element = my_list.pop()
print(my_list)
print(removed_element)
Page No.9
PEN:220840131131
Output:
9. Insert:
The insert() method inserts an element at a specified index in the list. It modifies the original
list.
Code:
my_list = [1,2,3,4]
my_list.insert(2,8)
print(my_list)
Output:R
10. Remove:
The remove() method removes the first occurrence of a specified value from the list. It
modifies the original list
Code:
my_list = [1,2,3,4]
my_list.remove(2)
print(my_list)
Output:
11.Reverse:
The reverse() method reverses the order of elements in the list. It modifies the original list.
Code:
my_list = [1,2,3,4]
my_list.reverse()
print(my_list)
Page No.10
PEN:220840131131
Output:
12. Sort:
The sort() method sorts the elements of the list in ascending order by default. It modifies the
original list. You can use the reverse argument to sort in descending order.
Code:
my_list = [3,2,1,4]
my.list.sort()
print(my_list)
Output:
Page No.11
PEN : 220840131131
PRACTICAL :- 3
Understand and use different methods of Tuple.
Theory of Tuple :
• Tuple is one of 4 built-in data types in Python used to store collections of data, the
other 3 are List, Set, and Dictionary, all with different qualities and usage.
Method Of Tuple :
1. Creating Tuples: You can create tuples using parentheses () and separating elements
with commas ,. For example:
Code :
my_tuple = (1, 2, 3, 4, 5)
2. Accessing Elements: You can access elements in a tuple using indexing. Indexing in
Python starts from 0.
Code :
my_tuple = (1, 2, 3, 4, 5)
Output :
3. Tuple Slicing: Similar to lists, you can slice tuples to extract a subset of elements
Code :.
print(my_tuple[1:4])
Output :
Page No.12
PEN : 220840131131
4. Tuple Concatenation: You can concatenate two tuples using the + operator.
Code :
tuple1 = (1, 2, 3)
tuple2 = ('a', 'b', 'c')
concatenated_tuple = tuple1 + tuple2
print(concatenated_tuple)
Output :
5. Tuple Repetition: You can create a new tuple by repeating an existing tuple using the
* operator.
Code :
repeated_tuple = (1, 2) * 3
print(repeated_tuple)
Output :
6. Tuple Membership: You can check if an element exists in a tuple using the in
operator.
Code :
print(3 in my_tuple)
Output :
7. Tuple Length: You can find the length of a tuple using the len() function.
Code :
print(len(my_tuple))
Output :
Page No.13
PEN : 220840131131
8. Count Occurrences: You can count the number of occurrences of a particular element
in a tuple using the count() method.
Code :
print(my_tuple.count(3))
Output :
9. Find Index of an Element: You can find the index of the first occurrence of a
particular element in a tuple using the index() method
Code :
print(my_tuple.index(3))
Output :
10.Tuple Packing and Unpacking: Tuples can be packed and unpacked. Packing is
placing values into a new tuple, and unpacking is extracting values into individual
variables.
Code :
# Packing
my_packed_tuple = 1, 2, 3
# Unpacking
a, b, c = my_packed_tuple
print(a)
print(b)
print(c)
Output :
Page No.14
PEN : 220840131131
PRACTICAL :- 4
Understand and use different methods of Dictionaries.
Theory of Dictonaries :
• Python dictionary methods is collection of Python functions that operates on
Dictionary.
• Python Dictionary is like a map that is used to store data in the form of a keyvalue pair.
• Python provides various built-in functions to deal with dictionaries
.Method Of Dictonary
3. fromkeys(): Creates a new dictionary with keys from a sequence and values set to a
specified value.
Syntax : new_dict = dict.fromkeys(sequence, value)
Code :
keys = ['a', 'b', 'c']
new_dict = dict.fromkeys(keys, 0)
print(new_dict)
Output :
Page NO.15
PEN : 220840131131
4. get(): Returns the value of a specified key. If the key does not exist, it returns None or a
specified default value
Syntax : value = dictionary.get(key, default=None)
Code :
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.get('a')
print(value)
Output :
Page NO.16
PEN : 220840131131
Code :
my_dict = {'a': 1, 'b': 2, 'c': 3}
key, value = my_dict.popitem()
print(key)
Output :
9. setdefault(): Returns the value of a specified key. If the key does not exist, it inserts the
key with a specified value.
Syntax : value = dictionary.setdefault(key, default)
Code :
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.setdefault('d', 4)
print(value)
Output :
10. update(): Updates the dictionary with the key-value pairs from another dictionary or an
iterable of key-value pairs.
Syntax : dictionary.update(iterable)
Code :
my_dict = {'a': 1, 'b': 2}
my_dict.update({'c': 3, 'd': 4})
print(my_dict)
Output :
Page NO.17
PEN : 220840131131
PROGRAM
Code :
bank = {}
# Adding an account
account_number = "123456789"
bank[account_number] = {
"bank_name": "SBI",
"holder_name": "Aditya Soni",
"amount": 50000
}
# clear()
bank.clear()
print("After clear:", bank)
# copy()
bank_copy = bank.copy()
print("Copy of bank:", bank_copy)
# fromkeys()
keys = ["account_number", "bank_name", "holder_name", "amount"]
default_value = "N/A"
new_account = dict.fromkeys(keys, default_value)
Page NO.18
PEN : 220840131131
# get()
print("Holder name for account 123456789:", bank.get("123456789").get("holder_name"))
# items()
print("Items in bank dictionary:", bank.items())
# keys()
print("Keys in bank dictionary:", bank.keys())
# pop()
popped_value = bank.pop("123456789")
print("Popped value:", popped_value)
# popitem()
if bank:
popped_item = bank.popitem()
print("Popped item:", popped_item)
else:
print("The dictionary is empty. Cannot pop an item.")
Page NO.19
PEN : 220840131131
else:
print("The dictionary is empty. Cannot pop an item.")
# setdefault()
print("Value for key '987654321':", bank.setdefault("987654321", {"bank_name": "HDFC",
"holder_name": "Alice", "amount": 20000}))
# update()
bank.update({"987654321": {"bank_name": "HDFC", "holder_name": "Bob", "amount":
30000}})
print("Updated bank dictionary:", bank)
# values()
print("Values in bank dictionary:", bank.values())
Output :
Page NO.20