Python Basics Tutorial 60 Lessons-18th class bool type conversion

Source: Internet
Author: User
Tags in python
Python Basic Tutorials 60 lessons-18th class bool type conversion "Python 18th class" bool type conversion
A few last words left yesterday about the conversion of type bool, which has one row:
BOOL (' False ')
Print the result and you will find that it is true. What is the reason.


First, because in Python, the following values are considered false:
The second and 0 figures, including 0,0.0
Third, empty string, including ', ' '
The none that represents a null value
Five, empty collection, including (), [],{}
The other values are considered true.


None is a special value in Python, which means nothing, and it is not the same as 0, null characters, False, or empty collections. About the collection, we'll talk about the following course.
So, ' False ' is a string that is not empty, and is true when converted to bool type.
The result of the same bool (") is true, and a space cannot be counted as an empty string.
BOOL (") is false.


In the IF, while and other conditional judgment statements, the judgment condition is automatically converted to bool. Like what
A = ' 123 '
If a:
Print ' This isn't a blank string '


This is a very common way of writing in programming. The effect is equal to
if BOOL (a)
Or

If a!= '



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.