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 Odd8 để 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 Odd9 là trường thay thế cho
Enter a number: 18 18 is Even0.
Is
Enter a number: 18
18 is Even
1Enter a number: 18
18 is Even
2 >>> name = "//George//"
>>> name.strip["/"]
'George'
>>> name.rstrip["/"]
'//George'
>>> name.lstrip["/"]
'George//'
69>>> name = "//George//"
>>> name.strip["/"]
'George'
>>> name.rstrip["/"]
'//George'
>>> name.lstrip["/"]
'George//'
50>>> name = "//George//"
>>> name.strip["/"]
'George'
>>> name.rstrip["/"]
'//George'
>>> name.lstrip["/"]
'George//'
71
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'72
Enter a number: 18 18 is Even2
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'74
Enter a number: 43 43 is Odd8
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'1
Enter a number: 18 18 is Even2
Enter a number: 18 18 is Even2
Làm thế nào để bạn trích xuất chẵn và số lẻ từ một danh sách trong Python? 2, 4, 6, 8, 10, etc.
Bước 1: Tạo danh sách đầu vào của người dùng. Bước 2: Lấy hai danh sách trống một cho ODD và một cho chẵn. Bước 3: Sau đó, qua mỗi phần tử trong danh sách chính. Bước 4: Mỗi phần tử được chia cho 2, nếu phần còn lại là 0 thì nó thậm chí là số và thêm vào danh sách chẵn, nếu không thì số lẻ của nó và thêm vào danh sách lẻ.1, 3, 5, 7, 9 etc.
Làm thế nào để bạn tìm thấy số lẻ và chẵn của một danh sách nhất định?
Output:
Chương trình Python để kiểm tra xem một số là lẻ hay thậm chí
- Số lẻ và chẵn:
Nếu bạn chia số cho 2 và nó cho phần còn lại là 0 thì nó được gọi là số chẵn, nếu không thì một số lẻ.
Python Basic: Bài tập-21 với giải pháp
Viết một chương trình Python để tìm một số đã cho [chấp nhận từ người dùng] chẵn hoặc lẻ, in ra một tin nhắn thích hợp cho người dùng.
Trình bày hình ảnh của số chẵn:
Trình bày bằng hình ảnh về số lẻ:
Giải pháp mẫu:-:-
Mã Python:
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
Đầu ra mẫu:
Enter a number: 5 This is an odd number.
Thậm chí các số từ 1 đến 100:
Số lẻ từ 1 đến 100:
Flowchart:
Trực quan hóa thực thi mã Python:
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:
Trình chỉnh sửa mã Python:
Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn [và nhận xét] thông qua Disqus.
Trước đây: Viết chương trình Python để có được một chuỗi là N [số nguyên không âm] của một chuỗi đã cho. Write a Python program to get a string which is n [non-negative integer] copies of a given string.
Next:
Write a Python program to count the number 4 in a given list.
Python: Lời khuyên trong ngày
Xóa các ký tự vô dụng ở đầu/kết thúc/cả hai chuỗi:
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'
Đưa ra một danh sách các số, hãy viết một chương trình Python để đếm số và số lẻ trong danh sách. Thí dụ:
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 2
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 2
Ví dụ 1: & nbsp;
Đếm số chẵn và số lẻ từ danh sách đã cho bằng cách lặp để lặp lặp từng phần tử trong danh sách bằng cách sử dụng vòng lặp và kiểm tra xem num % 2 == 0, điều kiện để kiểm tra số chẵn. Nếu điều kiện thỏa mãn, sau đó tăng số lượng khác tăng số lẻ. & NBSP;
Python3
Các
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
8Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.0
Enter a number: 18 18 is Even5
Enter a number: 5 This is an odd number.0
Enter a number: 5 This is an odd number.3
Enter a number: 5 This is an odd number.4
Enter a number: 5 This is an odd number.5
Enter a number: 5 This is an odd number.6
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
Enter a number: 5 This is an odd number.4
Enter a number: 43 43 is Odd8
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'7
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Enter a number: 5 This is an odd number.7
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 22
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'5
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 25
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 21
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 22
‘
Output:
Even numbers in the list: 3 Odd numbers in the list: 4
Ví dụ 2: Sử dụng trong khi Loop & NBSP; Using while loop
Python3
Các
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
8Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.0
Enter a number: 18 18 is Even5
Enter a number: 5 This is an odd number.0
Enter a number: 5 This is an odd number.3
Enter a number: 5 This is an odd number.4
Enter a number: 5 This is an odd number.5
Enter a number: 5 This is an odd number.6
Enter a number: 43 43 is Odd02
Enter a number: 43 43 is Odd03
Enter a number: 43 43 is Odd04
Enter a number: 43 43 is Odd05
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
Enter a number: 5 This is an odd number.4
Enter a number: 43 43 is Odd8
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'7
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Enter a number: 5 This is an odd number.7
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 22
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'5
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 25
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6‘
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 21
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 22
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 25
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 26
Output:
Even numbers in the list: 3 Odd numbers in the list: 4
Ví dụ 2: Sử dụng trong khi Loop & NBSP; Using Python Lambda Expressions
Python3
Các
Enter a number: 5 This is an odd number.4
Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.0
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
Enter a number: 43 43 is Odd08
Enter a number: 43 43 is Odd8
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.4
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 25
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 26
Output:
Even numbers in the list: 3 Odd numbers in the list: 4
Ví dụ 3: Sử dụng các biểu thức Python lambda & nbsp; Using List Comprehension
Python3
Các
Enter a number: 5 This is an odd number.4
Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.0
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
Enter a number: 43 43 is Odd08
Enter a number: 43 43 is Odd8
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.4
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 25
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 26
Output:
Even numbers in the list: 3 Odd numbers in the list: 4
Ví dụ 3: Sử dụng các biểu thức Python lambda & nbsp;Using Recursion
Python3
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 25
Enter a number: 18 18 is Even2
Enter a number: 43 43 is Odd04
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20__162
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'7
Enter a number: 18 18 is Even2
Enter a number: 43 43 is Odd04
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20__162
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 21
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 22
Ví dụ 4: Sử dụng danh sách hiểu & nbsp;
Enter a number: 18 18 is Even15
Enter a number: 18 18 is Even2
Enter a number: 18 18 is Even17
Enter a number: 5 This is an odd number.3
Enter a number: 5 This is an odd number.4
Enter a number: 5 This is an odd number.5
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 25
Enter a number: 18 18 is Even2
Enter a number: 43 43 is Odd04
Enter a number: 18 18 is Even33
Các
Ví dụ 5: Sử dụng đệ quy
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'7
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Enter a number: 5 This is an odd number.7
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 22
Enter a number: 18 18 is Even74
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 25
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6‘
Ví dụ 2: Sử dụng trong khi Loop & NBSP;
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
05Enter a number: 5 This is an odd number.7
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 22
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'5
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 21
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 22
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 25
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 26
Các
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
36
8num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
Enter a number: 18
18 is Even
2 Enter a number: 5
This is an odd number.
0Enter a number: 18
18 is Even
5Enter a number: 5
This is an odd number.
0
Even numbers in the list: 3 Odd numbers in the list: 4
Enter a number: 5
This is an odd number.
3 Enter a number: 5
This is an odd number.
4Enter a number: 5
This is an odd number.
5 Enter a number: 5
This is an odd number.
6Using Bitwise XOR operator
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
Enter a number: 5 This is an odd number.4
Enter a number: 43 43 is Odd8
Kiểm tra xem số chẵn hoặc lẻ bằng toán tử XOR
Python3
Các
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
8Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.0
Enter a number: 18 18 is Even5
Enter a number: 5 This is an odd number.0
Enter a number: 5 This is an odd number.3
Enter a number: 5 This is an odd number.4
Enter a number: 5 This is an odd number.5
Enter a number: 5 This is an odd number.6
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
65num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6 Enter a number: 18 18 is Even2
Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.4__
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'7
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Enter a number: 5 This is an odd number.7
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 22
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'5
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 25
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 21
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 22
‘
Đầu ra
Even numbers in the list: 3 Odd numbers in the list: 4
Ví dụ 7: Sử dụng bitwise và toán tửUsing Bitwise AND operator
Ý tưởng là kiểm tra xem bit cuối cùng của số có được đặt hay không. Nếu bit cuối cùng được đặt thì số này là số lẻ, nếu không thì ngay cả chúng ta biết bitwise và hoạt động của số bằng 1 sẽ là 1, nếu nó là số lẻ vì bit cuối cùng sẽ được đặt. Nếu không, nó sẽ cung cấp 0 dưới dạng đầu ra. & NBSP;
As we know bitwise AND Operation of the Number by 1 will be 1, If it is odd because the last bit will be already set. Otherwise, it will give 0 as output.
Python3
Các
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
8Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.0
Enter a number: 18 18 is Even5
Enter a number: 5 This is an odd number.0
Enter a number: 5 This is an odd number.3
Enter a number: 5 This is an odd number.4
Enter a number: 5 This is an odd number.5
Enter a number: 5 This is an odd number.6
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
65num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6 Enter a number: 18 18 is Even2
Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.4__
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'7
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Enter a number: 5 This is an odd number.7
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 22
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'5
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 25
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 21
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 22
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 25
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 26
Đầu ra
Even numbers in the list: 3 Odd numbers in the list: 4
Ví dụ 7: Sử dụng bitwise và toán tử: Using Bitwise OR operator
Ý tưởng là kiểm tra xem bit cuối cùng của số có được đặt hay không. Nếu bit cuối cùng được đặt thì số này là số lẻ, nếu không thì ngay cả chúng ta biết bitwise và hoạt động của số bằng 1 sẽ là 1, nếu nó là số lẻ vì bit cuối cùng sẽ được đặt. Nếu không, nó sẽ cung cấp 0 dưới dạng đầu ra. & NBSP;
Python3
Các
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
8Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.0
Enter a number: 18 18 is Even5
Enter a number: 5 This is an odd number.0
Enter a number: 5 This is an odd number.3
Enter a number: 5 This is an odd number.4
Enter a number: 5 This is an odd number.5
Enter a number: 5 This is an odd number.6
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
65num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6 Enter a number: 18 18 is Even2
Enter a number: 18 18 is Even2
Enter a number: 5 This is an odd number.4__
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'7
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Enter a number: 5 This is an odd number.7
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 22
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'5
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 25
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 21
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 22
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 25
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 26
Đầu ra
Even numbers in the list: 3 Odd numbers in the list: 4
Ví dụ 7: Sử dụng bitwise và toán tử
Python3
Ý tưởng là kiểm tra xem bit cuối cùng của số có được đặt hay không. Nếu bit cuối cùng được đặt thì số này là số lẻ, nếu không thì ngay cả chúng ta biết bitwise và hoạt động của số bằng 1 sẽ là 1, nếu nó là số lẻ vì bit cuối cùng sẽ được đặt. Nếu không, nó sẽ cung cấp 0 dưới dạng đầu ra. & NBSP;
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
Enter a number: 5 This is an odd number.22
Enter a number: 5 This is an odd number.23
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'5
Ví dụ 8: Sử dụng bitwise hoặc toán tử
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'30
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Enter a number: 5 This is an odd number.7
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 22
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'5
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'6
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'38
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'8
Enter a number: 18 18 is Even2
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'44
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'45
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'46
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'47
Đầu ra
Enter a number: 43 43 is Odd6
Ví dụ 7: Sử dụng bitwise và toán tử
Ý tưởng là kiểm tra xem bit cuối cùng của số có được đặt hay không. Nếu bit cuối cùng được đặt thì số này là số lẻ, nếu không thì ngay cả chúng ta biết bitwise và hoạt động của số bằng 1 sẽ là 1, nếu nó là số lẻ vì bit cuối cùng sẽ được đặt. Nếu không, nó sẽ cung cấp 0 dưới dạng đầu ra. & NBSP;
Enter a number: 5
This is an odd number.
7Enter a number: 5
This is an odd number.
8 Enter a number: 5
This is an odd number.
22 Enter a number: 5
This is an odd number.
23num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6>>> name = "//George//"
>>> name.strip["/"]
'George'
>>> name.rstrip["/"]
'//George'
>>> name.lstrip["/"]
'George//'
5
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
Ví dụ 8: Sử dụng bitwise hoặc toán tử
Ý tưởng là kiểm tra xem bit cuối cùng của số có được đặt hay không. Nếu bit cuối cùng được đặt thì số là số lẻ, nếu không thì thậm chí. Như chúng ta đã biết bitwise hoặc hoạt động của số lên 1 tăng giá trị của số lên 1 nếu số thậm chí còn không thay đổi. Vì vậy, nếu sau hoặc hoạt động của số với 1 cho kết quả lớn hơn số thì đó là chẵn và chúng tôi sẽ trả về đúng nếu không nó là số lẻ và chúng tôi sẽ trả về sai.
Enter a number: 5 This is an odd number.7
Enter a number: 5 This is an odd number.8
Enter a number: 5 This is an odd number.75
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
6 Enter a number: 5 This is an odd number.77
Phương pháp: Sử dụng hàm liệt kê & nbsp;
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 21
Enter a number: 18 18 is Even5
Enter a number: 43 43 is Odd04
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'86
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 29
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 20
Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 25
Enter a number: 18 18 is Even5
Enter a number: 43 43 is Odd04
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'92
Enter a number: 18 18 is Even40
Enter a number: 43 43 is Odd04
>>> name = "//George//" >>> name.strip["/"] 'George' >>> name.rstrip["/"] '//George' >>> name.lstrip["/"] 'George//'86
Output:
Even numbers in the list: 3 Odd numbers in the list: 4