Kiểm tra số nguyên dương Python

Ví dụ 1. Đếm số dương và số âm từ danh sách đã cho bằng vòng lặp for Lặp lại từng phần tử trong danh sách bằng vòng lặp for và kiểm tra xem num >= 0 hay không, điều kiện để kiểm tra số dương. Nếu điều kiện thỏa mãn thì tăng pos_count ngược lại tăng neg_count.  

Python3




# Python program to count positive and negative numbers in a List

 

# list of numbers

Positive numbers in the list:  4
Negative numbers in the list:  3
0_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
1
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
6_______0_______4_______11_______8_______11_______9

 

Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
2

 

Positive numbers in the list:  4
Negative numbers in the list:  3
5

Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  4
Negative numbers in the list:  3
7_______0_______8
Positive numbers in the list:  4
Negative numbers in the list:  3
9

 

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______30_______1

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______30_______3
Positive numbers in the list:  1
Negative numbers in the list:  6
4
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  1
Negative numbers in the list:  6
7

Positive numbers in the list:  1
Negative numbers in the list:  6
8
Positive numbers in the list:  1
Negative numbers in the list:  6
9
Length of Positive numbers is: 3
Length of Negative numbers is: 2
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
8

 

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______40_______4
Positive numbers in the list:  1
Negative numbers in the list:  6
7

Positive numbers in the list:  1
Negative numbers in the list:  6
8
Length of Positive numbers is: 3
Length of Negative numbers is: 2
7
Length of Positive numbers is: 3
Length of Negative numbers is: 2
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
8

 

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
8

Đầu ra

Positive numbers in the list:  4
Negative numbers in the list:  3

Thời gian phức tạp. O[N], Ở đây N là số mục trong danh sách
Không gian phụ trợ. O[1], Vì không gian thừa không đổi được sử dụng

Ví dụ #2. Sử dụng vòng lặp while

Python3




# Python program to count positive and negative numbers in a List

 

# list of numbers

Positive numbers in the list:  4
Negative numbers in the list:  3
0_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
1
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
3_______0_______4_______11_______6_______0_______1_______1__391

 

Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
2

Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2

 

Positive numbers in the list:  4
Negative numbers in the list:  3
01

Positive numbers in the list:  4
Negative numbers in the list:  3
02_______0_______03
Positive numbers in the list:  4
Negative numbers in the list:  3
04
Positive numbers in the list:  4
Negative numbers in the list:  3
05

 

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______30_______1

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______30_______3
Positive numbers in the list:  4
Negative numbers in the list:  3
10
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  1
Negative numbers in the list:  6
7

Positive numbers in the list:  1
Negative numbers in the list:  6
8
Positive numbers in the list:  1
Negative numbers in the list:  6
9
Length of Positive numbers is: 3
Length of Negative numbers is: 2
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
8

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______40_______4
Positive numbers in the list:  1
Negative numbers in the list:  6
7

Positive numbers in the list:  1
Negative numbers in the list:  6
8
Length of Positive numbers is: 3
Length of Negative numbers is: 2
7
Length of Positive numbers is: 3
Length of Negative numbers is: 2
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
8

 

Positive numbers in the list:  1
Negative numbers in the list:  6
0______0_______28

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______0_______7
Length of Positive numbers is: 3
Length of Negative numbers is: 2
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
8

 

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
8

Đầu ra

Positive numbers in the list:  2
Negative numbers in the list:  5

Thời gian phức tạp. O[N], Ở đây N là số mục trong danh sách
Không gian phụ trợ. O[1], Vì không gian thừa không đổi được sử dụng

Ví dụ #3. Sử dụng biểu thức Lambda Python

Python3




Positive numbers in the list:  4
Negative numbers in the list:  3
42

Positive numbers in the list:  4
Negative numbers in the list:  3
43

 

# list of numbers

Positive numbers in the list:  4
Negative numbers in the list:  3
0_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
1
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
6
Positive numbers in the list:  4
Negative numbers in the list:  3
4_______0_______5_______396_______1_______11_______9

 

