[go: up one dir, main page]

0% found this document useful (0 votes)
40 views8 pages

PY-1 (A) Write A Program To Calculate The Factorial of The Given Number Using For Loop

The document contains 5 Python programs. Program 1 calculates factorial and sums a series. Program 2 checks if a number is even/odd and reverses a string. Program 3 generates numbers from 1-10 and removes odds. Program 4 performs set operations on prime and odd numbers. Program 5 analyzes a string using a class to count uppercase, lowercase, vowels, consonants and spaces.

Uploaded by

Arasu Lingam
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)
40 views8 pages

PY-1 (A) Write A Program To Calculate The Factorial of The Given Number Using For Loop

The document contains 5 Python programs. Program 1 calculates factorial and sums a series. Program 2 checks if a number is even/odd and reverses a string. Program 3 generates numbers from 1-10 and removes odds. Program 4 performs set operations on prime and odd numbers. Program 5 analyzes a string using a class to count uppercase, lowercase, vowels, consonants and spaces.

Uploaded by

Arasu Lingam
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/ 8

PY-1(a)

Write a program to calculate the factorial of the


given number using for loop

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


if num ==0:
fact = 1
fact = 1
for i in range(1,num+1):
fact = fact*i
print("Factorial of", num, "is",fact)

------------------------------------------------
PY-1(b)

Write a program to sum the series:1/1 + 2**2/2 + 3**3/3


+ ...... n**n/n

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


s = 0.0
for i in range(1,n+1):
a = float(i**i/i)

1
s = s+a
print("Sum of the Series",n,"is",s)
------------------------------------------------
PY-2(a)

Write a program using functions to check whether a


number is even or odd

def odd_even(a):
if (a%2==0):
return 1
else:
return 0
n = int(input("Enter a Number: "))
if (odd_even(n)==1):
print("The Given Number is Even.")
elif(odd_even(n)==0):
print("The Given Number is Odd.")
------------------------------------------------
PY-2(b)

Write a program to create a mirror of the given string.

2
For example, “welcome” = “emoclew“

def rev(str1):
str2=''
i = len(str1)-1
while i>=0:
str2+=str1[i]
i-=1
return str2
word = input("Enter The Word: ")
print("Reversed Word is",rev(word))
------------------------------------------------
PY-3(a)

Write a program to generate values from 1 to 10 and


then remove all the odd numbers from the list

num1 = []
for i in range(1,11):
num1.append(i)

3
print("Print Numbers From 1 To 10....","\n",num1)

for j,i in enumerate(num1):


if(i%2==1):
del num1[j]
print("Numbers After Removed Odd Numbers....",'\n',num1)

------------------------------------------------
PY-4

Write a Program that generate a set of prime numbers


and another set of odd numbers. Display the result of
union, intersection, difference and symmetric
difference operations

from typing import Union

odd = set([a*2+1 for a in range(0,5)])

prime = set()

for i in range(2,10):

j = 2

f = 0

while j<=i/2:

if i%j==0:

f = 1

4
j+=1

if f==0:

prime.add(i)

print("Prime Numbers:",prime)

print("Odd Numbers:",odd)
print("Union of Prime Numbers and Odd Numbers:",odd.union(prime))

print("Intersection:",odd.intersection(prime))

print("Difference:",odd.difference(prime))
print("Symetric Difference:",odd.symmetric_difference(prime))

------------------------------------------------
PY-5

Write a program to accept a string and print the


number of uppercase, lowercase, vowels,
consonants and spaces in the given string using
Class

cl ass St r i ng:

def __i ni t __( sel f ) :

sel f . upcase=0

sel f . l wcase=0

sel f . vowel s=0

5
sel f . cons=0

sel f . spaces=0

sel f . st r i ng=""

def get st r ( sel f ) :

sel f . st r i ng=st r ( i nput ( "Ent er a St r i ng: ") )

def count _up( sel f ) :

f or ch i n sel f . st r i ng:

i f ( ch. i supper ( ) ) :

sel f . upcase+=1

def count _l ow( sel f ) :

f or ch i n sel f . st r i ng:

i f ( ch. i sl ower ( ) ) :

sel f . l wcase+=1

def vowel ( sel f ) :

f or ch i n sel f . st r i ng:

i f ( ch i n
( ' A' , ' a' , ' E' , ' e' , ' I ' , ' i ' , ' O' , ' o' , ' U' , ' u' ) ) :

sel f . vowel s+=1

6
def const ( sel f ) :

f or ch i n sel f . st r i ng:
i f ( ch not i n ( ' A' , ' a' , ' E' , ' e' , ' I ' , ' i ' , ' O' , ' o' , ' U' , ' u' ) ) :

sel f . cons+=1

def space( sel f ) :

f or ch i n sel f . st r i ng:

i f ( ch==" ") :

sel f . spaces+=1

def exe( sel f ) :

sel f . count _up( )

sel f . count _l ow( )

sel f . vowel ( )

sel f . const ( )

sel f . space( )

def out ( sel f ) :

pr i nt ( "The Gi ven St r i ng Cont ai ns. . . ")

pr i nt ( "%d Upper case Let t er s"%sel f . upcase)

pr i nt ( "%d Lower case Let t er s"%sel f . l wcase)

pr i nt ( "%d Vowel s. "%sel f . vowel s)

7
pr i nt ( "%d Consonant s. "%sel f . cons)

pr i nt ( "%d Spaces"%sel f . spaces)

s=St r i ng( )

s. get st r ( )

s. exe( )

s. out ( )

------------------------------------------------

You might also like