You are on page 1of 12

PYTHON LAB PROGRAMS (20.11.

2023)
EXERCISE NO: 01A (FLOW CONTROLS)
IF STATEEMENT
1. ELIGIBLE FOR VOTING:

x=int (input("Enter your age :"))


if x>=18:
print ("You are eligible for voting")

IF ELSE STATEMENT
1. LEAP YEAR:

year=int(input("Enter year to be checked:"))


if(year%4==0):
print("The year is a leap year!")
else:
print("The year isn't a leap year!")

2. ODD OR EVEN:

a = int (input("Enter any number :"))


x="even" if a%2==0 else "odd"
print (a, " is ",x)

IF ELIF ELSE STATEMENT:


1. POSITIVE OR NEGATIVE OR ZERO:

num = float(input("Enter a number: "))


if num >= 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

NESTED IF:
1. POSITIVE OR NEGATIVE OR ZERO:

num = -3
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

WHILE LOOP:
1. TO PRINT NUMBERS FROM 1 TO 5:

i=1
while(i<=5):
print(i,end='\t')
i=i+1

2. TO PRINT NUMBERS FROM 10 TO 15 WITH ELSE PART:

i=10
while(i<=15):
print(i,end='\t')
i=i+1
else:
print("\nValue of i when the loop exit ",i)
FOR LOOP:
1. PRINTING EVEN NUMBERS:

for i in range(2,10,2):
print(i,end=' ')
else:
print ("\nEnd of the loop")

2. PRINTING SOME WORDS:

words= ["Apple", "Banana", "Car", "Dolphin" ]


for word in words:
print (word)

NESTED LOOP:
1. PRINTING STARS PYRAMID:

rows = 5
k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end=" ")
k=k-2
for j in range(0, i + 1):
print("* ", end="")
print(" ")

2. PRINTING NUMBER PYRAMID:

rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print('')

JUMP STATEMENTS:
1. Break STATEMENT: (Printing numbers)

for i in range(1, 5):


if(i == 3):
break
print(i)

2. Continue STATEMENT: (Printing numbers)

for i in range(1, 5):


if(i == 2):
continue
print(i)

3. Pass STATEMENT: (Printing alphabets)

for alphabet in 'Legends':


if(alphabet == 'g'):
pass
else:
print(alphabet)

BASIC PROGRAMS:
1. ADD TWO NUMBERS:

x = input("Type a number: ")


y = input("Type another number: ")
sum = int(x) + int(y)
print("The sum is: ", sum)

2. FIBONACCI SERIES:

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


a=0
b=1
sum = a + b
count = 1
print("Fibonacci series is: ", end=" ")
while (count <= n):
count += 1
print(a, end=" ")
a=b
b = sum
sum = a + b

3. FACTORIAL OF THE GIVEN NUMBER:

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


fact=1
if(num!=0):
for i in range(1,num+1):
fact= fact*i
print("Factorial of ", num, "is ", fact)
EXERCISE NO: 01B (FUNCTIONS)
1. PRINTING STRINGS USING FUNCTIONS:

def greet():
print('Hello World!')
greet()
print('Outside function')

2. FUNCTIONS ARGUMENTS- ADD:

def add_numbers(num1, num2):


sum = num1 + num2
print("Sum: ",sum)
add_numbers(5, 4)

3. FUNCTIONS RETURN TYPE- SUBTRACT:

def diff_numbers(num1, num2):


diff = num1 - num2
return diff
result = diff_numbers(5, 4)
print('Difference: ', result)

4. LIBRARY FUNCTIONS:

import math
square_root = math.sqrt(4)
print("Square Root of 4 is",square_root)
power = pow(2, 3)
print("2 to the power 3 is",power)
5. RECURSIVE FUNCTION:

print("Enter the two Number:")


number1=int(input())
number2=int(input())
def Div(number1,number2):
if number1<number2:
return 0
return 1 + Div(number1-number2, number2)
print("result ",Div(number1,number2))

BASIC PROGRAMS:
1. ADDING TWO NUMBERS USING RECURSIVE FUNCTION:

def add_numbers_recursive(x, y):


if y == 0:
return x
else:
return add_numbers_recursive(x + 1, y - 1)
num1 = 1
num2 = 2
result = add_numbers_recursive(num1, num2)
print("The sum of", num1, "and", num2, "is", result)

