Practice problems (02/08/2023)

1.) Program to print ASCII values of a character

i/p = a
o/p = 97

i/p = D
o/p = 68

2.) WAP for sum of squares of first n natural numbers

i/p = n = 4 o/p = 30

4^2+ 3^2 + 2^2 + 1^2 = 16+9+4+1 = 30

3.) Program to swap two elements in a list

i/p = [23,65,19,90]
pos = 0
pos1 = 2

o/p = [19,65,23,90]

1 Like

1. Program to print ASCII values of a character

def asci():
print(f’{ord(a)}')
a=input()
asci()

output:
a
97

2. WAP for sum of squares of first n natural numbers

n=int(input())
m=list(map(lambda x:x**2,range(1,n+1)))
s=0
for i in m:
s+=i
print(s)

output:
4
30

3. Program to swap two elements in a list

n=eval(input())
n[0],n[2]=n[2],n[0]
print(n)

output:
[23,65,19,90]
[19, 65, 23, 90]

1 Like

[quote=“mohammedaliparkar342, post:1, topic:8111”]

#1.) Program to print ASCII values of a character

i/p = a

o/p = 97

i/p = D

o/p = 68

str = input(“Enter a letter :”)

for i in str:

print(ord(i))

#2.) WAP for sum of squares of first n natural numbers

i/p = n = 4 o/p = 30

4^2+ 3^2 + 2^2 + 1^2 = 16+9+4+1 = 30

n = int(input())

l = list(map(lambda x:x**2 ,range(1,n+1)))

sum = 0

for i in l:

sum = sum+i

print(sum)

#3.) Program to swap two elements in a list

i/p = [23,65,19,90]

pos = 0

pos1 = 2

o/p = [19,65,23,90]

l = [23,65,19,90]

l[0],l[2] = l[2],l[0]

print(l)]

1 Like

1.) Program to print ASCII values of a character

i/p = a

o/p = 97

i/p = D

o/p = 68

mystr = input("Enter a char : ")

def func(mystr):

return ord(mystr)

print(func(mystr))

Output:

Enter a char : a

97

2.) WAP for sum of squares of first n natural numbers

i/p = n = 4 o/p = 30

4^2+ 3^2 + 2^2 + 1^2 = 16+9+4+1 = 30

def func(num):

x = ((num+1)(2num+1)*(num))/6

return x

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

res = func(num)

print(res)

Output:

Enter a number : 4

30.0

3.) Program to swap two elements in a list

i/p = [23,65,19,90]

pos = 0

pos1 = 2

o/p = [19,65,23,90]

def func(l,a,b):
temp = l[a]
l[a] = l[b]
l[b] = temp
return l

l = [23,65,19,90]

l = eval(input("Enter a list : "))
a = int(input("Enter pos1 : "))
b = int(input("Enter pos2 : "))
print(func(l,a,b))

Output:

Enter a list : [23,65,19,90]

Enter pos1 : 0

Enter pos2 : 2

[19, 65, 23, 90]

1 Like