Hướng dẫn odd even python - trăn chẵn lẻ

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 Odd
9 để 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ẻ.

Nội phân chính

  • Mã nguồn
  • Mã nguồn
  • Chương trình Python để kiểm tra xem một số là lẻ hay thậm chí
  • Python Basic: Bài tập-21 với giải pháp
  • Trực quan hóa thực thi mã Python:
  • Python: Lời khuyên trong ngày
  • Phương pháp: Sử dụng hàm liệt kê & nbsp;

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))

Chương trình Python để kiểm tra xem một số là lẻ hay thậm chí

Enter a number: 43
43 is Odd

Python Basic: Bài tập-21 với giải pháp

Enter a number: 18
18 is Even

Trực quan hóa thực thi mã Python:

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 Odd
9 để 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ẻ.

Nội phân chính

  • Mã nguồn
  • Chương trình Python để kiểm tra xem một số là lẻ hay thậm chí
  • Python Basic: Bài tập-21 với giải pháp
  • Python Basic: Bài tập-21 với giải pháp
  • Trực quan hóa thực thi mã Python:
  • Python: Lời khuyên trong ngày
  • Phương pháp: Sử dụng hàm liệt kê & nbsp;

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))

Chương trình Python để kiểm tra xem một số là lẻ hay thậm chí

Enter a number: 43
43 is Odd

Python Basic: Bài tập-21 với giải pháp

Enter a number: 18
18 is Even

Trực quan hóa thực thi mã Python:

Chương trình Python để kiểm tra xem một số là lẻ hay thậm chí

Python Basic: Bài tập-21 với giải pháp

Trực quan hóa thực thi mã Python:

Python: Lời khuyên trong ngày 2, 4, 6, 8, 10, etc.

Phương pháp: Sử dụng hàm liệt kê & nbsp;1, 3, 5, 7, 9 etc.

Đầu ra 1

Output:


Đầu ra 2

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: 18 18 is Even 0 là trường thay thế cho Enter a number: 18 18 is Even 1.

  • Giúp đỡ người khác, xin vui lòng chia sẻ





Số lẻ và chẵn:

Python Basic: Bài tập-21 với giải pháp

Trực quan hóa thực thi mã Python:

Python: Lời khuyên trong ngày

Phương pháp: Sử dụng hàm liệt kê & nbsp;

Đầu ra 1:-

Đầu ra 2

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.")	

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: 18
18 is Even
0 là trường thay thế cho
Enter a number: 18
18 is Even
1.

Enter a number: 5                                                                                             
This is an odd number. 

Giúp đỡ người khác, xin vui lòng chia sẻ

Số lẻ và chẵn:

Flowchart:


Trực quan hóa thực thi mã Python:

Python: Lời khuyên trong ngày

Phương pháp: Sử dụng hàm liệt kê & nbsp;

Đầu ra 1

Đầu ra 2 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

Phương pháp: Sử dụng hàm liệt kê & nbsp;

from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem

Đầu ra 1

Input: list1 = [2, 7, 5, 64, 14]
Output: Even = 3, odd = 2

Input: list2 = [12, 14, 95, 3]
Output: Even = 2, odd = 2

Đầu ra 2

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: 18 18 is Even 0 là trường thay thế cho Enter a number: 18 18 is Even 1.

Giúp đỡ người khác, xin vui lòng chia sẻ

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ẻ.

Ví dụ số chẵn: 2, 4, 6, 8, 10, v.v.

Ví dụ số lẻ: 1, 3, 5, 7, 9, v.v.

Enter a number: 18
18 is Even
1
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: 18
18 is Even
9

Xem ví dụ này:

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

Đối với video, hãy tham gia kênh YouTube của chúng tôi: Tham gia ngay

Output:

Enter a number: 43
43 is Odd
0

Nhận xét

Python3

Gửi phản hồi của bạn đến [Email & NBSP; được bảo vệ]

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ẻ.

Enter a number: 43
43 is Odd
06
Enter a number: 43
43 is Odd
07
Enter a number: 43
43 is Odd
08
Enter a number: 43
43 is Odd
09

Ví dụ số chẵn: 2, 4, 6, 8, 10, v.v.

Ví dụ số lẻ: 1, 3, 5, 7, 9, v.v.

Xem ví dụ này:

Xem ví dụ này:

Đối với video, hãy tham gia kênh YouTube của chúng tôi: Tham gia ngay

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. 
3
Enter a number: 5                                                                                             
This is an odd number. 
4
Enter a number: 5                                                                                             
This is an odd number. 
9
from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem
0

Output:

Enter a number: 43
43 is Odd
0

Nhận xét

Python3

Gửi phản hồi của bạn đến [Email & NBSP; được bảo vệ]

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:48 (UTC/GMT +8 giờ)

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.")	
1
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
08
Enter a number: 5                                                                                             
This is an odd number. 
4
Enter a number: 43
43 is Odd
66__

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. 
3
Enter a number: 5                                                                                             
This is an odd number. 
4
Enter a number: 5                                                                                             
This is an odd number. 
9
from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem
0

