You are on page 1of 10

ASSIGNMENT-1

ENROLL-2019BITE045

TOPIC : BASICS OF PYTHON

Q1: WAP for cube sum of first n natural numbers.


CODE:
def CubeSum(n):
sum=0;
for i in range(n+1):
sum+=i**3
return sum
n=int(input("Enter the number whose cube you want to find"));
print("Sum:"+str(CubeSum(n)))

Output:
Q2.WAP to swap two numbers without using third variable.

CODE:
a=int(input("Enter first number"))
b=int(input("Enter second number"))

a,b=b,a
print("Numbers after swapping are:"+str(a)+","+str(b))

OUTPUT:

Q3.WAP to find sum of array.

CODE:
import array as ar
a=ar.array('i',[21,14,17,90])
print("sum of array is:",sum(a))
OUTPUT:

Q4.WAP for vertical concatenation in matrix.


from itertools import zip_longest

my_list = [["Hi", "Ron"], ["how", "are"], ["you"]]

print("The list is : ")


print(my_list)

my_result = ["".join(e) for e in zip_longest(*my_list, fillvalue ="")]

print("The list after concatenating the column is : ")


print(my_result)

OUTPUT:
Q5.WAP to replace multiple words with K.
CODE:
test_string = 'Welcome to the world of python '
print("The original string is : " + str(test_string))

word_list = ["Welcome", 'world', 'python']


replace_word = 'k'
replaced_string = ' '.join([replace_word if i in word_list else i for i in
test_string.split()])

print("String after multiple replace : " + str(replaced_string))

OUTPUT:
Q6.WAP to replace all occurrences of a substring in a string.

CODE:
string_a = "The brown coloured car is actually owned by a brown-eyed man."
string_b = string_a.replace("brown", "blue")
print(string_a)
print(string_b)

OUTPUT:
Q7.Sort python Dictionaries by Key or Value.

CODE:
def dictionairy():

key_value = {}

key_value[2] = 56
key_value[1] = 2
key_value[5] = 12
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323

print("Task 2:-\nKeys and Values sorted in",


"alphabetical order by the key ")

for i in sorted(key_value):
print((i, key_value[i]), end=" ")

def main():

dictionairy()

if __name__ == "__main__":
main()
OUTPUT:

Q8.WAP to remove key from dictionary.

CODE:
test_dict = {"Aru": 25, "Anu": 21, "Mani": 21, "Haritha": 21}

print("The dictionary before performing remove is : " + str(test_dict))


del test_dict['Mani']

print("The dictionary after remove is : " + str(test_dict))

OUTPUT:
Q9.Sort python dictionaries by key or value.
CODE:
def dictionairy():

key_value = {}

key_value[2] = 562
key_value[1] = 2
key_value[5] = 1
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323

print("Task 3:-\nKeys and Values sorted",


"in alphabetical order by the value")

print(sorted(key_value.items(), key=
lambda kv: (kv[1], kv[0])))

def main():

dictionairy()

if __name__ == "__main__":
main()

OUTPUT:
Q10.Check if binary representation of two numbers is
anagram.
CODE:
SIZE = 8

def bit_anagram_check(a, b):

global size

i = 0
binary_a = [0] * SIZE
while (a > 0):
binary_a[i] = a % 2
a //= 2
i += 1

j = 0
binary_b = [0] * SIZE
while (b > 0):
binary_b[j] = b % 2
b //= 2
j += 1

binary_a.sort()
binary_b.sort()

for i in range(SIZE):
if (binary_a[i] != binary_b[i]):
return 0
return 1
if __name__ == "__main__":
a = 8
b = 4
print(bit_anagram_check(a, b))

OUTPUT:

You might also like