| 
									
										
										
										
											2018-10-12 16:35:31 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | title: Python Bool Function | 
					
						
							|  |  |  |  | localeTitle: Python Bool函数 | 
					
						
							|  |  |  |  | --- | 
					
						
							|  |  |  |  | `bool()`是Python 3中的内置函数。此函数返回一个布尔值,即True或False。它需要一个参数, `x` 。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## 参数
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-05 04:02:12 +08:00
										 |  |  |  | 它需要一个参数`x` 。使用标准[真值测试程序](https://docs.python.org/3/library/stdtypes.html#truth)转换`x` 。 | 
					
						
							| 
									
										
										
										
											2018-10-12 16:35:31 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-05 04:02:12 +08:00
										 |  |  |  | ## 返回值
 | 
					
						
							| 
									
										
										
										
											2018-10-12 16:35:31 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 如果`x`为false或省略,则返回`False` ;否则返回`True` 。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## 代码示例
 | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | print(bool(4 > 2)) # Returns True as 4 is greater than 2  | 
					
						
							| 
									
										
										
										
											2019-01-05 04:02:12 +08:00
										 |  |  |  | print(bool(4 < 2)) # Returns False as 4 is not less than 2  | 
					
						
							|  |  |  |  | print(bool(4 == 4)) # Returns True as 4 is equal to 4  | 
					
						
							|  |  |  |  | print(bool(4 != 4)) # Returns False as 4 is equal to 4 so inequality doesn't holds  | 
					
						
							|  |  |  |  | print(bool(4)) # Returns True as 4 is a non-zero value  | 
					
						
							|  |  |  |  | print(bool(-4)) # Returns True as -4 is a non-zero value  | 
					
						
							|  |  |  |  | print(bool(0)) # Returns False as it is a zero value  | 
					
						
							|  |  |  |  | print(bool('dskl')) # Returns True as the string is a non-zero value  | 
					
						
							|  |  |  |  | print(bool([1, 2, 3])) # Returns True as the list is a non-zero value  | 
					
						
							|  |  |  |  | print(bool((2,3,4))) # Returns True as tuple is a non-zero value  | 
					
						
							|  |  |  |  | print(bool([])) # Returns False as list is empty and equal to 0 according to truth value testing  | 
					
						
							| 
									
										
										
										
											2018-10-12 16:35:31 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-05 04:02:12 +08:00
										 |  |  |  | [运行代码](https://repl.it/CVCS/2) | 
					
						
							| 
									
										
										
										
											2018-10-12 16:35:31 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-05 04:02:12 +08:00
										 |  |  |  | [官方文件](https://docs.python.org/3/library/functions.html#bool) |