Common Python Coding Interview Questions: Part 5

Common Python Coding Interview Questions: Part 5

A breakdown of a common Python problem given in interviews. Write a function that takes in a year and returns ‘True’ or ‘False’ whether that year is a leap year. In the Gregorian calendar, three conditions are used to identify leap years:

Write a function that takes in a year and returns ‘True’ or ‘False’ whether that year is a leap year. In the Gregorian calendar, three conditions are used to identify leap years:

  • The year can be evenly divided by 4, is a leap year, unless:
  • The year can be evenly divided by 100, it is NOT a leap year, unless:
  • The year is also evenly divisible by 400. Then it is a leap year.
def is_leap(year):
    leap = False
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                leap = True
        else:
            leap = True
    return leap

Step 1: Declare the function

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that the function will be taking in a year, be sure to include one argument in your function declaration that will represent that year.

Step 2: Set variable as ‘False’ by default

Since the majority of years are not leap years, it makes sense to initiate the return value as ‘False’ from the get-go. Doing this prevents you from having to have a lot of unnecessary ‘leap == False’ statements in your code. Only a very specific set of conditions will change leap to ‘True’ and otherwise leap will simply stay as ‘False’.

software-development coding-interviews interview-questions python coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top 130 Android Interview Questions - Crack Technical Interview Now!

Top Android Interview Questions & Answers from Beginner to Advanced level. Get ready to crack your next android interview with these android interview questions

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Common Python Coding Interview Questions: Part 5

A breakdown of a common Python problem given in interviews. Write a function that takes in a year and returns ‘True’ or ‘False’ whether that year is a leap year. In the Gregorian calendar, three conditions are used to identify leap years:

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...