Làm thế nào để bạn tránh các số âm trong python?

Mtf. Dòng trả về “không có số âm”;

Mick. Xác thực chỉ cần gọi hàm có mục nhập âm và kiểm tra giá trị trả về. Nó không kiểm tra hoặc triệt tiêu bất kỳ chức năng nào khác, kể cả bảng điều khiển. thông báo nhật ký, thực thi như dự định. Không có cuộc gọi xác thực nào trong bàn di chuột nên bạn chỉ thấy thông báo khi bạn sử dụng các mục nhập phủ định

Ở đây, chúng tôi đã sử dụng câu lệnh if...elif...else. Chúng ta có thể làm điều tương tự bằng cách sử dụng các câu lệnh if lồng nhau như sau

Mã nguồn. Sử dụng Nested nếu

num = float[input["Enter a number: "]]
if num >= 0:
   if num == 0:
       print["Zero"]
   else:
       print["Positive number"]
else:
   print["Negative number"]

Đầu ra của cả hai chương trình sẽ giống nhau

Đầu ra 1

Enter a number: 2
Positive number

Đầu ra 2

Enter a number: 0
Zero

Một số dương nếu nó lớn hơn 0. Chúng tôi kiểm tra điều này trong biểu thức của if. Nếu là False, số đó sẽ bằng 0 hoặc âm. Điều này cũng được kiểm tra trong biểu thức tiếp theo

Dot Net Perls là tập hợp các ví dụ về mã đã được thử nghiệm. Các trang được cập nhật liên tục để luôn cập nhật, với ưu tiên hàng đầu là tính chính xác của mã

Sam Allen đam mê ngôn ngữ máy tính. Trước đây, công việc của anh ấy đã được Apple và Microsoft giới thiệu và anh ấy đã học máy tính tại một trường đại học chọn lọc ở Hoa Kỳ

Không tìm thấy cập nhật nào cho trang này

Đôi khi, trong khi làm việc với danh sách Python, chúng ta có thể gặp sự cố trong đó chúng ta cần xóa tất cả các phần tử phủ định khỏi danh sách. Loại vấn đề này có thể có ứng dụng trong nhiều lĩnh vực như lập trình trường học và phát triển web. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện

Đầu vào. test_list = [6, 4, 3] Đầu ra. [6, 4, 3] Đầu vào. test_list = [-6, -4] Đầu ra. []

Phương pháp số 1. Sử dụng hiểu danh sách Có thể sử dụng kết hợp các chức năng trên để giải quyết vấn đề này. Trong phần này, chúng tôi thực hiện nhiệm vụ loại bỏ các phần tử tiêu cực bằng cách lặp lại trong một lớp lót bằng cách sử dụng khả năng hiểu danh sách

Python3




# Python3 code to demonstrate working of

# Remove Negative Elements in List

# Using list comprehension

 

# initializing list

test_list___= [

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1__
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
5_______3_______1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
8
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4_______3_______5_______3_______1_______3_______7_______3_______8

 

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
9

# Python3 code to demonstrate working of0# Python3 code to demonstrate working of1# Python3 code to demonstrate working of2 # Python3 code to demonstrate working of3 # Python3 code to demonstrate working of4# Python3 code to demonstrate working of5 # Python3 code to demonstrate working of6# Python3 code to demonstrate working of7

 

# Remove Negative Elements in List

# Using list comprehension

# Remove Negative Elements in List0= # Remove Negative Elements in List2_______95_______3 # Remove Negative Elements in List4# Remove Negative Elements in List5 test_list# Remove Negative Elements in List7 # Remove Negative Elements in List8# Remove Negative Elements in List9

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
8

 

# Using list comprehension1

# Python3 code to demonstrate working of0# Using list comprehension3# Using list comprehension4 # Using list comprehension5# Python3 code to demonstrate working of5 # Python3 code to demonstrate working of6# Using list comprehension8

đầu ra.

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]

Phương pháp #2. Sử dụng filter[] + lambda Sự kết hợp của các chức năng trên cũng có thể đưa ra giải pháp thay thế cho vấn đề này. Trong phần này, chúng tôi mở rộng logic giữ lại tích cực được hình thành bằng hàm lambda và mở rộng bằng cách sử dụng bộ lọc [].  

Python3




# Python3 code to demonstrate working of

# Remove Negative Elements in List

# initializing list1

 

# initializing list

test_list___= [

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1__
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
5_______3_______1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
8
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4_______3_______5_______3_______1_______3_______7_______3_______8

 

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
9

# Python3 code to demonstrate working of0# Python3 code to demonstrate working of1# Python3 code to demonstrate working of2 # Python3 code to demonstrate working of3 # Python3 code to demonstrate working of4# Python3 code to demonstrate working of5 # Python3 code to demonstrate working of6# Python3 code to demonstrate working of7

 

# Remove Negative Elements in List

# initializing list1

# Remove Negative Elements in List0= # Python3 code to demonstrate working of2[9

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
00[9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
02
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
03# Remove Negative Elements in List9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
05

 

# Using list comprehension1

# Python3 code to demonstrate working of0# Using list comprehension3# Using list comprehension4 # Using list comprehension5# Python3 code to demonstrate working of5 # Python3 code to demonstrate working of6# Using list comprehension8

đầu ra.

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]

Phương pháp #3. Sử dụng các phương thức list[],map[],startswith[]

Python3




# Python3 code to demonstrate working of

# Remove Negative Elements in List

 

# initializing list

test_list___= [___

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4_______3_______8
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4_______3_______5_______3_______1_______3_______7_______3_______ 8

 

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
9

# Python3 code to demonstrate working of0[9

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
44 # Python3 code to demonstrate working of5 # Python3 code to demonstrate working of6# Python3 code to demonstrate working of7

 

# Remove Negative Elements in List

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
49_______99__________# Python3 code to demonstrate working of2_______100_______9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
53[9# Python3 code to demonstrate working of6
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
56

# Remove Negative Elements in List0_______99_______

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
59

# Remove Negative Elements in List3

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
61_______95_______5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
63[9# Remove Negative Elements in List9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
67
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
68

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
69# Remove Negative Elements in List7[9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
72
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
73
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
74
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
75
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
76
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
77=
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
79
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
80

Chủ Đề