Python Basic Programs

MATHEMATICS

#provided an list print the elements with repeating twice

N=int(input()) # user input
lst=list(map(int, input().split(' ')[:N]))
freq={}

def CountFrequency(lst):
 	for items in lst: 
 		freq[items] = lst.count(items)
CountFrequency(lst)
for key, value in freq.items(): 
 	if value==2:
 		print(key)
print(freq)
Output

8
2 3 4 4 5 5 7 8
4
5
{2: 1, 3: 1, 4: 2, 5: 2, 7: 1, 8: 1}


#coprime test

n,m=map(int,input().split(' '))
import math
if math.gcd(n,m)==1:
	print("both numbers verify the coprime test")
else:
 	print("false")
Output

3   5
both numbers verify the coprime test


#Given a number N, print yes if the number is a multiple of 7 else print no.

N=int(input())
if N%7==0:
 	print("yes")
else:
 	print("no")
Output

21
yes


#A person saves his monthly saving according to given schema. 
#He saves same amount of money which is equal to the money saved in immediate previous two 
#Assume, initially he saved 1000 rupees and in first month he saved another 1000. 
#Your task is to tell how much he had totally saved at the end of ‘n’ months

n=int(input())
a=1000
b=1000
l=[]
total=0
for i in range(0,n):
 	c = a + b
 	a = b
	b = c
 	l.append(c)
if n==1:
 	total=2000
 	print(total)
else:
 	total = l[n-1] + l[n-2] 
 	print(total)
print(l)
Output

2
5000
[2000, 3000]


#You are given a large number made of only 0’s and 1’s.Your task is to find the max 
#no of consecutive 1’s. 
#If there are no 1’s print -1

n=int(input())
s = str(n)
sum=0
k=0
max=0
for i in range(len(s)):

 	if s[i]=='1':
 		k+=1
		if k>max:
 			max=k
 	elif s[i]=='0':
 			k=0
for i in range(len(s)):
 	sum=sum+int(s[i])
if sum==0:
 	print('-1')
else: 
 	print(max)

Output

00000000
-1


#Simi is learning about palindromic numbers.
#Her teacher gave her the task to count all palindromic numbers present in that range.
#Simi has told you about this and want your help.
#You design an algorithm in order to help simi.

n=int(input())
count=0
for i in range(1,n+1):
 	x=i
 	rev=0
 	while i!=0:
 		num=i%10
 		rev=rev*10+num
 		i=i//10
 	if x==rev:
 		count+=1
print(count)
Output

1021
109


#You are provided with a number ’n’. 
#Your task is to tell whether that number is saturated. 
#A saturated number is a number which is made by exactly two digits.

n=int(input())
s=str(n)
if len(s)==2:
 	print("saturated")
else:
 	print("unsaturated")

Output

34
saturated


#You are given a number ‘n’.
#You have to tell whether a number is great or not.
#A great number is a number whose sum of digits let (m) and 
#product of digits let(j) when summed together gives the number back

# also find the no of great nos in a particular given range and also find the list

n=int(input())
s=str(n)
m=0
j=1
for i in range(0,len(s)):
 	m=m+(int(s[i]))
 	j=j*(int(s[i]))
if m+j==n:
 	print("Great")
else:
 	print("no")
Output

123
no


#You are given a task to tell whether the number is pure or not. 
#A pure number is a number whose sum of digits is multiple of 3.

n=int(input())
s=str(n)
sum=0
for i in range(len(s)):
 	sum+=int(s[i])
if sum%3==0:
 	print("yes")
else:
 	print("not")
Output

234
yes


# find the minimum in 10 numbers writing the logic in one line

lst=list(map(int, input().split(' ')[:10]))
print(min(lst))
Output

2 3 4 5 6 7 8 9 10 6
2


#You are given a list of numbers.
#Print the least occurring element. 
#If there is more than 1 element print all of them in decreasing order of their value.

import collections
N=int(input())
l=list(map(int, input().split(' ')[:N]))
occ=collections.Counter(l)
lst=[]
key_min = min(occ.keys(), key=(lambda k: occ[k]))
for key,value in occ.items():
 	if value==occ[key_min]: 
 		lst.append(key)
print(lst)
lst.sort(reverse=True)
print(occ)
for x in range(len(lst)): 
 	print (lst[x], end=" ")
Output

5
0 5 6 3 2
[0, 5, 6, 3, 2]
Counter({0: 1, 5: 1, 6: 1, 3: 1, 2: 1})
6 5 3 2 0


#You are given with a number ‘n’. 
#You have to count the pair of two numbers a and b such that sum of two numbers are equal to the number itself

n=int(input())
lst=[]
for x in range(1,n):
 	lst.append(x)
count = 0
print(lst)
for i in range(0, len(lst)): 
 	for j in range(0, len(lst)): 
 		if lst[i] + lst[j] == n: 
 			count += 1
print(count)
Output

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


#In XYZ country there is rule that car’s engine no. depends upon car’ number plate. 
#Engine no is sum of all the integers present on car’s Number plate.
#The issuing authority has hired you in order to provide engine no. to the cars.
#Your task is to develop an algorithm which takes input as in form of string(Number plate) and Output is.
#Engine number

s=str(input())
count=0
for x in s:
 	if x.isdigit()== True:
 		count+=int(x)
 
print(count)
Output

td67r58
26


#print the minimum in the elements

n=int(input())
l=list(map(int, input().split(' ')[:n]))
print(min(l))
Output

5
1 2 3 4 5
1


#Given 3 numbers N , L and R. Print 'yes' if N is between L and R else print 'no'.

