Một số là ngay cả khi nó hoàn toàn chia hết cho 2. Khi số được chia cho 2, chúng tôi sử dụng toán tử còn lại
Enter a number: 43 43 is Odd0 để tính toán phần còn lại. Nếu phần còn lại không bằng không, số là số lẻ.
Mã nguồn
# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.
num = int[input["Enter a number: "]]
if [num % 2] == 0:
print["{0} is Even".format[num]]
else:
print["{0} is Odd".format[num]]
Đầu ra 1
Enter a number: 43 43 is Odd
Đầu ra 2
Enter a number: 18 18 is Even
Trong chương trình này, chúng tôi yêu cầu người dùng cho đầu vào và kiểm tra xem số này là lẻ hay chẵn. Xin lưu ý rằng
Enter a number: 43 43 is Odd1 là trường thay thế cho
Enter a number: 43 43 is Odd2.
Làm thế nào để bạn tìm thấy số lẻ?
Làm thế nào để bạn xác định xem một số là lẻ hay thậm chí? Nếu một số chia đều cho 2 thì đó là một số chẵn, nếu không, đó là một số lẻ. Chúng ta cũng có thể nói khi chúng ta chia số cho 2 và còn lại một số phần còn lại, điều này không chia hết cho 2, thì đó là một số lẻ.
Làm thế nào để bạn kiểm tra xem một số có kỳ lạ trong danh sách Python không?
Làm thế nào để bạn tìm thấy số lẻ?
Làm thế nào để bạn xác định xem một số là lẻ hay thậm chí? Nếu một số chia đều cho 2 thì đó là một số chẵn, nếu không, đó là một số lẻ. Chúng ta cũng có thể nói khi chúng ta chia số cho 2 và còn lại một số phần còn lại, điều này không chia hết cho 2, thì đó là một số lẻ.
Làm thế nào để bạn kiểm tra xem một số có kỳ lạ trong danh sách Python không?
Cách pythonic nhất để kiểm tra xem danh sách có số phần tử lẻ là sử dụng biểu thức modulo LEN [My_list]%2 trả về 1 nếu độ dài danh sách là lẻ và 0 nếu độ dài danh sách đều. Vì vậy, để kiểm tra xem danh sách có số phần tử lẻ sử dụng biểu thức LEN [my_list]%2 == 1.
Example:
Số lẻ trong Python là gì?Một số là ngay cả khi nó hoàn toàn chia hết cho 2. Khi số được chia cho 2, chúng tôi sử dụng % toán tử còn lại % để tính phần còn lại. Nếu phần còn lại không bằng không, số là số lẻ. Print all odd numbers from the given list using for loop
- Làm thế nào để bạn in 5 số lẻ trong Python?
- start = int [input ["Nhập bắt đầu phạm vi:"]] end = int [input ["Nhập phần cuối của phạm vi:"]].
- # Lặp lại từng số trong danh sách. cho num trong phạm vi [bắt đầu, kết thúc + 1]:.
- # Kiểm tra điều kiện. Nếu Num % 2! = 0: In [Num].
Python3
Enter a number: 43 43 is Odd3
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd5
Enter a number: 43 43 is Odd6
Enter a number: 43 43 is Odd7
Enter a number: 43 43 is Odd8
Enter a number: 43 43 is Odd9
Enter a number: 18 18 is Even0
Enter a number: 18 18 is Even6
Enter a number: 18 18 is Even7
Enter a number: 43 43 is Odd9
Enter a number: 43 43 is Odd0
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 110
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 111
Enter a number: 43 43 is Odd4
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 113
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 114
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 115
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 117
Enter a number: 43 43 is Odd4
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 119
5 7 9 11 13 15 17 190
Output:
5 7 9 11 13 15 17 19
& nbsp; Ví dụ #2: Lấy giới hạn phạm vi từ đầu vào của người dùng & nbsp;Example #2: Taking range limit from user input
Python3
5 7 9 11 13 15 17 191
Enter a number: 43 43 is Odd4
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 195
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 197
5 7 9 11 13 15 17 198
5 7 9 11 13 15 17 199
Enter a number: 43 43 is Odd4
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 195
5 7 9 11 13 15 17 1944455554
Enter a number: 43 43 is Odd8
Enter a number: 43 43 is Odd9
Enter a number: 18 18 is Even0
Enter a number: 18 18 is Even6
Enter a number: 18 18 is Even7
Enter a number: 43 43 is Odd9
Enter a number: 43 43 is Odd0
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 110
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 111
Enter a number: 43 43 is Odd4
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 113
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 114
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 115
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 116
Output:
Enter the start of range: 3 Enter the end of range: 7 3 5 7
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
5Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
6Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
7Enter a number: 43
43 is Odd
4 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
95 7 9 11 13 15 17 19
0 Taking range limit from user input or with static inputs to reduce code execution time and to increase code performance.
Python3
& nbsp; Ví dụ #2: Lấy giới hạn phạm vi từ đầu vào của người dùng & nbsp;
5 7 9 11 13 15 17 191
Enter a number: 43 43 is Odd4
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 195
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 197
5 7 9 11 13 15 17 198
5 7 9 11 13 15 17 199
Enter a number: 43 43 is Odd4
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 195
5 7 9 11 13 15 17 1944455554
Ví dụ #3: Lấy giới hạn phạm vi từ đầu vào của người dùng hoặc với đầu vào tĩnh để giảm thời gian thực hiện mã và tăng hiệu suất mã.
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 115
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
Enter a number: 43 43 is Odd04
Enter a number: 43 43 is Odd4
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 119
5 7 9 11 13 15 17 190
Enter a number: 43 43 is Odd08
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 114
5 7 9 11 13 15 17 191
Enter a number: 43 43 is Odd4
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 119
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 115
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
Enter a number: 43 43 is Odd04
Enter a number: 43 43 is Odd4
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 119
5 7 9 11 13 15 17 190
5 7 9 11 13 15 17 19
9Enter a number: 43
43 is Odd
4 [5, 7, 9, 11, 13, 15]
2
5 7 9 11 13 15 17 19
Enter a number: 18
18 is Even
7 5 7 9 11 13 15 17 19
1Enter a number: 43
43 is Odd
0 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
0 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
1Enter a number: 43
43 is Odd
4 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
3Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
4 Taking range limit from user input
Python3
Enter a number: 18 18 is Even6
Enter a number: 43 43 is Odd8
Enter a number: 43 43 is Odd9
Enter a number: 18 18 is Even0
Enter a number: 18 18 is Even1
Enter a number: 18 18 is Even2223
Enter a number: 18 18 is Even6
Enter a number: 43 43 is Odd8
Enter a number: 43 43 is Odd9
Enter a number: 18 18 is Even0
Enter a number: 18 18 is Even1
Enter a number: 43 43 is Odd15
Enter a number: 18 18 is Even3__
Đầu ra
Ví dụ #4: Lấy giới hạn phạm vi từ đầu vào của người dùng & nbsp;
5 7 9 11 13 15 17 191
Enter a number: 43 43 is Odd4
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194___
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 11
5 7 9 11 13 15 17 19
9Enter a number: 43
43 is Odd
4 5 7 9 11 13 15 17 19
35 7 9 11 13 15 17 19
45 7 9 11 13 15 17 19
55 7 9 11 13 15 17 19
4Enter a number: 43
43 is Odd
445 7 9 11 13 15 17 19
8
Python3
Enter a number: 43 43 is Odd68
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd70
Enter a number: 43 43 is Odd71
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd73
Enter a number: 43 43 is Odd74
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd76
Enter a number: 43 43 is Odd46
Enter a number: 43 43 is Odd4
Enter a number: 18 18 is Even1
Enter a number: 18 18 is Even2223
Enter a number: 18 18 is Even4
Enter a number: 43 43 is Odd522
Enter a number: 43 43 is Odd0________________
Enter a number: 43 43 is Odd55
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 110 ____157
Enter a number: 18 18 is Even6
Enter a number: 43 43 is Odd86
Enter a number: 43 43 is Odd8
Enter a number: 43 43 is Odd9
Enter a number: 18 18 is Even0
Enter a number: 43 43 is Odd61
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
Enter a number: 18 18 is Even02
Enter a number: 18
18 is Even
6Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
6Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
7Enter a number: 43
43 is Odd
4 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
95 7 9 11 13 15 17 19
0
Python3
Phương pháp: Sử dụng chức năng Lambda
Enter a number: 43 43 is Odd8
Enter a number: 43 43 is Odd78
Enter a number: 18 18 is Even0
Enter a number: 18 18 is Even1
Enter a number: 43 43 is Odd81
Enter a number: 18 18 is Even3
Enter a number: 18 18 is Even4
Enter a number: 18 18 is Even5
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 115
Enter a number: 18 18 is Even09
Enter a number: 18 18 is Even6
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
Enter a number: 18 18 is Even12
Enter a number: 18 18 is Even3
Enter a number: 18 18 is Even4
Enter a number: 18 18 is Even15
Enter a number: 43 43 is Odd4
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 119
5 7 9 11 13 15 17 190
Enter a number: 43 43 is Odd87
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd89
5 7 9 11 13 15 17 194
Enter a number: 43 43 is Odd91
5 7 9 11 13 15 17 194
Enter a number: 43 43 is Odd93
Enter a number: 43 43 is Odd94
Enter a number: 43 43 is Odd0________________
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 111
Enter a number: 43 43 is Odd4
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 113__
Enter a number: 18 18 is Even25
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd5
Enter a number: 18 18 is Even28
Enter a number: 43 43 is Odd4
Enter a number: 18 18 is Even30
Enter a number: 18 18 is Even31
Phương pháp: Sử dụng đệ quy & nbsp;
Python3
Enter a number: 18 18 is Even03
Enter a number: 18 18 is Even04
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
5 7 9 11 13 15 17 194
Enter a number: 18 18 is Even56
Enter a number: 18 18 is Even57
Enter a number: 18
18 is Even
6Enter a number: 18
18 is Even
7 Enter a number: 18
18 is Even
07
Python3
Enter a number: 43 43 is Odd68
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd5
Enter a number: 43 43 is Odd71
Enter a number: 43 43 is Odd4
Enter a number: 18 18 is Even30
Enter a number: 18 18 is Even64
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd76
Enter a number: 43 43 is Odd46
Enter a number: 43 43 is Odd4
Enter a number: 18 18 is Even1
Enter a number: 18 18 is Even2223
Enter a number: 18 18 is Even4
Enter a number: 43 43 is Odd522
Enter a number: 43 43 is Odd0________________
Enter a number: 43 43 is Odd55
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 110 ____157
Enter a number: 18 18 is Even75
Enter a number: 18 18 is Even76
Enter a number: 43 43 is Odd8
Enter a number: 43 43 is Odd9
Enter a number: 18 18 is Even0
Enter a number: 43 43 is Odd61
5 7 9 11 13 15 17 19
9Enter a number: 43
43 is Odd
4 [5, 7, 9, 11, 13, 15]
2
[5, 7, 9, 11, 13, 15]
Enter a number: 18
18 is Even
7 5 7 9 11 13 15 17 19
1Enter a number: 43
43 is Odd
0 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
0 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
1Enter a number: 43
43 is Odd
4 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
3Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
4
Python3
Enter a number: 43 43 is Odd68
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd5
Enter a number: 43 43 is Odd71
Enter a number: 43 43 is Odd4
Enter a number: 18 18 is Even30
Enter a number: 43 43 is Odd46
Enter a number: 43 43 is Odd4
Enter a number: 18 18 is Even1
Enter a number: 18 18 is Even2223
Enter a number: 18 18 is Even4
Enter a number: 43 43 is Odd522
Enter a number: 43 43 is Odd0________________
Enter a number: 43 43 is Odd55
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 110 ____157
Enter a number: 43 43 is Odd8
Enter a number: 43 43 is Odd9
Enter a number: 18 18 is Even0
Enter a number: 43 43 is Odd61
Enter a number: 18 18 is Even6
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 1116
Enter a number: 18 18 is Even75
Enter a number: 43 43 is Odd08
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 114
Enter a number: 18 18 is Even6
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 1122
Enter a number: 43 43 is Odd4
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 119
5 7 9 11 13 15 17 190
Enter a number: 18
18 is Even
6Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
6Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
7Enter a number: 43
43 is Odd
4 Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
Input: start = 3, end = 11
Output: 3, 5, 7, 9, 11
95 7 9 11 13 15 17 19
0
Python3
Enter a number: 43 43 is Odd68
Enter a number: 43 43 is Odd4
Enter a number: 43 43 is Odd5
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 1129
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 1130
Enter a number: 43 43 is Odd4
Enter a number: 18 18 is Even30
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 1129
Phương pháp: Sử dụng chức năng Lambda
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 116
5 7 9 11 13 15 17 194
Enter a number: 18 18 is Even56
Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 1151
Output:
5 7 9 11 13 15