IAT-1 Workbook P3-Python
IAT-1 Workbook P3-Python
1. Write a python program to find the better of two test average marks out of three
test’s marks accepted from the user.
Test case 1:
20
15
22
Output:
21.0
In [2]:
test1=int(input())
test2=int(input())
test3=int(input())
if(test1<test2 and test1<test3):
avg=(test2+test3)/2
elif(test2<test1 and test2<test3):
avg=(test1+test3)/2
else:
avg=(test1+test2)/2
print(avg)
30
30
30
30.0
2. Develop a Python program to check whether a given number is palindrome or not and
also count the number of occurrences of each digit in the input number.
Input: 12321
Output:Palindrome
22122
Input: 12982221
Output:Not Palindrome
24114442
In [6]:
45232223254
Palindrome
22525552522
3. Write a Python Program to form an Integer that has the Number of Digits at Ten’s
Place and the Least Significant Digit of the Entered Integer at One’s Place
Test case 1:
enter number:129
Output:39
Test case 2:
enter number:24678
Output:58
In [7]:
num=input()
L=len(num)
LSB=num[-1]
print(L*10+int(LSB))
129
39
In [11]:
def is_prime(N):
for i in range(2,(N//2)+1):
if(N%i==0):
return False
return True
##################################
list1=input().split()
low=int(list1[0])
high=int(list1[1])
for num in range(low,high+1):
if(is_prime(num)):
print(num,end=", ")
2 70
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
In [ ]:
In [12]:
L=int(input())
list1=input().split()
list2=[]
for word in list1:
if(len(word)>L):
list2.append(word)
print(list2)
5
python loosely typed language
['python', 'loosely', 'language']
In [ ]:
In [14]:
st1=input()
st2=""
for ch in st1:
if(st1.count(ch)>1 and (ch not in st2)):
st2=st2+ch
print(st2)
hellooo
lo
In [19]:
11111110
254
In [24]:
254
11111110
In [26]:
100
0x64
In [29]:
0xff
255
In [ ]:
In [32]:
st=input()
words=0
digits=0
up=0
low=0
for ch in st:
if(ch==" "):
words+=1
elif(ch.isdigit()):
digits+=1
elif(ch.islower()):
low+=1
elif(ch.isupper()):
up+=1
print(words+1,digits,up,low)
In [ ]:
Sample Input:
Python Exercises
Python Exercise
Output
0.967741935483871
In [35]:
st1=input().lower()
st2=input().lower()
L1=len(st1)
L2=len(st2)
L=min(L1,L2)
similar=0
for i in range(L):
if(st1[i]==st2[i]):
similar+=1
print(similar*2/(L1+L2))
python exercises
python exercise
0.967741935483871
In [ ]:
In [42]:
list1=input().split()
m=0
for word in list1:
cnt=0
for ch in word:
if(ch in "aeiouAEIOU"):
cnt+=1
if(cnt>m):
reqword=word
m=cnt
print(reqword)
In [44]:
21
1 3 7 21
In [ ]:
In [ ]:
13. Few prime numbers can be equal to the sum of other consecutive
prime numbers.
For example
5 = 2 + 3,
17 = 2 + 3 + 5 + 7,
41 = 2 + 3 + 5 + 7 + 11 + 13.
Find out how many prime numbers which satisfy this property
are present in the range 3 to N subject to a constraint that
summation should always start with number 2.
Write a python program to find out the prime numbers that
satisfy the above mentioned property in a given range.
Input:
15
Output:
5
Input:
20
Output:
5
17
In [ ]:
#Logic..N=100
1) Generate the list of prime numbers 2 to N
list1=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
2) start testing from second to last element. if second is sum of
first, third is sum of first+second and so on
3)if(true) print the prime element
In [73]:
def is_prime(N):
for i in range(2,(N//2)+1):
if(N%i==0):
return False
return True
##################################
N=int(input())
list1=[]
for num in range(2,N+1):
if(is_prime(num)):
list1.append(num)
print(list1)
for i in range(1,len(list1)):
num=list1[i]
sum1=0
for ele in list1[0:i]:
sum1=sum1+ele
if(sum1==num):
print(num)
break
100
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 7
1, 73, 79, 83, 89, 97]
5
17
41
In [ ]:
In [76]:
def is_pwd(pwd):
L=len(pwd)
l=0
d=0
u=0
c=0
##############################################
N=int(input())
list1=[]
for i in range(N):
passwd=input()
if(is_pwd(passwd)):
list1.append(passwd)
for pw in list1:
print(pw)
2
2w3EEEgw3*
a F1ttt#
In [ ]:
In [78]:
def is_usn(w):
L=len(w)
if(L==10):
region=w[0].isdigit()
college=w[1:3].isalpha()
year=w[3:5].isdigit()
branch=w[5:7].isalpha()
roll=w[7:10].isdigit()
if(region and college and year and branch and roll):
return True
return False
#######################################
N=int(input())
list1=[]
for i in range(N):
usn=input()
if(is_usn(usn)):
list1.append(usn)
for u in list1:
print(u)
1
1CR1mEC112
In [ ]:
askdf
1CR20IS003
1CR19EC112
2CE18EC110
In [ ]:
In [79]:
def is_phonenumber(num):
list1=num.split("-")
if(len(list1)==3):
n1=list1[0]
n2=list1[1]
n3=list1[2]
cond1=len(n1)==3 and n1.isdigit()
cond2=len(n2)==3 and n2.isdigit()
cond3=len(n3)==4 and n3.isdigit()
if(cond1 and cond2 and cond3):
return True
return False
###################
ph=input()
if(is_phonenumber(ph)):
print(ph)
445-567-9876
445-567-9876
In [ ]:
In [81]:
N=int(input())
list1=[]
for i in range(N):
st=input()
list1.append(st)
for st in list1:
list2=st.split()
first=list2[0].lower()
last=list2[-1]
if(first=="hello" and last.isdigit()):
print(st)
4
Hello How you 9
Helo How is
How are you99
Hello 999
Hello How you 9
Hello 999
In [ ]:
In [82]:
emails=input().split()
for email in emails:
ind1=email.index("@")
ind2=email.index(".")
name=email[0:ind1]
company=email[ind1+1:ind2]
print(name,company)
In [ ]:
In [84]:
dates=input().split()
for date in dates:
list1=date.split("-")
if(len(list1[0])==4):
list1.reverse()
st1="-".join(list1)
print(st1,end=" ")
else:
print(date,end=" ")
In [ ]:
In [88]:
valid_Scheme=["https","http","ftp"]
domain=[".com",".in",".edu",".org"]
N=int(input())
url_list=[]
for i in range(N):
url=input()
url_list.append(url)
cnt=0
for url in url_list:
list1=url.split("://")
if(list1[0] in valid_Scheme):
1
https://www.yahoo.com (https://www.yahoo.com)
https://www.yahoo.com (https://www.yahoo.com)
1
In [5]:
CDXLIII
443
In [ ]: