Sunteți pe pagina 1din 8

Program to find the factorial of a number using function

def factorial(num):

fact=1

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

fact=fact*i

print("The factorial of a number is ", fact)

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

factorial(num)

Program to find the factorial of a number using recursion

def factorial(num):

if num==0:

return 1

return num*factorial(num-1)

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

print("The factorial of a number is ", factorial(num))

Program to compare the two numbers using function

def maximum(num1, num2): #formal parameters

if num1>num2:

print(num1, "is greater than", num2)

elif num2>num1:

print(num2, "is greater than", num1)

else:

print("Both are equal")

num3=int(input("Enter first number = "))

num4=int(input("Enter second number = "))


maximum(num3,num4) #arguments

Program to compare two numbers with the help of function using return statement

def largest(num1, num2, num3):

if num1>num2 and num1>num3:

return num1

elif num2>num3 and num2>num1:

return num2

else:

return num3

num1=int(input("Enter first number = "))

num2=int(input("Enter second number = "))

num3=int(input("Enter third number = "))

a=largest(num1,num2,num3)

print(a,"is largest")

Program to show the use of local and global variable

a=20

def value():

global b

b=30

print("value of local variable b is ", b)

print("Value of global variable a inside value function is ", a)

print("Value of global variable a is ", a)

value()

print("Value of local variable b outside value function is ", b)


Program to show the use of positional argument in functions

def display(name, age):

print("Name = ", name, "\nAge = ", age)

display("abc", 40) #positional argument

display(age=45, name="def") #keyword argument

display(50, age=2) # error due to multiple values for the name

display("jkl", age=55) #keyword argument

def display1(department="xyz", rollno=10): #default parameters

print("Department = ", department, "\nRoll Number = ", rollno)

display1()

display1("cse")

display1("ece", 20)

display1(30)

Program to show the use of function

def display():

print("Welcome to functions ")

display()

Program to show the use of function

def message():

str1=input("Enter your name = ")

print("Dear", str1, "Welcome to the group of python programmers")

message()

message()
Program to find the sum of two numbers using function

def sum(x, y):

s=0

for i in range(x, y+1):

s=s+i

print("The sum of integers from ",x, "to",y, "is", s)

sum(1,25)

sum(50,75)

sum(90,100)

Program to show the use of multiple return statements

def calculator(num1,num2):

return num1+num2, num1-num2, num1*num2

num1=int(input("Enter first number = "))

num2=int(input("Enter second number = "))

a=calculator(num1,num2)

print("The result of various arithmetic operations is ", a)


Strings
Program to access the string with the help of index

s1= str("Hello")

print(s1)

print(s1[2])

print(s1[-1])

print(s1[-4])

Program for string traversing using for loop

s2="Welcome to the Python"

for ch in s2:

print(ch,end="")

print("\n")

Program for string traversing using while loop

s3="Lovely Professional University"

i=0

while i<len(s3):

print(s3[i],end="")

i=i+1

Program to show the use of string slicing

#string slicing

s1="School of Mechanical Engineering"

print(s1[: :]) #display full string

print(s1[2:8]) #display from 2nd index upto 7th index with a difference of 1

print(s1[1:16:2])#display from 1st index upto 15th index with a difference of 2

print(s1[::-1]) #display in reverse order


print(s1[-1:0:-1]) #display in reverse order upto 2nd last character

print(s1[:-1:])

Program to implement the various comparison operations on string

s1="abcd"

s2="ABCD"

print(s1>s2) #ascii value of a=97 and A= 65

s3="abc"

s4="abc"

print(s3==s4)

s5="ABC"

s6="DEF"

print(s5>s6)

s7="AAA"

s8="AAB"

print(s8>s7)

print(s1.upper())

print(s2.lower())

Program to show the use of various functions on string

#format()

print("Department is {} and Block number is {}".format("CSE", 34))

#split()

s1="C C++ Java Python"

print(s1.split())

#find()

s2="Welcome to Lovely Professional University"


print(s2.find("agr"))

print(s2.find("come"))

print(s2.find("e"))

print(s2.find("co",6))

#use of for loop

prefix="JKLMNOPQ"

suffix="ack"

for ch in prefix:

print(ch+suffix)

Program to implement various predefined functions of string

from string import *

shape="rectangle square"

f="10"

f1=" "

print(len(shape))

print(shape.find("r"))

print(ascii_lowercase)

print(ascii_uppercase)

print(digits)

print(shape.upper())

print(shape.lower())

print(shape.capitalize())

print(shape.title())

print(shape.islower())

print(shape.isupper())
print(shape.istitle())

print(f1.isspace())

print(f.isdigit())

S-ar putea să vă placă și