Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
396 views

15 Programs On Python

The document contains 15 code snippets demonstrating various Python programming concepts like: finding the greatest of 3 numbers, checking if a character is alphabet/number/symbol, calculating student grade based on 4 subject marks, checking if a number is Armstrong, calculating digit sum and reverse, finding GCD of 2 numbers, calculating average of first n natural numbers, calculating factorial of a number, finding square root of a number, swapping two numbers without temp variable, generating Fibonacci sequence until n, finding prime numbers between ranges, performing basic string functions, sorting a list and finding sum/mean, creating lists of odd and even random numbers from a list.

Uploaded by

REDDY SARAGADA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
396 views

15 Programs On Python

The document contains 15 code snippets demonstrating various Python programming concepts like: finding the greatest of 3 numbers, checking if a character is alphabet/number/symbol, calculating student grade based on 4 subject marks, checking if a number is Armstrong, calculating digit sum and reverse, finding GCD of 2 numbers, calculating average of first n natural numbers, calculating factorial of a number, finding square root of a number, swapping two numbers without temp variable, generating Fibonacci sequence until n, finding prime numbers between ranges, performing basic string functions, sorting a list and finding sum/mean, creating lists of odd and even random numbers from a list.

Uploaded by

REDDY SARAGADA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

program to find the greatest number from three numbers

a=int (input ("enter value for a:"))

b=int (input ("enter value for b:"))

c=int (input ("enter value for c:"))

if(a>b and a>c):

print (a ,"is greater than", b ,"and", c)

elif (b>a and b>c):

print (b ,"is greater than", a ,"and", c)

else:

print (c ,"is greater than", a ,"and", b)

2.write a program to take input from the user and then check

whether it is alphabet number or special symbol

check=input ("enter something to check:")

if((check>='a' and check<='z') or (check>='A' and check<='Z')):

print (check ,"is alphabet")

elif(check>='0' and check<='9'):

print (check ,"is number")

else:

print (check ,"is special symbol")

3.write a program to enter the marks of a studentin four subjects then

calculate the total and aggregate display the grade obtained by the student

sub1=int (input ("enter marks for sub1:"))

sub2=int (input ("enter marks for sub2:"))

sub3=int (input ("enter marks for sub3:"))

sub4=int (input ("enter marks for sub4:"))

total=sub1+sub2+sub3+sub4

avg=total/4

if (sub1<35 and sub2<35 and sub3<35 and sub4<4):

print("fail")

else:

print("pass")
print(total)

print(avg)

if (100>avg and avg>85):

print ("grade is O")

elif(85>avg and avg>70):

print ("grade is A")

elif(70>avg and avg>55):

print ("grade is B")

elif(55>avg and avg>40):

print ("grade is C")

else:

print ("grade is F")

4.Python program to check whether the number is an Armstrong number or not

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

dsum = 0

temp = num

while temp > 0:

digit = temp % 10

dsum += digit ** 3

temp //= 10

if num == dsum:

print (num,"is an Armstrong number")

else:

print (num,"is not an Armstrong number")

5.write a program to calculate sum of digits and reverse of the digits in sum

var=int (input ("enter value for var:"))

dsum=0

rev=0

var1=var

while (var1! = 0):


dsum=dsum+var1%10

var1=int(var1/10)

print(dsum)

var1=dsum

while (var1! = 0):

rev=rev*10+var1%10

var1=int(var1/10)

print(rev)

6.Write a program to calculate gcd of two numbers

def gcd(a,b):

if(b==0):

return a

else:

return gcd(b,a%b)

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

b=int (input ("Enter second number:"))

GCD=gcd(a,b)

Print (GCD)

7.python program to calculate average of first n natural numbers

num=int (input ("enter value for num:"))

dsum=0

for i in range (1, num+1):

dsum=dsum+i

avg=dsum/num

print(dsum); print(avg)

8.python program to calculate factorial of a given number

num=int (input ("enter value for num:"))

fact=1

if(num<0):

print ("num must be positive")


elif(num==0):

print ("factorial of o is 1")

else:

for i in range (1, num+1):

fact=fact*i

print(fact)

9.python program to calculate square root of a number use break

#and continue statements

import math

for_it=int (input ("enter value for for_it:"))

for i in range(for_it):

num=int (input ("enter value for num:"))

print ("The Square root of", num,"= ", math.sqrt(num))

10.python program to swap two numbers without using temporary variable

a=int (input ("enter value for a:"))

b=int (input ("enter value for b:"))

a=a+b

b=a-b

a=a-b

print(a,b)

x=int (input ("enter value for x:"))

y=int (input ("enter value for y:"))

x=x*y

y=x/y

x=x/y

print(x,y)

11.python program for fabicco sequence untill n

nums=int(input("enter value for num:"))

n1=0
n2=1

count=0

if(nums<0):

print("must be positive")

elif(nums==1):

print(n1)

else:

while(count<nums):

print(n1,end=' , ')

nth=n1+n2

n1=n2

n2=nth

count+=1

12.Write a program to generate prime numbers in between one and twenty

lower=int(input("enter lower value:"))

upper=int(input("enter upper value:"))

for i in range(lower,upper+1):

if(i>1):

for x in range(2,i):

if(i%x==0):

break

else:

print(i)

13.Write a program to perform basic string functions in Python

14.python program to find sum and mean of elements in a list and

#display list elements in ascending order

list1=[12,67,23,50,34,100,54]

list1.sort()

print(list1)

dsum=0

for x in list1:
dsum+=x

print(dsum)

15.program that creates a list of 10 random integers in create two list order

#list and even list to display oddand even values respectively

list1=[12,21,23,32,34,43,45,54,56,65,67,76]

even_list=[]

odd_list=[]

for i in list1:

if(i%2==0):

even_list.append(i)

print(even_list)

else:

odd_list.append(i)

print(odd_list)

You might also like