Output:

Enter a number: 43
43 is Odd
0

Ví dụ 4: Sử dụng danh sách hiểu & nbsp;

Python3

Enter a number: 18
18 is Even
5
Enter a number: 18
18 is Even
6
Enter a number: 18
18 is Even
7
Enter a number: 18
18 is Even
8
Enter a number: 18
18 is Even
9
# 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))
0____29
# 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))
2
Enter a number: 18
18 is Even
9
# 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))
4
Enter a number: 18
18 is Even
9
# 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))
6____29____
# 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))
8
Enter a number: 18
18 is Even
9
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.")	
Enter a number: 43
43 is Odd
1

Enter a number: 18
18 is Even
19
Enter a number: 18
18 is Even
6
Enter a number: 18
18 is Even
21
Enter a number: 43
43 is Odd
7
Enter a number: 43
43 is Odd
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.")	
9
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
08
Enter a number: 18
18 is Even
37

Các

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. 
9
from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem
0

Output:

Enter a number: 43
43 is Odd
0

Ví dụ 5: Sử dụng đệ quyUsing Recursion

Python3

Enter a number: 18
18 is Even
50
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
4

Enter a number: 18
18 is Even
53
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
4

Enter a number: 18
18 is Even
56
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
4

Enter a number: 18
18 is Even
59
Enter a number: 18
18 is Even
60

Enter a number: 18
18 is Even
1
Enter a number: 18
18 is Even
62
Enter a number: 18
18 is Even
50

Enter a number: 18
18 is Even
1
Enter a number: 18
18 is Even
62
Enter a number: 18
18 is Even
56

Enter a number: 18
18 is Even
1
Enter a number: 18
18 is Even
62
Enter a number: 18
18 is Even
53

Enter a number: 18
18 is Even
1
Enter a number: 18
18 is Even
2
Enter a number: 18
18 is Even
72
Enter a number: 43
43 is Odd
9
Enter a number: 18
18 is Even
55____26
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
444

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.")	
0
Enter a number: 18
18 is Even
50
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.")	
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
0

Enter a number: 18
18 is Even
1
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: 18
18 is Even
9

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.")	
0
Enter a number: 18
18 is Even
56
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.")	
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
0

Enter a number: 18
18 is Even
1
Enter a number: 18
18 is Even
2
Enter a number: 18
18 is Even
94
Enter a number: 43
43 is Odd
9
Enter a number: 18
18 is Even
96
Enter a number: 5                                                                                             
This is an odd number. 
4
Enter a number: 43
43 is Odd
08
Enter a number: 18
18 is Even
99
Enter a number: 18
18 is Even
444404040302

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.")	
0
Enter a number: 18
18 is Even
53
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.")	
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
0

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.")	
0
# 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))
09

Enter a number: 18
18 is Even
1
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: 18
18 is Even
9

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.")	
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

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.")	
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. 
9
from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem
0

Enter a number: 18
18 is Even
5
Enter a number: 18
18 is Even
6
Enter a number: 18
18 is Even
7
Enter a number: 18
18 is Even
8
Enter a number: 18
18 is Even
9
# 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))
0____29
# 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))
2
Enter a number: 18
18 is Even
9
# 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))
4
Enter a number: 18
18 is Even
9
# 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))
6____29____
# 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))
8
Enter a number: 18
18 is Even
9
Enter a number: 43
43 is Odd
0
Enter a number: 43
43 is Odd
1

# 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))
40

Đầu ra

Enter a number: 43
43 is Odd
0

Ví dụ 6: Sử dụng toán tử BitWise XORUsing Bitwise XOR 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ố là số lẻ, nếu không thì thậm chí. Như chúng ta đã biết hoạt động XOR bitwise của số lên 1 tăng giá trị của số lên 1 nếu số thậm chí còn giảm giá trị của số xuống 1 nếu giá trị là số lẻ.

Kiểm tra xem số chẵn hoặc lẻ bằng toán tử XOR


Python3

Enter a number: 18
18 is Even
5
Enter a number: 18
18 is Even
6
Enter a number: 18
18 is Even
7
Enter a number: 18
18 is Even
8
Enter a number: 18
18 is Even
9
# 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))
0____29
# 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))
2
Enter a number: 18
18 is Even
9
# 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))
4
Enter a number: 18
18 is Even
9
# 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))
6____29____
# 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))
8
Enter a number: 18
18 is Even
9
Enter a number: 43
43 is Odd
0
Enter a number: 43
43 is Odd
1

Đầu ra

Ví dụ 6: Sử dụng toán tử BitWise XOR

Ý 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 hoạt động XOR bitwise của số lên 1 tăng giá trị của số lên 1 nếu số thậm chí còn giảm giá trị của số xuống 1 nếu giá trị là số lẻ.

Kiểm tra xem số chẵn hoặc lẻ bằng toán tử XOR

Enter a number: 18
18 is Even
1
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: 18
18 is Even
9

Enter a number: 43
43 is Odd
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
4
Enter a number: 18
18 is Even
9
Enter a number: 43
43 is Odd
4

