Leap year check in Python: Here, we will learn how to check a given year is a
leap year or not in Python programming language?
A leap year is a year that is completely divisible by 4 except the century
year (a year that ended with 00). A century year is a leap year if it is divisible by
400. Here, a year is provided by the user and we have to check whether the given
year is a leap year or not. This problem, we will solve in two ways first by using
the calendar module and second by simply checking the leap year condition.
1) By using the calendar module
Before going to solve the problem, initially, we learn a little bit about the calendar
module. Calendar module is inbuilt in Python which provides us various functions
to solve the problem related to date, month and year.
Program:
# importing the module
import calendar
# input the year
year=int(input('Enter the value of year: '))
leap_year=calendar.isleap(year)
# checking leap year
if leap_year: # to check condition
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2021
The given year is a non-leap year.
2) By simply checking the leap year condition
As we know the condition to check the given year is a leap year or not.
So, here we will implement the condition and try to write the Python program.
Program:
# input the year
y=int(input('Enter the value of year: '))
# To check for non century year
if y%400==0 or y%4==0 and y%100!=0:
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Python | Calculate square of a given number (3 different ways)
Here, we are going to implement python programs to calculate square of a given
number using 3 different ways.
Calculating square is a basic operation in mathematics; here we are calculating the
square of a given number by using 3 methods.
1. By multiplying numbers two times: (number*number)
2. By using Exponent Operator (**): (number**2)
3. By using math.pow() method: (math.pow(number,2)
1) By multiplying numbers two times: (number*number)
To find the square of a number - simple multiple the number two times.
Program:
# Python program to calculate square of a number
# Method 1 (using number*number)
# input a number
number = int (raw_input ("Enter an integer number: "))
# calculate square
square = number*number
# print
print "Square of {0} is {1} ".format (number, square)
Output
Enter an integer number: 8
Square of 8 is 64
2) By using Exponent Operator (**): (number**2)
The another way is to find the square of a given number is to use Exponent
Operator (**), it returns the exponential power. This operator is represented by **
Example: Statement m**n will be calculated as "m to the power of n".
Program:
# Python program to calculate square of a number
# Method 2 (using number**2)
# input a number
number = int (raw_input ("Enter an integer number: "))
# calculate square
square = number**2
# print
print "Square of {0} is {1} ".format (number, square)
Output
Enter an integer number: 8
Square of 8 is 64
3) By using math.pow() method: (math.pow(number,2)
pow(m,n) is an inbuilt method of math library, it returns the value of "m to the power
n". To use this method, we need to import math library in the program.
The statement to import math library is import math.
Program:
# Python program to calculate square of a number
# Method 3 (using math.pow () method)
# importing math library
import math
# input a number
number = int (raw_input ("Enter an integer number: "))
# calculate square
square = int(math.pow (number, 2))
# print
print "Square of {0} is {1} ".format (number, square)
Output
Enter an integer number: 8
Square of 8 is 64