www.acharya.ac.
in
Dept. of MCA
Dept. of MCA
Shaheena KV,Dept.of MCA
Python Program to Check Leap Year
# To get year (integer input) from the user
year = int(input("Enter a year: "))
# divided by 100 means century year (ending with 00)
# century year divided by 400 is leap year
Python programming topics:
if (year % 400 == 0) and (year % 100 == 0): • Python Operators
print("{0} is a leap year".format(year)) • Python if…elif…else Statement
# not divided by 100 means not a century year
Dept. of MCA # year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))
# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
print("{0} is not a leap year".format(year))
Python Program to Create Pyramid Patterns
Dept. of MCA
Program to print half pyramid using * *
**
***
****
*****
rows = int(input("Enter number of rows: "))
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print()
Dept. of MCA
Program to print half pyramid a using
numbers 1
12
123
1234
12345
rows = int(input("Enter number of rows: "))
Dept. of MCA
for i in range(rows):
for j in range(i+1):
print(j+1, end=" ")
print()
Program to print half pyramid using alphabets
BB
CCC
Dept. of MCA
DDDD
EEEEE
Inverted half pyramid using * *****
****
***
**
*
rows = int(input("Enter number of rows: "))
Dept. of MCA for i in range(rows, 0, -1):
for j in range(0, i):
print("* ", end=" ")
print()
Inverted half pyramid using numbers
12345
1234
123
12
1
rows = int(input("Enter number of rows: "))
Dept. of MCA
for i in range(rows, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()
Program to print full pyramid using * *
rows = int(input("Enter number of rows: ")) ***
k=0 *****
for i in range(1, rows+1): *******
for space in range(1, (rows-i)+1): *********
print(end=" ")
while k!=(2*i-1):
Dept. of MCA
print("* ", end="")
k += 1
k=0
print()
Full Pyramid of Numbers 1
rows = int(input("Enter number of rows: ")) 232
k=0 34543
count=0 4567654
count1=0 567898765
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(" ", end="")
count+=1
while k!=((2*i)-1):
Dept. of MCA
if count<=rows-1:
print(i+k, end=" ")
count+=1
else:
count1+=1
print(i+k-(2*count1), end=" ")
k += 1
count1 = count = k = 0
print()
Program Assignments
• Python Program to Move all Zeros present in an
Array/List to the End
Dept. of MCA
• Python Program to Left Rotate a List by R Times
• Python Program to Put Even and Odd elements in a List
into Two Different Lists
Program Assignments
• Program to Create a List of Tuples with the First Element as
the Number and Second Element as the Square of the
Number
• Python Program to Remove Duplicate elements from a
Dept. of MCA Tuple
• Python Program to Shuffle Elements of a Tuple
• Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3
+ ….. + 1/N
Shaheena KV
Dept.of MCA
Dept. of MCA