Enter a number: 43
43 is Odd
7
Enter a number: 43
43 is Odd
8
Enter a number: 43
43 is Odd
9
Enter a number: 18
18 is Even
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. 
9
from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem
0

Đầu ra

Enter a number: 43
43 is Odd
0

Ví dụ 6: Sử dụng toán tử BitWise XORUsing Bitwise AND operator
The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even.
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

Enter a number: 18
18 is Even
5
Enter a number: 18
18 is Even
6
Enter a number: 18
18 is Even
7
Enter a number: 18
18 is Even
8
Enter a number: 18
18 is Even
9
# 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))
0____29
# 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))
2
Enter a number: 18
18 is Even
9
# 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))
4
Enter a number: 18
18 is Even
9
# 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))
6____29____
# 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))
8
Enter a number: 18
18 is Even
9
Enter a number: 43
43 is Odd
0
Enter a number: 43
43 is Odd
1

Đầu ra

Ví dụ 6: Sử dụng toán tử BitWise XOR

Ý 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 hoạt động XOR bitwise của số lên 1 tăng giá trị của số lên 1 nếu số thậm chí còn giảm giá trị của số xuống 1 nếu giá trị là số lẻ.

Kiểm tra xem số chẵn hoặc lẻ bằng toán tử XOR

Enter a number: 18
18 is Even
1
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: 18
18 is Even
9

Enter a number: 43
43 is Odd
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
4
Enter a number: 18
18 is Even
9
Enter a number: 43
43 is Odd
4

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. 
3
Enter a number: 5                                                                                             
This is an odd number. 
4
Enter a number: 5                                                                                             
This is an odd number. 
9
from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem
0

Đầu ra

Enter a number: 43
43 is Odd
0

Ví dụ 6: Sử dụng toán tử BitWise XOR: 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ố là số lẻ, nếu không thì thậm chí. Như chúng ta đã biết hoạt động XOR bitwise của số lên 1 tăng giá trị của số lên 1 nếu số thậm chí còn giảm giá trị của số xuống 1 nếu giá trị là số lẻ.

Python3

Enter a number: 18
18 is Even
5
Enter a number: 18
18 is Even
6
Enter a number: 18
18 is Even
7
Enter a number: 18
18 is Even
8
Enter a number: 18
18 is Even
9
# 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))
0____29
# 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))
2
Enter a number: 18
18 is Even
9
# 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))
4
Enter a number: 18
18 is Even
9
# 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))
6____29____
# 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))
8
Enter a number: 18
18 is Even
9
Enter a number: 43
43 is Odd
0
Enter a number: 43
43 is Odd
1

Đầu ra

Ví dụ 6: Sử dụng toán tử BitWise XOR

Ý 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 hoạt động XOR bitwise của số lên 1 tăng giá trị của số lên 1 nếu số thậm chí còn giảm giá trị của số xuống 1 nếu giá trị là số lẻ.

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.")	
0
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.")	
1
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.")	
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
0

Enter a number: 18
18 is Even
1
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: 18
18 is Even
9

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.")	
0
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.")	
9
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.")	
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
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. 
3
Enter a number: 5                                                                                             
This is an odd number. 
4
Enter a number: 5                                                                                             
This is an odd number. 
9
from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem
0

Đầu ra

Enter a number: 43
43 is Odd
0

Phương pháp: Sử dụng hàm liệt kê & nbsp;

Python3

Enter a number: 18
18 is Even
03
Enter a number: 18
18 is Even
6
Enter a number: 18
18 is Even
7
Enter a number: 18
18 is Even
06
Enter a number: 18
18 is Even
9
Enter a number: 18
18 is Even
08
Enter a number: 18
18 is Even
9
Enter a number: 18
18 is Even
10
Enter a number: 18
18 is Even
9
Enter a number: 18
18 is Even
1212

Enter a number: 43
43 is Odd
7
Enter a number: 18
18 is Even
20
Enter a number: 43
43 is Odd
9
Enter a number: 18
18 is Even
222
Enter a number: 18
18 is Even
23

Enter a number: 18
18 is Even
1
Enter a number: 18
18 is Even
2
Enter a number: 18
18 is Even
26
Enter a number: 43
43 is Odd
9
Enter a number: 18
18 is Even
55____26
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
444

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.")	
0
Enter a number: 18
18 is Even
34
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.")	
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
0

Enter a number: 18
18 is Even
1
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: 18
18 is Even
9

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.")	
0
Enter a number: 18
18 is Even
42
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.")	
2
Enter a number: 18
18 is Even
6
Enter a number: 43
43 is Odd
0

Enter a number: 5                                                                                             
This is an odd number. 
3
Enter a number: 5                                                                                             
This is an odd number. 
4
Enter a number: 18
18 is Even
48
Enter a number: 18
18 is Even
49
Enter a number: 18
18 is Even
50
Enter a number: 18
18 is Even
51

Đầu ra

Enter a number: 43
43 is Odd
8

Phương pháp: Sử dụng hàm liệt kê & nbsp;