Một số là số chẵn nếu nó chia hết cho 2. Khi một số được chia cho 2, chúng ta sử dụng toán tử dư
Enter a number: 18 18 is Even41 để tính số dư. Nếu phần còn lại không bằng 0, 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 nhập dữ liệu và kiểm tra xem số đó là số lẻ hay số chẵn. Xin lưu ý rằng
Enter a number: 18 18 is Even42 là trường thay thế cho
Enter a number: 18 18 is Even43
Mọi số nguyên dương hoặc âm không chia hết cho 2 đều là số lẻ. Các số có chữ số tận cùng là 1, 3, 5, 7, 9 là các số lẻ
Ghi chú. Số tự nhiên là số nguyên dương. Nó nằm trong khoảng từ 1 đến vô cùng.
ví dụ
Đầu vào. N. 5
đầu ra. 5 số tự nhiên lẻ đầu tiên là. 1 3 5 7 9
Đầu vào. N. 6
đầu ra. 6 số tự nhiên lẻ đầu tiên là. 1 3 5 7 9 11
Hãy xem các phiên bản mã khác nhau cho chương trình này
Phiên bản mã 1. Sử dụng bộ đếm
Trong mã này, chúng tôi lấy một biến đếm 'lẻ' được khởi tạo bằng 1 và trên mỗi lần lặp, giá trị bộ đếm này được tăng thêm 2
# main code if __name__ == "__main__" : # input value of n n = 11 # take a odd variable # and assign 1 to it odd = 1 print["odd numbers are: "] # loop run from 0 # to n - 1 for i in range[n] : # print value of odd variable # and end parameter is ',' # so print all outputs # in a single line with ',' print[odd, end = ","] # increment by 2 # in every iteration odd += 2
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,
Phiên bản mã 2. Sử dụng câu lệnh if-else cùng với vòng lặp for
Trong đoạn mã này, chúng tôi chạy một vòng lặp từ 1 đến 2 * n – 1 và trong mỗi lần lặp, chúng tôi kiểm tra giá trị của i có chia hết cho 2 hay không nếu không chia hết thì là số lẻ nếu không là số chẵn
# main code if __name__ == "__main__" : # input value of n n = 7 print["odd numbers are: "] # loop run from 1 # to 2 * n - 1 for i in range[1, 2 * n] : # if condition is True # that means i % 2 gives # non-zero value then if i % 2 : # print value of i variable # and end parameter is ',' # so print all outputs # in a single line with ',' print[i, end = ","]
Output: odd numbers are: 1,3,5,7,9,11,13,
Ghi chú. Mã phiên bản 1 và 2 cũng có thể được viết bằng vòng lặp While.
Phiên bản mã 3. Sử dụng khái niệm hàm range[]
Như chúng ta đã biết, chúng ta có thể truyền 3 tham số trong hàm range[], tham số thứ ba là bước i. e nhảy sau mỗi lần lặp
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 thi chương trình nói trên
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?
Trước. Viết chương trình Python chấp nhận một từ từ người dùng và đảo ngược nó.
Tiếp theo. Viết chương trình Python in từng mục và loại tương ứng của nó từ danh sách sau.
Mức độ khó của bài tập này là gì?
Dễ dàng trung bình khóKiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource
con trăn. Lời khuyên trong ngày
Lambda
Lambda giống như một chức năng nhỏ một dòng do người dùng định nghĩa. Khi bạn có một chức năng không quá phức tạp và có thể được thực hiện dưới dạng lambda, việc nhập nó trong 1 dòng đơn giản sẽ thuận tiện hơn nhiều. Làm cho bạn nhận ra mức độ thực tế của Python
Cho trước điểm đầu và điểm cuối, hãy viết chương trình Python để in tất cả các số chẵn trong dãy đã cho đó.
Ví dụ
Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 Input: start = 8, end = 11 Output: 8, 10
Ví dụ 1. In tất cả các số chẵn từ danh sách đã cho bằng cách sử dụng vòng lặp Xác định giới hạn bắt đầu và kết thúc của phạm vi. Lặp lại từ đầu cho đến phạm vi trong danh sách bằng cách sử dụng vòng lặp for và kiểm tra xem num % 2 == 0. Nếu điều kiện thỏa mãn thì chỉ in ra số.
Python3
4 6 8 10 12 140
4 6 8 10 12 141
4 6 8 10 12 142
4 6 8 10 12 143
4 6 8 10 12 144
4 6 8 10 12 145
4 6 8 10 12 146
4 6 8 10 12 147
4 6 8 10 12 146
4 6 8 10 12 149
Enter a number: 43 43 is Odd80
Enter a number: 43 43 is Odd81
Enter a number: 43 43 is Odd82
________ 183 ________ 184 ________ 185
đầu ra
________số 8Ví dụ #2. Lấy giới hạn phạm vi từ đầu vào của người dùng
Python3
Enter a number: 43 43 is Odd86
Enter a number: 43 43 is Odd87
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd89
4 6 8 10 12 144
Enter a number: 43 43 is Odd81
4 6 8 10 12 144
Enter a number: 43 43 is Odd83
Enter a number: 43 43 is Odd84
Enter a number: 43 43 is Odd85
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd89
4 6 8 10 12 144
Enter a number: 43 43 is Odd81
4 6 8 10 12 144
4 6 8 10 12 141
Enter a number: 43 43 is Odd84
4 6 8 10 12 143
4 6 8 10 12 140
4 6 8 10 12 141
4 6 8 10 12 142
4 6 8 10 12 143
4 6 8 10 12 148
4 6 8 10 12 149
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,90
Enter a number: 43 43 is Odd80
Enter a number: 43 43 is Odd83
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,93
Enter a number: 43 43 is Odd83
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,95
4 6 8 10 12 141
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,97
4 6 8 10 12 149
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd88
Output: odd numbers are: 1,3,5,7,9,11,13,01
Output: odd numbers are: 1,3,5,7,9,11,13,02
Output: odd numbers are: 1,3,5,7,9,11,13,03
Enter a number: 43 43 is Odd84
Output: odd numbers are: 1,3,5,7,9,11,13,05
Enter a number: 43 43 is Odd88
Output: odd numbers are: 1,3,5,7,9,11,13,07
Output: odd numbers are: 1,3,5,7,9,11,13,08
đầu ra
Enter a number: 43 43 is Odd8
Ví dụ #3 Lấy đầu vào của người dùng giới hạn phạm vi và sử dụng bỏ qua số thứ tự trong hàm phạm vi để tạo ra số chẵn.
Python3
Enter a number: 43 43 is Odd86
Enter a number: 43 43 is Odd87
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd89
4 6 8 10 12 144
Enter a number: 43 43 is Odd81
4 6 8 10 12 144
Enter a number: 43 43 is Odd83
Enter a number: 43 43 is Odd84
Enter a number: 43 43 is Odd85
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd89
4 6 8 10 12 144
Enter a number: 43 43 is Odd81
4 6 8 10 12 144
4 6 8 10 12 141
Enter a number: 43 43 is Odd84
4 6 8 10 12 146
Enter a number: 43 43 is Odd87
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd87
4 6 8 10 12 149
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,90
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,95
4 6 8 10 12 1433
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,90
4 6 8 10 12 1435
Enter a number: 43 43 is Odd87
4 6 8 10 12 1437
4 6 8 10 12 1438
4 6 8 10 12 1439
4 6 8 10 12 1400
Enter a number: 43 43 is Odd84
4 6 8 10 12 1402
4 6 8 10 12 140
4 6 8 10 12 1404
4 6 8 10 12 142
4 6 8 10 12 143______88
4 6 8 10 12 149
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,90
4 6 8 10 12 146
4 6 8 10 12 149
4 6 8 10 12 1412
đầu ra
Enter a number: 43 43 is Odd8
Phương pháp. Sử dụng đệ quy
Python3
4 6 8 10 12 1413
4 6 8 10 12 1414
Enter a number: 43 43 is Odd83
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,95
4 6 8 10 12 1417
Output: odd numbers are: 1,3,5,7,9,11,13,03
4 6 8 10 12 1419
Enter a number: 43 43 is Odd83
Enter a number: 43 43 is Odd84____822
Enter a number: 43 43 is Odd88
Output: odd numbers are: 1,3,5,7,9,11,13,07
Output: odd numbers are: 1,3,5,7,9,11,13,08
Enter a number: 43 43 is Odd83
4 6 8 10 12 1419
4 6 8 10 12 1428
4 6 8 10 12 149
4 6 8 10 12 149
4 6 8 10 12 1431
4 6 8 10 12 1432
Enter a number: 43 43 is Odd88
4 6 8 10 12 145______835
Enter a number: 43 43 is Odd88
4 6 8 10 12 147
4 6 8 10 12 1438
Đầu ra
4 6 8 10 12 14
Phương pháp. Sử dụng hàm lambda
Python3
4 6 8 10 12 1439
4 6 8 10 12 1440
4 6 8 10 12 1441
Enter a number: 43 43 is Odd88
4 6 8 10 12 145
4 6 8 10 12 1444
Enter a number: 43 43 is Odd88
4 6 8 10 12 147
4 6 8 10 12 1447
Enter a number: 43 43 is Odd88
4 6 8 10 12 1449
4 6 8 10 12 140
4 6 8 10 12 1451
4 6 8 10 12 142
4 6 8 10 12 143____854
4 6 8 10 12 149
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,90
Enter a number: 43 43 is Odd80
Enter a number: 43 43 is Odd83
4 6 8 10 12 1459
4 6 8 10 12 1460
4 6 8 10 12 1461
Enter a number: 43 43 is Odd88
4 6 8 10 12 1463
4 6 8 10 12 144
4 6 8 10 12 1465
4 6 8 10 12 144
4 6 8 10 12 1467
4 6 8 10 12 1468
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,97
4 6 8 10 12 149
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd88
Output: odd numbers are: 1,3,5,7,9,11,13,01
4 6 8 10 12 1474
Enter a number: 43 43 is Odd84
4 6 8 10 12 1476
Đầu ra
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,9
Phương pháp. Sử dụng hiểu danh sách
Python3
4 6 8 10 12 1404
Enter a number: 43 43 is Odd88
4 6 8 10 12 1479
4 6 8 10 12 140
4 6 8 10 12 1451
4 6 8 10 12 142
4 6 8 10 12 143_______84
4 6 8 10 12 145
4 6 8 10 12 146
4 6 8 10 12 147
4 6 8 10 12 149
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,90
Output: odd numbers are: 1,3,5,7,9,11,13,08
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,95
4 6 8 10 12 1451
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,97
4 6 8 10 12 149
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd88
Output: odd numbers are: 1,3,5,7,9,11,13,01
4 6 8 10 12 1498
Enter a number: 43 43 is Odd84
4 6 8 10 12 144
Enter a number: 43 43 is Odd801
Enter a number: 43 43 is Odd802
Đầu ra
Output: odd numbers are: 1,3,5,7,9,11,13,0
Phương pháp. Sử dụng hàm liệt kê
Python3
4 6 8 10 12 1441
Enter a number: 43 43 is Odd88
4 6 8 10 12 145
4 6 8 10 12 1444
Enter a number: 43 43 is Odd88
4 6 8 10 12 147
Enter a number: 43 43 is Odd809
Enter a number: 43 43 is Odd88
4 6 8 10 12 1449
4 6 8 10 12 140
4 6 8 10 12 1451
4 6 8 10 12 142
4 6 8 10 12 143____854
4 6 8 10 12 149
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,90
Enter a number: 43 43 is Odd80
Enter a number: 43 43 is Odd81
Enter a number: 43 43 is Odd821
Enter a number: 43 43 is Odd84
Enter a number: 43 43 is Odd823
4 6 8 10 12 140
Enter a number: 43 43 is Odd825
4 6 8 10 12 142
Enter a number: 43 43 is Odd827
Enter a number: 43 43 is Odd828
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,95
4 6 8 10 12 1441
Output: odd numbers are: 1,3,5,7,9,11,13,15,17,19,21,97
4 6 8 10 12 149
Enter a number: 43 43 is Odd88
Enter a number: 43 43 is Odd88
Output: odd numbers are: 1,3,5,7,9,11,13,01
Enter a number: 43 43 is Odd836