Placeholder Image

字幕列表 影片播放

  • Computers are funny creatures. They think in terms of 1’s and 0’s, True and False.

  • While Python has several numeric types, there is only one logical type: booleans. A boolean

  • can only take 2 values: True or False. And this is all you need, IF you are logical...

  • Booleans are a built-in data type in Python. There are only two values: True and False.

  • Take care to note thattrueandfalseare both capitalized. If you typeTrue

  • correctly into the console, Python echoes it back to you.

  • But if you type it incorrectly, you will receive a NameError.

  • The same thing happens with False. If you capitalize it, you get a friendly echo. Uncapitalized

  • returns an error.

  • Booleans are commonly encountered when comparing two objects. For example, suppose “a=3”

  • and “b=5”.

  • To compare two numbers, use the double equals operator:

  • We getFalsesince “a” and “b” are different integers.

  • Notice that you use one equals sign to assign numbers to a variable,

  • and a double equals sign to compare them.

  • In addition to testing if two numbers are the same, you can test if they are different

  • using thenot equaloperator. This comparison returnsTruebecause it is true that

  • “a” does not equal “b”. By the way, the exclamation mark is commonly used as a

  • logicalnotsymbol in programming languages. So this symbol literally reads asnot equal”.

  • And finally, in addition to comparing two numbers for equality or inequality, you can

  • test to see if one is larger than the other.

  • Is “a” greater than “b”? No. This is a false statement.

  • Is “a” less than “b” ? Yes. This is a true statement.

  • If you inspect the type of Trueand False, you see the type isbool”.

  • This suggests another way to create booleans: by passing values to the boolean constructor.

  • For example, let’s convert some numbers to booleans: 28…

  • – e…

  • 0…

  • In Python, 0 is converted to False, while every other number is converted to True.

  • We can also convert strings to booleans. For example,

  • Turing”...

  • A space...

  • And the empty string

  • In Python, the empty string is converted to False, while every other string is converted

  • to True.

  • This is a general principle in Python. When converting a value to a boolean, trivial values

  • are converted to False, while non-trivial values are converted to True.

  • Just as you can convert objects to booleans, you can convert booleans to other types of

  • objects.

  • If you convertTrueto a string, it returns… “True”. But notice this is

  • a string, since it is surrounded by quotes. The boolean value does not have quotes.

  • A similar thing occurs when you convertFalseto a string. The string has quotes, the boolean

  • does not.

  • You can also convert booleans to numbers. If you convertTrueto an integer, you

  • get 1. And if you convertFalseto an integer,

  • you get 0.

  • Look what happens if you add a number and a boolean:

  • Python recognizes that you are trying to addTrueto an integer, so it first converts

  • it to an integer then adds. What do you think 10 timesFalsewill be?

  • Zero. Like before, Python recognizes you are

  • trying to perform an arithmetic operation, so it convertsFalseto the number 0

  • then multiplies.

  • Is this something you will use? Probably not. But it does highlight that Python treats 1

  • as True and 0 as False, and vice-versa. In computer science, this is a fundamental fact.

  • True or False: Booleans come in only two valuesTrue!

  • True or False: We will be making another Python video soonDefinitely true!

  • True or False: Subscribers make fewer errors in their codeVery, very True

Computers are funny creatures. They think in terms of 1’s and 0’s, True and False.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

Python Booleans || Python教程 || 學習Python編程 (Python Booleans || Python Tutorial || Learn Python Programming)

  • 18 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字