Python Programs Related To Year Operations

#1 write a python program for display all leap years from 1900 to 2101 

Explanation
=====================

This program uses a for loop to iterate through all the range of years between start year and end+1 year. For each year, it checks whether the year is a leap year using the following conditions:

  •  If the year is divisible by 4 and not divisible by 100, or
  •  If the year is divisible by 400.


If either of these conditions is true, then the year is a leap year and the program prints the year.

 

Complete Python Program
============================

start = 1900
end = 2101
print(f"The Leap Years Between {start} and {end} are ...")
for year in range(1900, end+1):
   if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
       print(year)


Output
=============

The Leap Years Between 1900 and 2101 are ...
1904
1908
1912
1916
1920
1924
1928
1932
1936
1940
1944
1948
1952
1956
1960
1964
1968
1972
1976
1980
1984
1988
1992
1996
2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048
2052
2056
2060
2064
2068
2072
2076
2080
2084
2088
2092
2096


Note : You can change the start year and end year in the above program to generate Leap Years between any ranges.




#2 write a python program to check whether a given year is a leap year or not


Explanation
====================

This program asks the user to enter a year as an integer type. It then checks whether the year is a leap year using the following conditions:

  • If the year is divisible by 4 and not divisible by 100, or
  • If the year is divisible by 400.

If either of these conditions is true, then the year is a leap year and the program prints a positive message indicating that the year is a leap year.
Otherwise, the program prints a negative message indicating that the year is not a leap year.

Complete Python program
============================

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
   print(f"{year} is a leap year")
else:
   print(f"{year} is not a leap year")



Output
===============

Enter a year: 2024
2024 is a leap year



Output 2
==================

Enter a year: 2023
2023 is not a leap year








#3 write a python program to check whether the given year is leap year or not with functions

Explanation
===============

This below python program asks the user to enter a year as an integer type. It calls the function check_leave_year()  by passing the year as input argument to the Python function.
The Python function then checks whether the year is a leap year using the following conditions:

  • If the year is divisible by 4 and not divisible by 100, or
  • If the year is divisible by 400.

If either of the above two conditions are satisfied it returns Irue else return False.

In the main program the return value is checked using the if statement and printed the message appropriately.


Complete Python Program
============================

def check_leap_year(number):
   if (number % 4 == 0 and number % 100 != 0) or (number % 400 == 0):
       return True
   return False

year = int(input("Enter a year: "))

if check_leap_year(year):
  print(f"{year} is a leap year")
else:
  print(f"{year} is not a leap year")




Output
=============

Enter a year: 2
2 is not a leap year



Output 2
==============

Enter a year: 8080
8080 is a leap year



#4 write a python program to convert days into years weeks and days

Explanation
===============

The below python program asks user to enter number of days as input, converts the type to integer. Then it converts the number of days in to years by performing the true division by 365 days.

It Calculates the number of weeks after removing the 365 multiples by performing the modulus by 365 and then performs true division by 7 to get weekends.

In order to calculate the number of days just it perform the days modulus by 7 to get the remaining day in the days.



Complete Python Program
================================

days = int(input("Enter the number of days: "))

years = days // 365
weeks = (days % 365) // 7
day = days % 7

print(f"Years : {years}, Weeks : {weeks} and Days : {day}")



Output
==============

Enter the number of days: 500
Years : 1, Weeks : 19 and Days : 3



 

#5 write a python program to convert year/month/day to day of year in python

Explanation
====================
The number of days in a year is 365 and in Leap Year it is 366. Given the date as input to the below python program it iterates from 1 to given month and keeps adding the days in a Month (i.e, 28/29/30/31) until the for loop is completed.

Once the for loop is completed it prints the day of the year using print function.

The logic is wrapper using if statement to check if user enters valid date or not.


Complete Python Program
============================

day = int(input("Enter the day : "))
month = int(input("Enter the month : "))
year = int(input("Enter the year : "))

day_of_year = day
months = [[2],[4, 6, 9, 11]]

