0% found this document useful (0 votes)
9 views5 pages

Class 24 8

The document contains solutions to programming problems involving lists, dictionaries, and conditional statements in Python. For lists, it includes programs to interchange the first and last element, check if an element exists, find the smallest number, count even and odd numbers, and perform list slicing. For dictionaries, it has programs to sort by value, find the sum of items, extract keys if present in both dictionary and list, and count key-value pairs. For conditional statements, it provides programs to display a multiplication table, compute a series sum, swap a string's case, display non-multiple elements, and reverse a list.

Uploaded by

Pran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
9 views5 pages

Class 24 8

The document contains solutions to programming problems involving lists, dictionaries, and conditional statements in Python. For lists, it includes programs to interchange the first and last element, check if an element exists, find the smallest number, count even and odd numbers, and perform list slicing. For dictionaries, it has programs to sort by value, find the sum of items, extract keys if present in both dictionary and list, and count key-value pairs. For conditional statements, it provides programs to display a multiplication table, compute a series sum, swap a string's case, display non-multiple elements, and reverse a list.

Uploaded by

Pran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

In-Class Assignment 1

G Pranav - CB.EN.U4AIE22016
1. Python program to interchange first and last element in a list
#1

list = [1,2,3,4,5]
list[0],list[-1] = list[-1],list[0]
print(list)

[5, 2, 3, 4, 1]

1. Check if element exists in a list


#2

element = 5
list = [1,2,3,4,5]

if element in list:
print("Yeah")
else:
print("Nah")

Yeah

1. Find smallest number in a list


#3

list = [1,2,3,-4,5]
print(min(list))

-4

1. Count even and odd numbers in a list


#4

list = [1,2,3,4,5]
even,odd = 0,0

for i in list:
if i%2 == 0:
even+=1
else:
odd+=1
print(f"There are {even} numbers and {odd} odd numbers")

There are 2 numbers and 3 odd numbers

1. List slicing: get first 4 elements in a list


#5

list = [1,2,3,4,5]
print(list[0:4])

[1, 2, 3, 4]

1. Write a program to add two lists index wise


#6

list1 = ["M", "na", "i", "Pra"]


list2 = ["y", "me", "s", "nav", "hi"]
minlength = min(len(list1),len(list2))
list3 = list1 + list2
list4 = []
for i in range(minlength):
list4.append(list1[i] + list2[i])

if len(list1) > len(list2):


list4 += list1[i+1::]
else:
list4 += list2[i+1::]

print(list3)
print(list4)

['M', 'na', 'i', 'Pra', 'y', 'me', 's', 'nav', 'hi']


['My', 'name', 'is', 'Pranav', 'hi']

Dictionary Questions
1. Write a python program to sort a dictionary by value
#1

dict = {1:"H", 2:"Y", 3:"A"}


keys, items, dict1_ascending, dict1_descending, dict2_ascending,
dict2_descending = [],[],{},{},{},{}
for key,value in dict.items():
keys.append(key)
items.append(value)
keys = sorted(keys)
items = sorted(items)

for key,value in zip(keys,items):


dict1_ascending[key] = value
for key,value in zip(keys[::-1],items):
dict1_descending[key] = value

for item in items:


for key in keys:
if dict[key] == item:
dict2_ascending[key] = item
items = sorted(items,reverse=True)
for item in items:
for key in keys:
if dict[key] == item:
dict2_descending[key] = item

print(dict1_ascending)
print(dict1_descending)
print()
print(dict2_ascending)
print(dict2_descending)

{1: 'A', 2: 'H', 3: 'Y'}


{3: 'A', 2: 'H', 1: 'Y'}

{3: 'A', 1: 'H', 2: 'Y'}


{2: 'Y', 1: 'H', 3: 'A'}

1. Find the sum of items in a dictionary


#2

dict = {1:1, 2:2, 3:3}


sum = 0

for i in dict.values():
sum = sum + i

sum

1. Extract keys value if key present in list and dictionary


#3

dict = {1:"H", 2:"Y", 3:"A"}


list = [2,3]
for key in dict.keys():
if key in dict and key in list:
print(dict[key])

Y
A

1. Find the count of each key, value, and key value pair
#4

dict = {1:1, 2:2, 3:3, 4:4, 5:1, 6:2}


freqency_dict = {}

for value in sorted(dict.values()):


if value in freqency_dict.keys():
freqency_dict[value]+=1
else:
freqency_dict[value] = 1

freqency_dict

{1: 2, 2: 2, 3: 1, 4: 1}

Conditional Statement exercises


Q1. Write a program to accept an integer and display its multiplication table until 10.

n = int(input("Enter number: "))


for i in range(1,11):
print(n*i)

4
8
12
16
20
24
28
32
36
40

Q2. Write a program to compute the sum of the series: 1+𝑥+𝑥^2+…+𝑥^𝑛 Let the user input x
and n.
x = int(input("Enter x: "))
n = int(input("Enter n: "))
sum = 0

for i in range(n+1):
sum += (x**i)

print(sum)

Q3. Write a program to swap the case of a string.

s = "StRinG"
s1 = ""
for i in s:
if ord(i) >= 65 and ord(i) <= 90:
i = chr(ord(i) + 32)
else:
i = chr(ord(i) - 32)
s1+=i;

s1

'sTrINg'

Q4. Write a program to display the elements and their locations which are not multiples of 2 or
3. The list must be user input.

list = eval(input("Enter list: "))

for i in range(len(list)):
if not (list[i]%2 == 0 or list[i]%3 == 0):
print (f"{list[i]} at position {i}")

1 at position 0
5 at position 3
7 at position 5

Q5. Write a program to reverse a user-input list.

list = eval(input("Enter list: "))

print(list[::-1])

[7, 6, 5, 4, 3, 2, 1]

You might also like