N=int(input())
L,R=map(int,input().split())
if L<=N<=R:
 	print('yes')
else:
 	print("no")
Output

4
3 7
yes


#Given a number N, print the product of the digits

N=int(input())
prod=1
l=[]
if N<=100000000000:
 	s=str(N)
 	for i in range(len(s)):
 		if s[i].isdigit()==True:
 			prod*=int(s[i])
print(prod)
Output

456
120


# compute power of two greater than or equal to n

def nextPowerOf2(n):
 	while n & n - 1:
 		n = n & n - 1 
 	return n << 1
n=int(input())
print(nextPowerOf2(n))
Output

32
64


#Given a number N, find its next immediate greater power of 2(i.e 2^1, 2^2, 2^3...)

from math import ceil,log2
N=int(input())
if N<=1000:
 	pos = ceil(log2(N))
 	p = pow(2, pos) 
if p==N:
 	print(pow(2,pos+1))
else:
 	print(p)

Output

5
8


#Given a number N, find the nearest greater multiple of 10.

import math
x=int(input())
p=math.ceil(x / 10) * 10
print(p)
Output

45
50


#program for composite number

n=int(input('Enter the number '))
factor=0
for i in range(1,n):
 	if n%i==0:
 		factor=i
if factor>1:
 	print ('yes')
else:
 	print ('no')

Output

Enter the number  3
no


N=int(input())
if N%13==0:
 	print("yes")
else:
 	print("no")

#Count the number of digits of a given number N

s=str(input())
count=0
for i in s:
 	if i.isdigit()==True:
 		count+=1
print(count)
Output

12345
5


#Given a number N, print the odd digits in the number(space seperated) 
#or print -1 if there is no odd digit in the given number.

N=int(input())
l=[]
if N<=100000000000000000000:
 	s=str(N)
 	for i in s:
 		if i.isdigit()==True:
 			if int(i)%2!=0:
 				l.append(i)
if len(l)==0:
 	print("-1")
else:
 	l.sort()
 	for j in l:
 		print(j,end=" ")
Output

12457986
1 5 7 9


#find the series 2 5 10 17

n=int(input())
l=[]
for i in range(1,n+1):
 	sum=i*i+1
 	l.append(sum)
for j in l:
	print(j,end=" ")
Output

5
2 5 10 17 26


#Given 2 numbers N,M. Find their difference and check whether it is even or odd.

N,M=map(int,input().split(" "))
diff=N-M
if diff%2==0:
 	print("even")
else:
 	print("odd")

Output

5 7
even


#Write a program to print the sum of the first K natural numbers

n=int(input())
sum=0
for i in range(1,n+1):
 	sum+=i
print(sum)

Output

3
6


#You are given with a string which comprises of some numbers. 
#Your task is to find the largest integer by converting the string to the corresponding integer

l=list(map(str, input().split(' ')))
lst=[]
for i in l:
 	if i.isdigit()==True:
 		lst.append(int(i))
print(max(lst))
print(lst)

a,b,c=map(int,input().split(" "))
fun=(a*b)%c
print(fun)

N=int(input())
if N<=1000:
 	for i in range(1,N+1):
 		if N%i==0:
 			print(i,end=" ")

#Given 3 numbers A,B,C find the sum of Arithmetic Series with a=A, d=B and n=C

import math
A,B,C=map(int,input().split(' '))
sum=math.floor(0.5*C*(2*A+(C-1)*B))
print(sum)

import math
N,M=map(int,input().split())
num=N*M
root=math.sqrt(num)
if int(root + 0.5) ** 2 == num:
 	print("yes")
else:
 	print("no")

#print the series 1,9,36,100,225

n=int(input())
for i in range(1,n+1):
 	sum=0
 	for j in range(1,i+1):
 		sum+=j
 	print(sum**2,end=" ")

#Given length L and breadth B of a farm, print the area of the farm upto 5 decimal decimals

L,B=map(float,input().split(" "))
area=L*B
ar = int(area*100000)/100000.
print("%.5f" % ar)

#Given a range of 2 numbers (i.e) L and R count the number of prime numbers in the range

import sympy
L,R=map(int,input().split(" "))
if L <= R <= 100000:
 	l=list(sympy.sieve.primerange(L, R+1))
print(len(l)) 

#Given numbers A,B find A^B

A,B=map(int,input().split(" "))
p=pow(A,B)
print(p)

#You are given a ‘true’ string. String is called true if weight of string is multiple of 8.
#Your task is to tell whether a string can be declared True or Not.
#Weight of string is the sum of ASCII value of Vowel character(s) present in the string.

ch = str(input())
vowels="aeiou"
sum=0
for i in ch:
 	for j in range(len(vowels)):
 		if i==vowels[j]:
 			sum += ord(i) 
if sum%8==0:
 	print("1")
else:
 	print("0")

L,B,H=map(int,input().split(" "))
volume=L*B*H
sa=2*(L*B+B*H+H*L)
print(sa," ",volume)


#Given 2 numbers N and M add both the numbers and check whether the sum is odd or even.

N,M=map(int,input().split(" "))
sum=N+M
if sum%2==0:
 	print("even")
else:
	print("odd")

#find the sum of all negative integers in an array
	
N=int(input())
if N<=100000:
 	l=list(map(int, input().split(' ')[:N]))
 	sum=0
 	for i in l:
 		if i<0:
 			sum=+i
print(sum)

n=int(input())
rev=0

while n!=0:
 	num=n%10
 	rev=rev*10+num
 	n=n//10
 
print(rev)


from math import ceil,floor
floor(2.32)
Output

2


import math
math.log2(5)
Output

2.321928094887362


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext