0% found this document useful (0 votes)
7 views11 pages

Progrm

The document contains various Python code snippets demonstrating different functionalities such as calculating student results, Fibonacci sequence generation, binomial coefficients, statistical calculations, character frequency analysis, file reading, exception handling, and complex number operations. Each code snippet includes user input and corresponding outputs showcasing the results of the operations performed. The examples illustrate basic programming concepts and data handling in Python.

Uploaded by

pcg7086
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)
7 views11 pages

Progrm

The document contains various Python code snippets demonstrating different functionalities such as calculating student results, Fibonacci sequence generation, binomial coefficients, statistical calculations, character frequency analysis, file reading, exception handling, and complex number operations. Each code snippet includes user input and corresponding outputs showcasing the results of the operations performed. The examples illustrate basic programming concepts and data handling in Python.

Uploaded by

pcg7086
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/ 11

def printResult():

total=(int)(subject1 + subject2 + subject3)

percentage = (int)( (subject1 + subject2 +subject3) / 300 * 100 );

print("USN Name total percentage")

print(usn,"\t",name,"\t",total,"\t",percentage)

if percentage<35:

print("student",name , "has failed")

else:

print("Student",name, "has passed")

usn=input("Enter USN : ")

name = input("Enter Name : ")

subject1 =int(input("Enter subject1 marks : "))

subject2 =int(input("Enter subject2 Marks : "))

subject3 = int(input("Enter subject3 Marks : "))

print("Result : ")

printResult()

OUTPUT1:

Enter USN : 12

Enter Name : Mark

Enter subject1 marks : 87

Enter subject2 Marks : 66

Enter subject3 Marks : 55

Result :

USN Name total percentage

12 Mark 208 69

Student Mark has passed

OUTPUT2:

Enter USN : 11

Enter Name : James

Enter subject1 marks : 23

Enter subject2 Marks : 11

Enter subject3 Marks : 15

Result :

USN Name total percentage

11 James 49 16

Student James has failed


class Person:

def getPersonDetails(self):

self.name=input("Enter Name : ")

self.year = int(input("Enter year : "))

def printResult(self):

self.age=(int)(2023-self.year)

print(self.age,"year")

if self.age>65:

print("Person",self.name , "is senior citizen")

else:

print("Person",self.name , "is not a senior citizen")

S1=Person()

S1.getPersonDetails()

print("age is : ")

S1.printResult()

OUTPUT1:

Enter Name : Spencer

Enter year : 2020

age is :

3 year

Person Spencer is not a senior citizen

OUTPUT2:

Enter Name : Scott

Enter year : 1945

age is :

78 year

Person Scott is senior citizen


nterms = int(input("Enter N? "))

n1, n2 = 0, 1

count = 0

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci sequence upto",nterms,":")

print(n1)

else:

print("Fibonacci sequence:")

while count < nterms:

print(n1)

nth = n1 + n2

n1 = n2

n2 = nth

count += 1

OUTPUT:

Enter N? 5

Fibonacci sequence:

3
def binomialCoeff(n, k):

if k > n:

return 0

if k == 0 or k == n:

return 1

return binomialCoeff(n-1, k-1) + binomialCoeff(n-1, k)

n= int(input("Enter a number N: "))

k=int(input("Enter value of K: "))

factorial = 1

if n < 0:

print(" Factorial does not exist for negative numbers")

elif n== 0:

print("The factorial of 0 is 1")

else:

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

factorial = factorial*i

print("The factorial of",n,"is",factorial)

print("Value of C(%d,%d) is (%d)" % (n, k, binomialCoeff(n, k)))

OUTPUT:

Enter a number N: 5

Enter value of K: 2

The factorial of 5 is 120

Value of C(5,2) is (10)


import numpy as np

lst = []

n = int(input("Enter number of elements : "))

for i in range(0, n):

ele = int(input())

lst.append(ele) # adding the element

print("list elements are",lst)

print("mean is" ,np.average(lst))

print("variance is" ,np.var(lst))

print("standars deviation is",np.std(lst))

OUTPUT:

Enter number of elements : 5

list elements are [1, 2, 3, 4, 5]

mean is 3.0

variance is 2.0

standars deviation is 1.4142135623730951


def char_frequency(str1):

dict = {}

for n in str1:

keys = dict.keys()

if n in keys:

dict[n] += 1

else:

dict[n] = 1

return dict

str1=input("Enter the multi-digit number: ")

print(char_frequency(str1))

OUTPUT:

Enter the multi-digit number: 55667888

{'5': 2, '6': 2, '7': 1, '8': 3}


text =open("sample.txt", "r")

d =dict()

forline intext:

line =line.strip()

line =line.lower()

words =line.split(" ")

forword inwords:

ifword ind:

d[word] =d[word] +1

else:

d[word] =1

forkey inlist(d.keys()):

print(key, ":", d[key])

OUTPUT:

mango : 3

banana : 3

apple : 3

pear : 2

grapes : 1

strawberry : 2

kiwi : 1
def DivExp(a,b):

try:

c=a/b

print("Quotient:",c)

except ZeroDivisionError:

print("Division by Zero!")

a=int(input("Enter the value of a:"))

b=int(input("Enter the value of b:"))

result=(DivExp(a,b))

OUTPUT:

Enter the value of a:5

Enter the value of b:0

Division by Zero!
class Complex:

def __init__(self, tempReal, tempImaginary):

self.real = tempReal;

self.imaginary = tempImaginary;

def addComp(self, C1, C2):

temp=Complex(0, 0)

temp.real = C1.real + C2.real;

temp.imaginary = C1.imaginary + C2.imaginary;

return temp;

if __name__=='__main__':

C1 = Complex(3, 2);

print("Complex number 1 :", C1.real, "+ i" + str(C1.imaginary))

C2 = Complex(9, 5);

print("Complex number 2 :", C2.real, "+ i" + str(C2.imaginary))

C3 = Complex(0, 0)

C3 = C3.addComp(C1, C2);

print("Sum of complex number :", C3.real, "+ i"+ str(C3.imaginary))

OUTPUT:

Complex number 1 : 3 + i2

Complex number 2 : 9 + i5

Sum of complex number : 12 + i7


def getStudentDetails(self):

self.usn=input("Enter USN : ")

self.name = input("Enter Name : ")

self.subject1 =int(input("Enter subject1 marks : "))

self.subject2 = int(input("Enter subject2 Marks : "))

self.subject3 = int(input("Enter subject3 Marks : "))

def printResult(self):

self.total=(int)(self.subject1 + self.subject2 + self.subject3)

self.percentage = (int)( (self.subject1 + self.subject2 + self.subject3) / 300 * 100 );

print("USN Name total percentage")

print(self.usn,self.name,self.total,self.percentage)

if self.percentage<35:

print("student",self.name , "has failed")

else:

print("Student",self.name, "has passed")

S1=Student()

S1.getStudentDetails()

print("Result : ")

S1.printResult()

OUTPUT1:

Enter USN : 12

Enter Name : Mark

Enter subject1 marks : 87

Enter subject2 Marks : 66

Enter subject3 Marks : 55

Result :

USN Name total percentage

12 Mark 208 69

Student Mark has passed

OUTPUT2:

Enter USN : 11

Enter Name : James

Enter subject1 marks : 23

Enter subject2 Marks : 11

Enter subject3 Marks : 15

Result :

USN Name total percentage

11 James 49 16
student James has failed

You might also like