Length of Positive numbers is: 3
Length of Negative numbers is: 2
7_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
04_______0_______2
Positive numbers in the list:  4
Negative numbers in the list:  3
69
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
71
Positive numbers in the list:  4
Negative numbers in the list:  3
2_______0_______73
Positive numbers in the list:  4
Negative numbers in the list:  3
74
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
76

 

Positive numbers in the list:  4
Negative numbers in the list:  3
77

Positive numbers in the list:  1
Negative numbers in the list:  6
9_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
04_______0_______2
Positive numbers in the list:  4
Negative numbers in the list:  3
69
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
71
Positive numbers in the list:  4
Negative numbers in the list:  3
2_______0_______73
Positive numbers in the list:  4
Negative numbers in the list:  3
87
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
76

 

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
8

Đầu ra

Positive numbers in the list:  4
Negative numbers in the list:  3

Thời gian phức tạp. O[N], Ở đây N là số mục trong danh sách
Không gian phụ trợ. O[1], Vì không gian thừa không đổi được sử dụng

Ví dụ #4. Sử dụng danh sách hiểu

Python3




Positive numbers in the list:  4
Negative numbers in the list:  3
42

Positive numbers in the list:  4
Negative numbers in the list:  3
43

 

# list of numbers

Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
1
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
3_______0_______4_______0_______5_______11_______6_______ 9_______1

 

Positive numbers in the list:  2
Negative numbers in the list:  5
25
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
27_______0_______6
Positive numbers in the list:  4
Negative numbers in the list:  3
7_______0_______8
Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  1
Negative numbers in the list:  6
3
Positive numbers in the list:  1
Negative numbers in the list:  6
4
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
8
Positive numbers in the list:  2
Negative numbers in the list:  5
9

Positive numbers in the list:  1
Negative numbers in the list:  6
9_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
04
Positive numbers in the list:  2
Negative numbers in the list:  5
40

 

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7_______0_______4
Positive numbers in the list:  4
Negative numbers in the list:  3
04
Positive numbers in the list:  2
Negative numbers in the list:  5
50
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
52

Đầu ra

Positive numbers in the list:  1
Negative numbers in the list:  6

Thời gian phức tạp. O[N], Ở đây N là số mục trong danh sách
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




Positive numbers in the list:  2
Negative numbers in the list:  5
53_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  2
Negative numbers in the list:  5
56
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
59
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
61
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
63
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
66
Positive numbers in the list:  2
Negative numbers in the list:  5
67_______0_______1_______0_______2

Positive numbers in the list:  2
Negative numbers in the list:  5
70_______0_______1
Positive numbers in the list:  2
Negative numbers in the list:  5
72
Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  2
Negative numbers in the list:  5
74
Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  2
Negative numbers in the list:  5
76
Positive numbers in the list:  2
Negative numbers in the list:  5
77
Positive numbers in the list:  1
Negative numbers in the list:  6
3
Positive numbers in the list:  2
Negative numbers in the list:  5
79
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  2
Negative numbers in the list:  5
9

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  2
Negative numbers in the list:  5
85
Positive numbers in the list:  4
Negative numbers in the list:  3
4_______0_______04
Positive numbers in the list:  2
Negative numbers in the list:  5
88

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  2
Negative numbers in the list:  5
91
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
04
Positive numbers in the list:  2
Negative numbers in the list:  5
77
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
04
Positive numbers in the list:  2
Negative numbers in the list:  5
88

Đầu ra

Length of Positive numbers is: 3
Length of Negative numbers is: 2

Thời gian phức tạp. O[N], Ở đây N là số mục trong danh sách
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 thức startedwith[]

Python3




# Python program to count positive and negative numbers in a List

 

# list of numbers

Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2_______0_______3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
1
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
6_______0_______4____
Positive numbers in the list:  2
Negative numbers in the list:  5
8_______1_______4_______1_______2_______1_______1_______ 2

 

Positive numbers in the list:  4
Negative numbers in the list:  3
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
2

Positive numbers in the list:  4
Negative numbers in the list:  3
27_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
69
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
31
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
33
Positive numbers in the list:  4
Negative numbers in the list:  3
34

 

