How to Display Even Numbers in Python: A Step-by-Step Guide

TLDRLearn how to write a Python program to display even numbers between 1 to 10 using a for loop and the range function. Also, discover how to count the total number of even numbers and print the result.

Key insights

💡Iterating over all numbers in a given range and checking whether each number is even using the modulus operator.

🔍Using the range function to generate a sequence of numbers from 1 to 10.

🔢Incrementing a count variable every time an even number is encountered.

🖨️Printing the even numbers on the terminal using the print function.

Formatting and printing the count of even numbers using a formatted string.

Q&A

How do I write a Python program to display even numbers?

You can use a for loop and the range function to iterate over the numbers from 1 to 10. Inside the loop, check if each number is even using the modulus operator. If it is, print the number on the terminal.

What is the modulus operator in Python?

The modulus operator (%) returns the remainder of a division operation. In the context of finding even numbers, if a number modulus 2 equals zero, it means the number is even.

How do I count the total number of even numbers in Python?

To count the total number of even numbers, you can use a separate variable initialized to zero. Inside the loop, increment the count variable by one every time an even number is encountered.

Can I use a different range of numbers instead of 1 to 10?

Yes, you can modify the range function arguments to specify a different range of numbers. For example, range(1, 100, 2) will generate odd numbers from 1 to 99.

What other tutorials are available on Muhammad Ani's channel?

Muhammad Ani has a comprehensive Python tutorial on his channel, covering various topics from scratch. You can find the link to the tutorial below this video.

Timestamped Summary

00:03Introduction to the exercise: Writing a program to display even numbers between 1 to 10.

00:26Hint: Use the range function with arguments 1 and 10 to iterate over all numbers.

01:02Check if the remainder of the division of each number by 2 is zero to find even numbers.

01:18Running the program and printing the even numbers: 2 4 6 8.

01:36Counting the total number of even numbers using a separate variable.

01:52Final result: Print the count of even numbers using a formatted string.

02:01Subscribe to Muhammad Ani's channel for more tutorials and like and share this video.

02:10Link to a comprehensive Python tutorial by Muhammad Ani in the video description.