Check if a Number is Odd

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In programming, it is often necessary to check whether a given number is odd or even. This can be useful in various scenarios, such as when working with loops, conditional statements, or mathematical operations. In this challenge, you will be asked to write a function that checks whether a given number is odd or even.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") subgraph Lab Skills python/comments -.-> lab-13671{{"`Check if a Number is Odd`"}} python/booleans -.-> lab-13671{{"`Check if a Number is Odd`"}} python/function_definition -.-> lab-13671{{"`Check if a Number is Odd`"}} end

Check if a Number is Odd

Write a function called is_odd that takes a single integer as an argument and returns True if the number is odd, and False if the number is even. Your function should perform the following steps:

  1. Use the modulo (%) operator to check whether the number is even or odd.
  2. If the number is odd, return True. If the number is even, return False.
def is_odd(num):
  return num % 2 != 0
is_odd(3) ## True

Summary

In this challenge, you have learned how to write a function that checks whether a given number is odd or even. This can be useful in various programming scenarios, such as when working with loops, conditional statements, or mathematical operations. Remember to use the modulo operator to check whether a number is odd or even, and to return True if the number is odd, and False if the number is even.

Other Python Tutorials you may like