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

PRACTICAL1 Merged

Uploaded by

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

PRACTICAL1 Merged

Uploaded by

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

PEN : 220840131131

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:

1.4 Write a Python program to check whether an alphabet is a vowel or


consonant.
Code:
ch = input("Enter a character: ")
if ch in "AEIOUaeiou":
print(ch, "is a Vowel.")
else:
print(ch, "is a Consonant.")
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:

1.6 Write a Python program to check whether the given no is Armstrong or


not using user defined function.
Code:
def isArmstrong(num):
l = len(str(num))
sum = 0
n = num
while n > 0:
digit = n % 10
sum += digit ** l
n //= 10
if num == sum:
return True
else:
return False

Page No.4
PEN : 220840131131

num = int(input("Enter a number: "))


if isArmstrong(num):
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")

Output:

1.7 To write a Python program to find first n prime numbers.


code:
def generate_primes(n):
primes = []
num = 2
while len(primes) < n:
for i in range(2, num):
if (num % i) == 0:
break
else:
primes.append(num)
num += 1
return primes
n = int(input("Enter the value of n: "))
print("First", n, "prime numbers:", generate_primes(n))

Output:

Page No.5
PEN : 220840131131

1.8 Write a Python program to print Fibonacci series upto n terms


Code:
def fibonacci_series(n):
fib_series = []
a, b = 0, 1
for _ in range(n):
fib_series.append(a)
a, b = b, a + b
return fib_series
n = int(input("Enter the number of terms: "))
print("Fibonacci series upto", n, "terms:", fibonacci_series(n))
Output:

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..

Understanding and Using List Methods


Here's a breakdown of essential list methods in Python:

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 :

• Tuples are used to store multiple items in a single variable.

• 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.

• A tuple is a collection which is ordered and unchangeable.

• Tuples are written with round brackets

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

1. clear(): Removes all items from the dictionary.


Synatax : dictionary.clear()
Code :
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.clear()
print(my_dict)
Output :

2. copy(): Returns a shallow copy of the dictionary.


Syntax : new_dict = dictionary.copy()
Code:
original_dict = {'a': 1, 'b': 2, 'c': 3}
new_dict = original_dict.copy()
print(new_dict)
Output :

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 :

5. items(): Returns a list of key-value pairs in the dictionary.


Syntax : items = dictionary.items()
Code :
my_dict = {'a': 1, 'b': 2, 'c': 3}
items = my_dict.items()
print(items)
Output :

6. keys(): Returns a list of keys in the dictionary.


Syntax : keys = dictionary.keys()
Code :
my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = my_dict.keys()
print(keys)
Output :

7. pop(): Removes and returns the value of a specified key.


Syntax : value = dictionary.pop(key, default)
Code :
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.pop('b')
print(value)
Output :

8. popitem(): Removes and returns the last inserted key-value pair.


Syntax : key, value = dictionary.popitem()

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 :

11. values(): Returns a list of values in the dictionary.


Syntax : values = dictionary.values()
Code :
my_dict = {'a': 1, 'b': 2, 'c': 3}
values = my_dict.values()
print(values)
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)

# Adding back the account


account_number = "123456789"
bank[account_number] = {
"bank_name": "SBI",
"holder_name": "Aditya Soni",
"amount": 50000
}

# 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

print("New account:", new_account)

# 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.")

# Adding back the popped item


if bank:
popped_item = bank.popitem()
print("Popped item:", popped_item)

# Adding back the popped item


bank[popped_item[0]] = popped_item[1]

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

You might also like