Lab Python Programs
Lab Python Programs
2023)
EXERCISE NO: 01A (FLOW CONTROLS)
IF STATEEMENT
1. ELIGIBLE FOR VOTING:
IF ELSE STATEMENT
1. LEAP YEAR:
2. ODD OR EVEN:
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
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")
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(" ")
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)
BASIC PROGRAMS:
1. ADD TWO NUMBERS:
2. FIBONACCI SERIES:
def greet():
print('Hello World!')
greet()
print('Outside function')
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:
BASIC PROGRAMS:
1. ADDING TWO NUMBERS USING RECURSIVE FUNCTION:
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))
name = "Python"
print(name)
str1="How are you"
print (str1)
str1= "PYTHON"
print(str1*3) #REPEATING (*)
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")
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))