You are on page 1of 3

Python Programming Assignments

1. Write a Python program to find whether a given array of integers contains any duplicate element. Return
true if any value appears at least twice in the said array and return false if every element is distinct

Answer Code:-

def test_duplicate(array_nums):

nums_set = set(array_nums)

return len(array_nums) != len(nums_set)

print(test_duplicate([1,2,3,4,5]))

>>> True

print(test_duplicate([1,2,3,4, 4]))

>>> False

2. Given a list in Python and provided the positions of the elements, write a program to swap the two
elements in the list

Answer Code:-

def swapPositions(list, pos1, pos2):

    list[pos1], list[pos2] = list[pos2], list[pos1]

    return list

# Running function

List = [23, 65, 19, 90]

pos1, pos2  = 1, 3 

print(swapPositions(List, pos1-1, pos2-1))

3. Write a Python program to sort a tuple by its float element. Sample data: [('item1', '12.20'), ('item2',
'15.10'), ('item3', '24.5')] Expected Output: [('item3', '24.5'), ('item2', '15.10'), ('item1', '12.20')]

Answer Code:-

def Sort(tup):

    return(sorted(tup, key = lambda x: float(x[1]), reverse = True))


# Driver Code

tup = [('item1', '12.20'), ('item2', '15.10'), ('item3', '24.5')]

print(Sort(tup))

Output:-

[('item3', '24.5'), ('item2', '15.10'), ('item1', '12.20')]

4. Create a Python class called BankAccount which represents a bank account, having as attributes:
accountNumber (numeric type), name (name of the account owner as string type), balance.
Create a constructor with parameters: accountNumber, name, balance. Create a Deposit() method which
manages the deposit actions.
Create a Withdrawal() method which manages withdrawals actions.
Create an bankFees() method to apply the bank fees with a percentage of 5% of the balance account.
Create a display() method to display account details. Give the complete code for the BankAccount class

Answer Code:-

class BankAccount:

def __init__(self,accountNumber, name, balance):

self.accountNumber = accountNumber

self.name = name

self.balance = balance

def Deposit(self , d ):

self.balance = self.balance + d

def Withdrawal(self , w):

if(self.balance < w):

print("Opps! Insufficient balance !")

else:

self.balance = self.balance - w

def bankFees(self):
self.balance = (95/100)*self.balance

def display(self):

print("Account Number : " , self.accountNumber)

print("Account Name : " , self.name)

print("Account Balance : " , self.balance , " $")

You might also like