Viết tập lệnh Python để in 10 số tự nhiên lẻ đầu tiên

Viết chương trình Python xác định xem một số đã cho (được chấp nhận từ người dùng) là chẵn hay lẻ và in một thông báo thích hợp cho người dùng

Trình bày bằng hình ảnh của các số chẵn

Viết tập lệnh Python để in 10 số tự nhiên lẻ đầu tiên

Trình bày bằng hình ảnh của các số lẻ

Viết tập lệnh Python để in 10 số tự nhiên lẻ đầu tiên

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. 

Giải trình

Đoạn mã nói trên nhắc người dùng nhập một số, sau đó chuyển đổi đầu vào thành một số nguyên và gán nó cho biến 'num'. Sau đó, nó tính toán phần còn lại của 'num' và 2 và gán nó cho biến 'mod'. Tiếp theo, nó kiểm tra giá trị của 'mod'. Nếu giá trị của 'mod' lớn hơn 0, nó sẽ in ra "Đây là một số lẻ. " mặt khác in ra "Đây là số chẵn. "

Số chẵn từ 1 đến 100

Viết tập lệnh Python để in 10 số tự nhiên lẻ đầu tiên

Các số lẻ từ 1 đến 100

Viết tập lệnh Python để in 10 số tự nhiên lẻ đầu tiên

Sơ đồ

Viết tập lệnh Python để in 10 số tự nhiên lẻ đầu tiê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 để lấy một chuỗi là n bản sao (số nguyên không âm) của một chuỗi đã cho
Kế tiếp. Viết chương trình Python để đếm số 4 trong một danh sách nhất định

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



Theo dõi chúng tôi trên FacebookTwitter để cập nhật thông tin mới nhất.

con trăn. Lời khuyên trong ngày

Giải nén đối số

Hãy xem xét chúng ta có một hàm nhân các số đã cho

def mult(x, y, z):
   return x * y * z
print(mult(3, 5, 7))

đầu ra

105

Hàm này hoạt động tốt nếu chúng ta chỉ cần nhân ba số. Nó phải được cung cấp chính xác ba số. Chúng ta có thể làm cho hàm linh hoạt hơn bằng cách sử dụng giải nén đối số

def mult(*args):
   result = 1
   for i in args:
      result *= i
   return result

Bây giờ, hàm nhiều có thể nhân bất kỳ số lượng giá trị nào

đầu ra

print(mult(3, 5, 7, 9))
945
Đang tải

 

Giải nén đối số được sử dụng rất phổ biến trong Python. Chắc hẳn bạn đã thấy *args và **kwargs nếu bạn đọc tài liệu của một gói hoặc thư viện

Ví dụ #3. Lấy giới hạn phạm vi từ đầu vào của người dùng hoặc với đầu vào tĩnh để giảm thời gian thực thi mã và tăng hiệu suất mã

Python3




[5, 7, 9, 11, 13, 15]
6

[5, 7, 9, 11, 13, 15]
7

[5, 7, 9, 11, 13, 15]
8

 

[5, 7, 9, 11, 13, 15]
9

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
7_______11_______5
5 7 9 11 13 15 
2

[3, 5, 7, 9, 11]
5
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 
5

 

5 7 9 11 13 15 17 19 
2
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
7
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
9

 

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
Enter the start of range: 3
Enter the end of range: 7
3
5
7
0
Enter the start of range: 3
Enter the end of range: 7
3
5
7
1
Enter the start of range: 3
Enter the end of range: 7
3
5
7
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
3
Enter the start of range: 3
Enter the end of range: 7
3
5
7
4
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
5 7 9 11 13 15 17 19 
7
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
7

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
0_______10_______1
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
2
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
4
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
5

5 7 9 11 13 15 17 19 
21
5 7 9 11 13 15 17 19 
9

 

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
Enter the start of range: 3
Enter the end of range: 7
3
5
7
0
Enter the start of range: 3
Enter the end of range: 7
3
5
7
1
Enter the start of range: 3
Enter the end of range: 7
3
5
7
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
3
5 7 9 11 13 15 17 19 
28
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
5 7 9 11 13 15 17 19 
31
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
5 7 9 11 13 15 17 19 
7
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
7

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
0_______10_______1
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
2
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
4
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
5

Đầu ra

5 7 9 11 13 15 17 19 

Thời gian phức tạp. O(N), Ở đây N là sự khác biệt của kết thúc và bắt đầu
Không gian phụ trợ. O(1), Vì không gian thừa không đổi được sử dụng

Ví dụ #4. Lấy giới hạn phạm vi từ đầu vào của người dùng

