Python Boolean Logic: True + False

Python Logic: Is this Math or Magic?🤔 Day 8 of my Python journey! C++ logic tells me: You can't add words to numbers. Python logic says: Hold my coffee ☕ Check out this snippet: result = True + True + False * True What do you think the print(result) output will be? Drop your guess in the comments! 👇 A) True B) 2 C) 3 D) Error Hint💡: It comes down to how Python stores Booleans as Integers! #Python #LearninginPublic #30DaysOfCode #ProgrammingLogic #Day8

  • graphical user interface

2 as True will be converted into numerical value of 1 While False=0 Result= 1+1+0*1 Result= 2

The result should include learning Python. It's a different programming language which means things are done differently. You might learn faster if you stick more closely to a good tutorial or course and start more humbley. "Beginner's Mind" (or Shoshin)

Answer: 2, because we are performing Arithmetic Operation on 'True' and 'False' (not Boolean operations like: and, or, not). So True is treated here as 1, and False is treated here as 0.

FIrst you are multiple False * True True = 1 and False = 0 in numerical value Result = 1 + 1 + 0 * 1Final Result come out is 2.

That phenomena is called integral promotion. The interpreter will promote the boolean values to its integer counterparts before evaluating the expression. Each language have its own rules to do integral promotion or demotion of data types when evaluating expressions. For instance: #include<stdio.h> int main(void) { int a = 'c' + 1; printf("%d", a); return 0; } This will yield "100" as result. The char type constant 'c' is promoted to its integer counterpart and added to 1.

Option B are correct answer

See more comments

To view or add a comment, sign in

Explore content categories