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: 30_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 51
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 53
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 56_______0_______4_______11_______8_______11_______9
Positive numbers in the list: 4 Negative numbers in the list: 30
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 4 Negative numbers in the list: 37_______0_______8
Positive numbers in the list: 4 Negative numbers in the list: 39
Positive numbers in the list: 1 Negative numbers in the list: 60_______30_______1
Positive numbers in the list: 1 Negative numbers in the list: 60_______30_______3
Positive numbers in the list: 1 Negative numbers in the list: 64
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 1 Negative numbers in the list: 67
Positive numbers in the list: 1 Negative numbers in the list: 68
Positive numbers in the list: 1 Negative numbers in the list: 69
Length of Positive numbers is: 3 Length of Negative numbers is: 20
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 1 Negative numbers in the list: 60_______40_______4
Positive numbers in the list: 1 Negative numbers in the list: 67
Positive numbers in the list: 1 Negative numbers in the list: 68
Length of Positive numbers is: 3 Length of Negative numbers is: 27
Length of Positive numbers is: 3 Length of Negative numbers is: 20
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Đầ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: 30_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 51
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 53_______0_______4_______11_______6_______0_______1_______1__391
Positive numbers in the list: 4 Negative numbers in the list: 30
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 301
Positive numbers in the list: 4 Negative numbers in the list: 302_______0_______03
Positive numbers in the list: 4 Negative numbers in the list: 304
Positive numbers in the list: 4 Negative numbers in the list: 305
Positive numbers in the list: 1 Negative numbers in the list: 60_______30_______1
Positive numbers in the list: 1 Negative numbers in the list: 60_______30_______3
Positive numbers in the list: 4 Negative numbers in the list: 310
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 1 Negative numbers in the list: 67
Positive numbers in the list: 1 Negative numbers in the list: 68
Positive numbers in the list: 1 Negative numbers in the list: 69
Length of Positive numbers is: 3 Length of Negative numbers is: 20
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 1 Negative numbers in the list: 60_______40_______4
Positive numbers in the list: 1 Negative numbers in the list: 67
Positive numbers in the list: 1 Negative numbers in the list: 68
Length of Positive numbers is: 3 Length of Negative numbers is: 27
Length of Positive numbers is: 3 Length of Negative numbers is: 20
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 1 Negative numbers in the list: 60______0_______28
Positive numbers in the list: 1 Negative numbers in the list: 60_______0_______7
Length of Positive numbers is: 3 Length of Negative numbers is: 20
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Đầ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: 342
Positive numbers in the list: 4 Negative numbers in the list: 343
# list of numbers
Positive numbers in the list: 4 Negative numbers in the list: 30_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 51
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 53
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 56
Positive numbers in the list: 4 Negative numbers in the list: 34_______0_______5_______396_______1_______11_______9
Length of Positive numbers is: 3 Length of Negative numbers is: 27_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 304_______0_______2
Positive numbers in the list: 4 Negative numbers in the list: 369
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 371
Positive numbers in the list: 4 Negative numbers in the list: 32_______0_______73
Positive numbers in the list: 4 Negative numbers in the list: 374
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 376
Positive numbers in the list: 4 Negative numbers in the list: 377
Positive numbers in the list: 1 Negative numbers in the list: 69_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 304_______0_______2
Positive numbers in the list: 4 Negative numbers in the list: 369
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 371
Positive numbers in the list: 4 Negative numbers in the list: 32_______0_______73
Positive numbers in the list: 4 Negative numbers in the list: 387
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 376
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Đầ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: 342
Positive numbers in the list: 4 Negative numbers in the list: 343
# list of numbers
Positive numbers in the list: 4 Negative numbers in the list: 30
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 51
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 53_______0_______4_______0_______5_______11_______6_______ 9_______1
Positive numbers in the list: 2 Negative numbers in the list: 525
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 527_______0_______6
Positive numbers in the list: 4 Negative numbers in the list: 37_______0_______8
Positive numbers in the list: 4 Negative numbers in the list: 30
Positive numbers in the list: 1 Negative numbers in the list: 63
Positive numbers in the list: 1 Negative numbers in the list: 64
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 2 Negative numbers in the list: 59
Positive numbers in the list: 1 Negative numbers in the list: 69_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 304
Positive numbers in the list: 2 Negative numbers in the list: 540
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37_______0_______4
Positive numbers in the list: 4 Negative numbers in the list: 304
Positive numbers in the list: 2 Negative numbers in the list: 550
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 552
Đầ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: 553_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 2 Negative numbers in the list: 556
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 559
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 561
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 563
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 566
Positive numbers in the list: 2 Negative numbers in the list: 567_______0_______1_______0_______2
Positive numbers in the list: 2 Negative numbers in the list: 570_______0_______1
Positive numbers in the list: 2 Negative numbers in the list: 572
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 2 Negative numbers in the list: 574
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 2 Negative numbers in the list: 576
Positive numbers in the list: 2 Negative numbers in the list: 577
Positive numbers in the list: 1 Negative numbers in the list: 63
Positive numbers in the list: 2 Negative numbers in the list: 579
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 2 Negative numbers in the list: 59
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 2 Negative numbers in the list: 585
Positive numbers in the list: 4 Negative numbers in the list: 34_______0_______04
Positive numbers in the list: 2 Negative numbers in the list: 588
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 2 Negative numbers in the list: 591
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 304
Positive numbers in the list: 2 Negative numbers in the list: 577
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 304
Positive numbers in the list: 2 Negative numbers in the list: 588
Đầ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: 30
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32_______0_______3
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 51
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 53
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 56_______0_______4____
Positive numbers in the list: 2 Negative numbers in the list: 58_______1_______4_______1_______2_______1_______1_______ 2
Positive numbers in the list: 4 Negative numbers in the list: 30
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 327_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 369
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 331
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 333
Positive numbers in the list: 4 Negative numbers in the list: 334
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 4 Negative numbers in the list: 37_______0_______8
Positive numbers in the list: 4 Negative numbers in the list: 339
Positive numbers in the list: 1 Negative numbers in the list: 60_______30_______1
Positive numbers in the list: 1 Negative numbers in the list: 60_______30_______3
Positive numbers in the list: 4 Negative numbers in the list: 344
Positive numbers in the list: 4 Negative numbers in the list: 345_______0_______46
Positive numbers in the list: 1 Negative numbers in the list: 68
Length of Positive numbers is: 3 Length of Negative numbers is: 27
Length of Positive numbers is: 3 Length of Negative numbers is: 20
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 1 Negative numbers in the list: 60_______0_______53
Positive numbers in the list: 4 Negative numbers in the list: 354
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 356
Positive numbers in the list: 4 Negative numbers in the list: 346
Positive numbers in the list: 1 Negative numbers in the list: 68
Positive numbers in the list: 1 Negative numbers in the list: 63
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 361
Positive numbers in the list: 4 Negative numbers in the list: 344
Positive numbers in the list: 4 Negative numbers in the list: 345
Positive numbers in the list: 4 Negative numbers in the list: 364
Positive numbers in the list: 4 Negative numbers in the list: 365
Positive numbers in the list: 1 Negative numbers in the list: 69
Length of Positive numbers is: 3 Length of Negative numbers is: 20
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 1 Negative numbers in the list: 68
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 33
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 4 Negative numbers in the list: 37
Positive numbers in the list: 4 Negative numbers in the list: 38
Đầ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: 553_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 2 Negative numbers in the list: 556_______0_______4
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 559
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 561
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 2 Negative numbers in the list: 563
Positive numbers in the list: 4 Negative numbers in the list: 34
Positive numbers in the list: 4 Negative numbers in the list: 35
Positive numbers in the list: 2 Negative numbers in the list: 566
Positive numbers in the list: 4 Negative numbers in the list: 393
Positive numbers in the list: 2 Negative numbers in the list: 570_______0_______1
Positive numbers in the list: 4 Negative numbers in the list: 396
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 2 Negative numbers in the list: 58
Positive numbers in the list: 4 Negative numbers in the list: 36
Positive numbers in the list: 1 Negative numbers in the list: 600
Positive numbers in the list: 4 Negative numbers in the list: 38
Positive numbers in the list: 2 Negative numbers in the list: 553
Positive numbers in the list: 1 Negative numbers in the list: 63
Positive numbers in the list: 1 Negative numbers in the list: 604
Positive numbers in the list: 4 Negative numbers in the list: 31
Positive numbers in the list: 4 Negative numbers in the list: 32
Positive numbers in the list: 1 Negative numbers in the list: 607