Python3




Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
6

 

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
7_______11_______5
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
9
[3, 5, 7, 9, 11]
0
[3, 5, 7, 9, 11]
1
[3, 5, 7, 9, 11]
0
5 7 9 11 13 15 17 19 
50
[3, 5, 7, 9, 11]
4

[3, 5, 7, 9, 11]
5
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
9
[3, 5, 7, 9, 11]
0
[3, 5, 7, 9, 11]
1
[3, 5, 7, 9, 11]
0
5 7 9 11 13 15 17 19 
58
[3, 5, 7, 9, 11]
4

 

5 7 9 11 13 15 17 19 
60

5 7 9 11 13 15 17 19 
61
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
3
Enter the start of range: 3
Enter the end of range: 7
3
5
7
4
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
5 7 9 11 13 15 17 19 
67
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
70
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
72

 

Enter the start of range: 3
Enter the end of range: 7
3
5
7
0
Enter the start of range: 3
Enter the end of range: 7
3
5
7
1
Enter the start of range: 3
Enter the end of range: 7
3
5
7
2
5 7 9 11 13 15 17 19 
76

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
1
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
2
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
4
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
5

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 

Thời gian phức tạp. TRÊN)
Không gian phụ trợ. O(n), Vì không gian thừa không đổi được sử dụng

Phương pháp. Sử dụng hàm lambda

Python3




5 7 9 11 13 15 17 19 
83

5 7 9 11 13 15 17 19 
84

5 7 9 11 13 15 17 19 
85
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
87
5 7 9 11 13 15 17 19 
88
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
90

5 7 9 11 13 15 17 19 
91
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
93

Enter the start of range: 3
Enter the end of range: 7
3
5
7
0
5 7 9 11 13 15 17 19 
95
Enter the start of range: 3
Enter the end of range: 7
3
5
7
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
3
5 7 9 11 13 15 17 19 
98
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
Enter the start of range: 3
Enter the end of range: 7
3
5
7
7

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
Enter the start of range: 3
Enter the end of range: 7
3
5
7
03

Enter the start of range: 3
Enter the end of range: 7
3
5
7
04

Enter the start of range: 3
Enter the end of range: 7
3
5
7
05
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
07
[3, 5, 7, 9, 11]
0____24_______09
[3, 5, 7, 9, 11]
0
Enter the start of range: 3
Enter the end of range: 7
3
5
7
11
Enter the start of range: 3
Enter the end of range: 7
3
5
7
12
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
8
Enter the start of range: 3
Enter the end of range: 7
3
5
7
18

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
1
Enter the start of range: 3
Enter the end of range: 7
3
5
7
20

Đầu ra

[3, 5, 7, 9, 11]

Thời gian phức tạp. TRÊN)
Không gian phụ trợ. O(1), Vì không gian thừa không đổi được sử dụng

Phương pháp. Sử dụng đệ quy

Python3




Enter the start of range: 3
Enter the end of range: 7
3
5
7
21
Enter the start of range: 3
Enter the end of range: 7
3
5
7
22

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
5 7 9 11 13 15 17 19 
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
25

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
0____24_______27

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
5 7 9 11 13 15 17 19 
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
30
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
5 7 9 11 13 15 17 19 
9

Enter the start of range: 3
Enter the end of range: 7
3
5
7
33
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
1
Enter the start of range: 3
Enter the end of range: 7
3
5
7
35
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
4
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
5

Enter the start of range: 3
Enter the end of range: 7
3
5
7
33
Enter the start of range: 3
Enter the end of range: 7
3
5
7
27
Enter the start of range: 3
Enter the end of range: 7
3
5
7
41
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
44

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
5 7 9 11 13 15 17 19 
21
5 7 9 11 13 15 17 19 
9

Enter the start of range: 3
Enter the end of range: 7
3
5
7
33
Enter the start of range: 3
Enter the end of range: 7
3
5
7
27
Enter the start of range: 3
Enter the end of range: 7
3
5
7
41_______24_______5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
Enter the start of range: 3
Enter the end of range: 7
3
5
7
44

Enter the start of range: 3
Enter the end of range: 7
3
5
7
54_______11_______5
5 7 9 11 13 15 
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
57
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
59

Enter the start of range: 3
Enter the end of range: 7
3
5
7
60

Đầu ra

5 7 9 11 13 15 

Thời gian phức tạp. TRÊN)
Không gian phụ trợ. O(1), Vì hàm là đệ quy đuôi nên không sử dụng không gian ngăn xếp

Phương pháp. Sử dụng hiểu danh sách

Python3