Positive numbers in the list:  4
Negative numbers in the list:  3
5

Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  4
Negative numbers in the list:  3
7_______0_______8
Positive numbers in the list:  4
Negative numbers in the list:  3
39

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______30_______1

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______30_______3
Positive numbers in the list:  4
Negative numbers in the list:  3
44
Positive numbers in the list:  4
Negative numbers in the list:  3
45_______0_______46

Positive numbers in the list:  1
Negative numbers in the list:  6
8
Length of Positive numbers is: 3
Length of Negative numbers is: 2
7
Length of Positive numbers is: 3
Length of Negative numbers is: 2
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
8

Positive numbers in the list:  1
Negative numbers in the list:  6
0_______0_______53
Positive numbers in the list:  4
Negative numbers in the list:  3
54
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
56
Positive numbers in the list:  4
Negative numbers in the list:  3
46

Positive numbers in the list:  1
Negative numbers in the list:  6
8
Positive numbers in the list:  1
Negative numbers in the list:  6
3
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
61
Positive numbers in the list:  4
Negative numbers in the list:  3
44
Positive numbers in the list:  4
Negative numbers in the list:  3
45
Positive numbers in the list:  4
Negative numbers in the list:  3
64

Positive numbers in the list:  4
Negative numbers in the list:  3
65
Positive numbers in the list:  1
Negative numbers in the list:  6
9
Length of Positive numbers is: 3
Length of Negative numbers is: 2
0
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  2
Negative numbers in the list:  5
8

Positive numbers in the list:  1
Negative numbers in the list:  6
8

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
3
Positive numbers in the list:  4
Negative numbers in the list:  3
4

Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  4
Negative numbers in the list:  3
7
Positive numbers in the list:  4
Negative numbers in the list:  3
8

Đầu ra

Positive numbers in the list:  4
Negative numbers in the list:  3

Thời gian phức tạp. O[N], Ở đây N là số mục trong danh sách
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 thức sum[]

Python3




Positive numbers in the list:  2
Negative numbers in the list:  5
53_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  2
Negative numbers in the list:  5
56_______0_______4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
59
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
61
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  2
Negative numbers in the list:  5
63
Positive numbers in the list:  4
Negative numbers in the list:  3
4
Positive numbers in the list:  4
Negative numbers in the list:  3
5
Positive numbers in the list:  2
Negative numbers in the list:  5
66
Positive numbers in the list:  4
Negative numbers in the list:  3
93

Positive numbers in the list:  2
Negative numbers in the list:  5
70_______0_______1
Positive numbers in the list:  4
Negative numbers in the list:  3
96
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  2
Negative numbers in the list:  5
8
Positive numbers in the list:  4
Negative numbers in the list:  3
6
Positive numbers in the list:  1
Negative numbers in the list:  6
00
Positive numbers in the list:  4
Negative numbers in the list:  3
8
Positive numbers in the list:  2
Negative numbers in the list:  5
53
Positive numbers in the list:  1
Negative numbers in the list:  6
3
Positive numbers in the list:  1
Negative numbers in the list:  6
04
Positive numbers in the list:  4
Negative numbers in the list:  3
1
Positive numbers in the list:  4
Negative numbers in the list:  3
2
Positive numbers in the list:  1
Negative numbers in the list:  6
07

Làm cách nào để kiểm tra số nguyên dương đầu vào của người dùng trong Python?

Sử dụng phương thức chuỗi isdigit[] để kiểm tra đầu vào của người dùng là số hay chuỗi . Ghi chú. Hàm isdigit[] sẽ chỉ hoạt động với các số nguyên dương. tôi. e. , nếu bạn vượt qua bất kỳ số float nào, nó sẽ không hoạt động. Vì vậy, tốt hơn là sử dụng phương pháp đầu tiên.

Loại float nào là tích cực trong Python?

Trong Python, số dấu chấm động [float] là các số thực dương và âm với một phần phân số được biểu thị bằng ký hiệu thập phân . hoặc ký hiệu khoa học E hoặc e , e. g. 1234. 56, 3. 142, -1. 55, 0. 23. Các số float có thể được phân tách bằng dấu gạch dưới _ , e. g. 123_42. 222_013 là số float hợp lệ.

Chủ Đề