Coding Exercise for Beginners in Python | Exercise 6| Python for Beginners #lec25
Understanding Even and Odd Numbers in Python
Introduction to Conditional Statements
- The video begins with a recap of previous lessons on conditional statements in Python, specifically focusing on
ifandif-elsestatements.
- The instructor introduces a coding exercise where the goal is to determine if a given number is even or odd. An even number is defined as one that can be divided by 2 without any remainder.
Using the Modulo Operator
- The modulo operator (
%) is highlighted as essential for checking remainders when dividing numbers, which will help identify even numbers.
- Viewers are encouraged to pause the video and attempt the coding exercise themselves, reinforcing hands-on learning.
Coding Exercise Steps
- The instructor demonstrates how to create a new Python file named
coding_exercise_6.pyfor this task.
- Input from the user is taken using the
input()function, which captures a number that needs to be checked for its parity (even or odd).
Implementing Conditions
- A condition checks if the input number modulo 2 equals zero; if true, it prints "this is an even number."
- If false, it indicates that "this is our number," implying it's odd. The instructor notes flexibility in using single or double quotes for strings in Python.
Running and Testing Code
- Upon running the code with an example input of 34, it correctly identifies it as an even number.
- When testing with 33, it shows that there’s a remainder of 1, confirming it's an odd number. This reinforces understanding through practical examples.