Python Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Python Assignment

1. Write a Program to find Area of Triangle?

a=float(input("Enter the Length of Side 1"))


b=float(input("Enter the Length of Side 2"))
c=float(input("Enter the Length of Side 3"))
s=(a+b+c)/2
d=(s*(s-a)*(s-b)*(s-c))**0.5
print("The Area is",d)

Output

2. Write a Program to find Fibonacci Series?

a=0
b=1
n=int(input("Enter the No. of terms needed"))
while(n>0):
c=a+b
a=b
b=c
print(c)
n=n-1

Output
3. Write a Program to find a Factorial of a number?

a = int(input("Enter Number"))
b=1
for i in range(1,a+1):
b=b*i
print ("Factorial is",b)

Output

4. Write a Program to find the sum on n natural numbers?

a = int(input("Enter the number"))


b = (a*(a+1)*(2*a + 1))/6
print("Sum of",a,"natural numbers are",b)

5. Write a Program to find whether a person is eligible to vote or not?

age = int(input("Enter Age : "))

if age>=18:

status="Eligible"

else:

status="Not Eligible"

print("You are ",status," for Vote.")

Output
6. WAP to accept numbers & find out how many numbers are even or odd?

input_string = input("Enter a list element separated by space ")


list = input_string.split()
even_count, odd_count = 0, 0

for num in list1:

if num % 2 == 0:
even_count += 1

else:
odd_count += 1

print("Even numbers in the list: ", even_count)


print("Odd numbers in the list: ", odd_count)

Output

7. Program that allow user to enter number till user enters -1

a=int(input("enter the value"))


while a == -1:
break
print("You have entered wrong value")

Output
8. WAP to allow user to enter 10 numbers and find out their products and sum?

s=0
p=1
c=10
while(c==0):
a=int(input("Enter the Value"))
s=s+a
p=p*a
c=c-1
print("Sum of Numbers are",s)
print("And Product are",p)

Output

9. WAP to accept marks of n number of students for their 3 respective subjects. Calculate average
& Sum of individual Students as well as collectively

n=int(input("Enter number of Students"))


s=0
i=0
while i<n:
s1=0
j=0
while j<3:
x=int(input("Enter marks of Subject"))
s1+=x
j+=1
print("Total marks of student",i+1,"in 3 Subjects are",s1)
print("Average are",s1/3)
s+=s1
i+=1
print("Sum of marks and Average of all",n,"Students")
print("Sum are",s)
print("Average is",s/n)
Output

Python Functions

10. Write a function which will find out the sum of 2 digit number for a parameter passes to it.

def fun(num):
sum = 0
a=num%10
b=num/10
sum = a+b
return sum

Output

11. Write a function which will accept the Input as age of the person & State of the person and will
return whether he/she is able to vote or not

a= int(input("Enter Your Age"))


b=input("Enter your State")
if a>18:
print("You are able to vote")
else:
print("Not Eligible to vote")

Output
12. Demonstrate an example with function by showcasing use of default parameter

def student(firstname, lastname ='Jason', standard ='Bear'):


print(firstname, lastname, 'studies in', standard, 'Standard')

# 1 keyword argument
student(firstname ='Jason')

# 2 keyword arguments
student(firstname ='Jason', standard ='Seventh')

# 2 keyword arguments
student(lastname ='Gerad', firstname ='Jason')

Output

13. Write a function which will find out sum of even numbers upto the term passed to it.

n=int(input('Enter the limit '))


i=1
sum=0
while i<n:
if i%2==0:
sum=sum+i
i=i+1
print 'Sum of even numbers',sum

Output
14. Example of Slide 13

In this example we have created a list of abc and assigned separately values of a, b and c

#!/usr/bin/python
a, b, c = 0, 0, 0; abc = [0,0,0]
def getabc(a,b,c):
a = "Hello"; b = "World"; c = "!"
print 'Inside: a,b,c: ',a,b,c
return
def getlist(abc):
seq = ['Hello','World','!']
for index in range(len(abc)):
abc.pop(0)
abc.extend(seq)
print 'Inside: abc: ',abc
return
x = getabc(a,b,c)
print 'Outside: a,b,c: ',a,b,c
y = getlist(abc)
print 'Outside: abc: ',abc

Output

You might also like