0% found this document useful (0 votes)
168 views

Python Assignment

The document provides examples of Python programs and functions to perform various tasks: 1) It includes 7 programs that calculate the area of a triangle, Fibonacci series, factorial of a number, sum of natural numbers, voter eligibility, even/odd numbers in a list, and allowing the user to enter values until -1 is entered. 2) It also includes 7 functions examples: to find the sum of a 2-digit number, check voter eligibility based on age and state, use a default parameter, find the sum of even numbers within a limit, and demonstrate passing lists and modifying values within and outside functions. 3) The examples cover basic Python concepts like inputs, conditionals, loops, functions,

Uploaded by

Aniket Puri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views

Python Assignment

The document provides examples of Python programs and functions to perform various tasks: 1) It includes 7 programs that calculate the area of a triangle, Fibonacci series, factorial of a number, sum of natural numbers, voter eligibility, even/odd numbers in a list, and allowing the user to enter values until -1 is entered. 2) It also includes 7 functions examples: to find the sum of a 2-digit number, check voter eligibility based on age and state, use a default parameter, find the sum of even numbers within a limit, and demonstrate passing lists and modifying values within and outside functions. 3) The examples cover basic Python concepts like inputs, conditionals, loops, functions,

Uploaded by

Aniket Puri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy