Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to judge even numbers in Python

Shulou Source: shulou.com Published: 2022-06-02 05:51:53 09月25日 Update

This article introduces you how to judge even numbers in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

First, judge odd and even numbers

The smallest even number of natural numbers greater than 0 is 2.

The characteristics of the next even number 2, 4, 6, 8, 10...

According to the law of the above numbers, we can draw the conclusion that all even numbers are integral multiples of the minimum even number 2.

Question: so, how to tell whether a number is even?

Answer:

Judge an even number: if a number is divided by 2 and the remainder is 0, then the number is even, otherwise it is odd.

Actual combat cases in this section

Requirement: the user enters an integer, which represents the number of integers generated at random (the range of random numbers is between 1-100). The output result is that how many of the randomly generated numbers are even numbers, and what are these numbers?

For example:

User input 10

The system begins to randomly generate 10 random integers between 1 and 100

Output result: a total of 5 integers are generated, which are: 2, 10, 30, 36, 60

The thinking of software program practice and analysis:

1. User input uses input function

2. The number of cycles is required

3. Randomly generate numbers between 1 and 100

4. Judge whether it is an integer, calculate the number of integers, and save these integers

5. Output result

So, how to judge odd numbers?

In fact, as long as the opposite side is fine, that is, the case of numb% 2 is odd.

II. Summary and emphasis

1. Master and judge the parity of a number

2. Master the use of random numbers

3. Master the use of circulation

Knowledge source code for this issue:

From random import randint

# n represents the number of output n=input ("Please input how many integers need to be randomly generated?") N=int (n) x=0count=0 # number of records oushu= "" # saves an even number of while x

Tags: Even integer number output between odd result input number user generate minimum representative content that is more knowledge random number help loop Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB MySQL Shulou Information Apple