2. FINDING LCM OF THE GIVEN NUMBER USING


FUNCTION:

def calculate_lcm(x, y):


if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The L.C.M. of", num1,"and", num2,"is",
calculate_lcm(num1, num2))

3. SIMPLE CALCULATOR USING FUNCTIONS:

def add(P, Q):


return P + Q
def subtract(P, Q):
return P - Q
def multiply(P, Q):
return P * Q
def divide(P, Q):
return P / Q
print ("Please select the operation.")
print ("a. Add")
print ("b. Subtract")
print ("c. Multiply")
print ("d. Divide")
choice = input("Please enter choice (a/ b/ c/ d): ")
num_1 = int (input ("Please enter the first number: "))
num_2 = int (input ("Please enter the second number: "))
if choice == 'a':
print (num_1, " + ", num_2, " = ", add(num_1, num_2))
elif choice == 'b':
print (num_1, " - ", num_2, " = ", subtract(num_1, num_2))
elif choice == 'c':
print (num1, " * ", num2, " = ", multiply(num1, num2))
elif choice == 'd':
print (num_1, " / ", num_2, " = ", divide(num_1, num_2))
else:
print ("This is an invalid input")

4. MAXIMUM OF THREE NUMBERS USING FUNCTIONS:

def max_of_two( x, y ):
if x > y:
return x
return y
def max_of_three( x, y, z ):
return max_of_two( x, max_of_two( y, z ) )
print(max_of_three(3, 6, -5))

EXERCISE NO: 01C (STRING MANIPULATION)


1. CREATING STRINGS:

name = "Python"
print(name)
str1="How are you"
print (str1)

2. PROGRAM TO ACCESS EACH CHARACTER WITH


POSITIVE SUBSCRIPT:
str1 = input ("Enter a string: ")
index=0
for i in str1:
print ("Subscript[",index,"] : ", i)
index += 1

3. PROGRAM TO ACCESS EACH CHARACTER WITH


NEGATIVE SUBSCRIPT:

str1 = input ("Enter a string: ")


index=-1
while index >= -(len(str1)):
print ("Subscript[",index,"] : " + str1[index])
index += -1

4. OPERATIONS USING STRING OPERATORS:


str1= "WELCOME "
str2= "TO LEARN PYTHON "
print(str1 + str2) #CONCATENATION (+)

str1= "WELCOME TO"


str1+= " LAB"
print(str1) # APPEND (+=)

str1= "PYTHON"
print(str1*3) #REPEATING (*)

str1= "COMPUTER SCIENCE"


print(str1[0])
print(str1[0:5])
print(str1[:5])
print(str1[6:]) #STRING SLICING

str1 = "PROGRAMMING LANGUAGE"


print (str1[10:16])
print (str1[10:16:4])
print (str1[10:16:2])
print (str1[::3]) #STRIDE WHEN SLICING

BASIC PROGRAMS:
1. GIVEN STRING PALINDROME OR NOT:

def isPalindrome(s):
return s == s[::-1]
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")

2. PROGRAM TO DISPLAY A STRING IN A PATTERN:

str1=' STR '


i=1
while i<=5:
print (str1*i)
i+=1

3. FINDING CONSONANTS AND VOWELS OF A GIVEN


STRING:
str=input("ENTER A STRING: ");
vowels=0
consonants=0
for i in str:
if(i == 'a'or i == 'e'or i == 'i'or i == 'o'or i == 'u' or
i == 'A'or i == 'E'or i == 'I'or i == 'O'or i == 'U' ):
vowels= vowels+1
else:
consonants=consonants+1;
print("The number of vowels:",vowels);
print("\nThe number of consonant:",consonants);

4. SWAPPING STRINGS:
x=5
y = 10
print('The value of x before swapping:',x)
print('The value of y before swapping:',y)
temp = x
x=y
y = temp
print('The value of x after swapping: { }'.format(x))
print('The value of y after swapping: { }'.format(y))

5. MIRROR IMAGE OF THE GIVEN STRING:


str=input("Enter a String: ")
print("The Entered String is ",str)
print("The Mirror image of the given string: ",str[::-1])

You might also like