You are on page 1of 5

Assignment-1

Name-Utkarsh Singh

Roll No.-1900520130058

Branch-IT(4th Sem.)

1. Given a list, iterate it, and display numbers divisible by five, and if you find a
number greater than 150, stop the loop iteration.

Code:

n=int(input('Enter number of elements in the list:'))

l=list(map(int,input().strip().split()))

for i in l:

if i==150:

break;

if i%5==0:

print(i)

2. Create an inner function to calculate the addition in the following way

i) Create an outer function that will accept two parameters, a and b.


ii) Create an inner function inside an outer function that will calculate the addition
of a and b.
iii) At last, an outer function will add 5 into addition and return it.

Code:
def outer_function(a,b):

c=0

def inner_function(a,b):

return a+b

c=inner_function(a,b)

return c+5
a=int(input())

b=int(input())

print(outer_function(a, b))

3. Given a list slice it into 3 equal chunks and reverse each chunk.

Code:

def divide_chnks(l,n):

for i in range(0,n,3):

yield l[i:i+3][::-1]

n=int(input('Enter number of elements in the list:'))

l=list(map(int,input().strip().split()))

x=list(divide_chnks(l, n))

print(x)

4. Given two strings, s1 and s2, create a new string by appending s2 in the middle of
s1.

Code:

s1=input("Enter 1st string: ")

s2=input("enter 2nd string: ")

n=len(s1)//2

s3=s1[:n]+s2+s1[n:]

print("Resulting String : ",end=" ")

print(s3)

5. Convert decimal number to octal using print() output formatting.

Code:

def dectoOct(decimal):
if(decimal > 0):

dectoOct((int)(decimal/8))

print(decimal%8, end='')

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

print(decimal)

print("Octal: ", end='')

dectoOct(decimal)

6. Merge two Python dictionaries into one.

Code:

def Merge(dict1, dict2):

return(dict1.update(dict2))

dict1 = {'a': 10, 'b': 8}

dict2 = {'d': 6, 'c': 4}

Merge(dict1, dict2)

print(dict1)

7. Counts the number of occurrences of item 50 from a tuple.


tuple1 = (50, 10, 60, 70, 50)

Code:

tuple1 = (50, 10, 60, 70, 50)

print(tuple1)

print("Occurrences of 50 in tuple: ",end=" ")

print(tuple1.count(50))

8. Generate 6-digit random secure OTP.

Code:

import random
otp = random.randint(100000, 999999)

print("OTP:", otp)

9. Generate a random Password which meets the following conditions

i) Password length must be 10 characters long.


ii) It must contain at least 2 upper case letters, 1 digit, and 1 special symbol.

Code:
import math,random

def generateOTP() :

digits = "0123456789"

specialsymbol="@#$%&!"

uppercase_letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

all1=digits+specialsymbol+uppercase_letters

password=""

for i in range(6) :

password += all1[math.floor(random.random() * 10)]

for i in range(2) :

password += uppercase_letters[math.floor(random.random() * 10)]

for i in range(1) :

password += digits[math.floor(random.random() * 10)]

for i in range(1) :

password += specialsymbol[math.floor(random.random() * 10)]

return password

x=generateOTP()

print("Random password: ",end=" ")

print(x)

10. Return array of odd rows and even columns from given numpy array.
Code:
Import numpy
arr=numpy.array([[3, 6, 9, 12], [15, 18, 21, 24], [27, 30, 33, 36], [39, 42, 48], [51, 54, 57, 60]])
print(“Printing input array”)
print(arr)
print(“Printing array of odd rows & even columns”)
newArr=arr[::2, 1::2]
print(newArr)

You might also like