if month < 12 or year > 0 or day <= 31:
   for i in range(1, month):
       if i in months[0]:
           if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
               day_of_year += 29
           else:
               day_of_year += 28
       elif i in months[1]:
           day_of_year += 30
       else:
           day_of_year += 31
   print(f"Its {day_of_year} day of the year {year}")
else:
   print("Invalid Date Entered !!!")




Output
=============

Enter the year: 2023
Enter the month: 12
Enter the day: 31
The day of the year for 2023/12/31 is 365




#6 write a python program to display calendar for the given month and year using calendar module

Explanation
===============

The python program accepts year and month as input to it, an integer type. Use the print function call with calendar.month() function to print the calendar of the month

Complete Python program
==============================

import calendar

year = int(input("Enter the year : "))
month = int(input("Enter the month : "))
print(calendar.month(year, month, 9))



Output
=============

Enter the year : 2023
Enter the month : 5
May 2023
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

 

 

#7 write a python program to generate the next 15 leap years starting from a given year

Explanation
====================

The python program prompts a user to enter an year, converts it to integer type, it checks if year is valid or not using if statement.

It takes a empty lists until the list length if 15 it repeats the while loop and adding the year to the list by checking if the year is leap.

It uses below two condition to check if year is leap or not

  • If the year is divisible by 4 and not divisible by 100, or
  • If the year is divisible by 400.


Complete Python program
========================

year = int(input("Enter Year : "))
if year < 0:
    print("Invalid Year: Year cannot be negative number !")
    exit(1)

years_list = []
while len(years_list) < 15:
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        years_list.append(year)
    year += 1

for item in years_list:
    print(item)



Output
===============

Enter Year : 2023
2024
2028
2032
2036
2040
2044
2048
2052
2056
2060
2064
2068
2072
2076
2080



#8 write a python program to get the last day of a specified year and month


Explanation
===============

Given the year and month as input to the python program, it checks if they are valid. 

The program checks if month is February then it checks if year is leap, it prints end of the month day is 28/29

The program checks if the month is having 30 days September, April, June, and November it prints last day of month is 30  

Finally the program uses else block to print the last day of month is 31 days

 

Complete Python program
=============================

year = int(input("Enter the year : "))
month = int(input("Enter the month : "))

if month < 12 or year > 0:
   months = [[2], [4, 6, 9, 11]]
   if month in months[0]:
       if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
           print("Last Day of This Month is : ", 29)
       else:
           print("Last Day of This Month is : ", 28)
   elif month in months[1]:
       print("Last Day of This Month is : ", 30)
   else:
       print("Last Day of This Month is : ", 31)
else:
   print("Invalid Month / Year")





Output
==================

Enter the year : 2023
Enter the month : 6
Last Day of This Month is :  30




Output 1
===============

Enter the year : 2000
Enter the month : 2
Last Day of This Month is :  29




Output 2
===========

Enter the year : 2023
Enter the month : 12
Last Day of This Month is :  31





#9 write a python program to print a 3-column calendar for an entire year.

Explanation
=================

Import the TextCalendar class from the calendar module, used to generate the calendar of Given Month.

Get the object of TextCalendar class, prompt the user to enter the year and using print module print the calendar using format function call.


Complete Python Program
==============================

from calendar import TextCalendar

text_calendar = TextCalendar()
year = int(input("Enter a Year : "))
print(text_calendar.formatyear(year, 2, 1, 1, 3))



Output
==========

Enter a Year : 2020
2020

January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1
6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29
30 31

April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30

July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
31

October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30






#10 write a python program to read a year of birth from the user and display the corresponding age


Explanation
=======================

Ask the user to enter the year of birth, then get the current year from the datetime.now() function call and store them into two veriables.

Calculate the difference between the current year and the year of birth and finally print the age of the user.

 

Complete python
=======================

import datetime

birth_year = int(input("Enter your year of birth: "))
current_year = datetime.datetime.now().year

age = current_year - birth_year
print(f"You are {age} years old")



Output
============

Enter your year of birth: 1990
You are 33 years old



Output 2
===============

Enter your year of birth: 2000
You are 23 years old





    

Post a Comment

0 Comments