Enter the start of range: 3
Enter the end of range: 7
3
5
7
61
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
63
Enter the start of range: 3
Enter the end of range: 7
3
5
7
0
5 7 9 11 13 15 17 19 
95
Enter the start of range: 3
Enter the end of range: 7
3
5
7
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
3
[3, 5, 7, 9, 11]
0
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
7
Enter the start of range: 3
Enter the end of range: 7
3
5
7
59
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
5
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
95
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
72

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
1
[3, 5, 7, 9, 11]
0
Enter the start of range: 3
Enter the end of range: 7
3
5
7
85
Enter the start of range: 3
Enter the end of range: 7
3
5
7
86

Đầu ra

5 7 9 11 13 15

Thời gian phức tạp. TRÊN)
Không gian phụ trợ. O(1), Vì không gian thừa không đổi được sử dụng

Phương pháp. Sử dụng hàm liệt kê

Python3




5 7 9 11 13 15 17 19 
85
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
88
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
59
Enter the start of range: 3
Enter the end of range: 7
3
5
7
93
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
93

Enter the start of range: 3
Enter the end of range: 7
3
5
7
0
5 7 9 11 13 15 17 19 
95
Enter the start of range: 3
Enter the end of range: 7
3
5
7
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
3
5 7 9 11 13 15 17 19 
98
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
Enter the start of range: 3
Enter the end of range: 7
3
5
7
7

5 7 9 11 13 15 17 19 
04
5 7 9 11 13 15 17 19 
05

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
1
5 7 9 11 13 15 17 19 
07
Enter the start of range: 3
Enter the end of range: 7
3
5
7
0
5 7 9 11 13 15 17 19 
09
Enter the start of range: 3
Enter the end of range: 7
3
5
7
2
5 7 9 11 13 15 17 19 
11
5 7 9 11 13 15 17 19 
12
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
85
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
20

Đầu ra

[5, 7, 9, 11, 13, 15]

Thời gian phức tạp. TRÊN)
Không gian phụ trợ. O(1), Vì không gian thừa không đổi được sử dụng

Phương pháp. Sử dụng thẻ

Python3




5 7 9 11 13 15 17 19 
85
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
88
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
59

Enter the start of range: 3
Enter the end of range: 7
3
5
7
0
5 7 9 11 13 15 17 19 
95
Enter the start of range: 3
Enter the end of range: 7
3
5
7
2
Enter the start of range: 3
Enter the end of range: 7
3
5
7
3
5 7 9 11 13 15 17 19 
98
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
Enter the start of range: 3
Enter the end of range: 7
3
5
7
7

5 7 9 11 13 15 17 19 
04
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
95
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
9

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
5 7 9 11 13 15 17 19 
45

5 7 9 11 13 15 17 19 
04
5 7 9 11 13 15 17 19 
21
5 7 9 11 13 15 17 19 
9

Enter the start of range: 3
Enter the end of range: 7
3
5
7
8
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
1
5 7 9 11 13 15 17 19 
51
5 7 9 11 13 15 17 19 
5
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
4
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
5

Đầu ra

5 7 9 11 13 15 

Thời gian phức tạp. TRÊN)
Không gian phụ trợ. O(1), Vì không gian thừa không đổi được sử dụng

Phương pháp. Sử dụng phương pháp lọc

Python3




Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
6

5 7 9 11 13 15 17 19 
56

[5, 7, 9, 11, 13, 15]
9

5 7 9 11 13 15 17 19 
85
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
61

5 7 9 11 13 15 17 19 
62_______11_______5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
59
5 7 9 11 13 15 17 19 
61

 

5 7 9 11 13 15 17 19 
60

5 7 9 11 13 15 17 19 
67_______11_______5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
09
[3, 5, 7, 9, 11]
0
Enter the start of range: 3
Enter the end of range: 7
3
5
7
11
5 7 9 11 13 15 17 19 
72
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
7
Enter the start of range: 3
Enter the end of range: 7
3
5
7
3
5 7 9 11 13 15 17 19 
77
Enter the start of range: 3
Enter the end of range: 7
3
5
7
5
Enter the start of range: 3
Enter the end of range: 7
3
5
7
6
[3, 5, 7, 9, 11]
4

10 số tự nhiên lẻ đầu tiên trong Python là gì?

Vì vậy, nếu đầu vào là n = 10, thì đầu ra sẽ là 100, vì 10 số nguyên lẻ đầu tiên là [ 1, 3, 5, 7, 9, . ] and its sum is 100.

Trong 10 số tự nhiên lẻ đầu tiên là số nào?

Danh sách 10 số lẻ đầu tiên. 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 .