Hướng dẫn how do you find the even and odd numbers in python? - làm thế nào để bạn tìm thấy các số chẵn và lẻ trong 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 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ẻ. Show
Mã nguồn
Đầ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
>>> 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: Đế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ẻ. Chương trình Python để kiểm tra xem một số là lẻ hay thậm chí
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ápViế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:
Đầ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. Python: Lời khuyên trong ngàyXó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; Python3Các 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 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 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 Python3Các 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 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 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 Python3Cá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 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 Python3Cá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 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 Python3Input: 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 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 6‘ Ví dụ 2: Sử dụng trong khi Loop & NBSP; >>> name = "//George//" >>> name.strip("/") 'George' >>> name.rstrip("/") '//George' >>> name.lstrip("/") 'George//'6 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 36
Even numbers in the list: 3 Odd numbers in the list: 4
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 Python3Các 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 65 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 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 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; Python3Các 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 65 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 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 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; Python3Các 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 65 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 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 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 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 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 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
Ý 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//' 5Ví 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 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 Làm thế nào để bạn trích xuất chẵn và số lẻ từ một danh sách trong Python?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ẻ.
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?Đế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ẻ.Iterate each element in the list using for loop and check if num % 2 == 0, the condition to check even numbers. If the condition satisfies, then increase the even count else increase odd count. |