Basic Data Types in Python 3: Booleans
source link: https://www.fullstackpython.com/blog/python-basic-data-types-booleans.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Welcome back to our ongoing series of blog posts on basic data types in Python 3! Last time, we explored the functionality of strings. Today, we dive in to another key data type - booleans. Booleans (and "boolean logic") are an important concept in programming, representing the concept of "true" and "false".
If you're learning Python, you might also want to check out TwilioQuest 3. You'll learn about basic data types like the boolean, and much more about Python programming.
Ready to learn how to use booleans in Python 3? Let's get started!
Booleans in Python 3
Booleans are a concept that exists in every programming language. A boolean represents the idea of "true" or "false". When you are writing a program, there are often circumstances where you want to execute different code in different situations. Booleans enable our code to do just that.
You can declare a boolean value in your code using the keywords True
and
False
(note the uppercase). The following code would create two boolean
values and assign them to variables.
mullet_looks_good = False python_is_fun = True
More commonly, a boolean value is returned as a result of some kind of
comparison. The following code example would store a boolean value of False
in the have_same_name
variable after using the
equality comparison operator,
the ==
symbol.
my_name = "Wammu" your_name = "Kars" have_same_name = my_name == your_name
Boolean logic
Booleans are used in your code to make it behave differently based on current
conditions within your program. You can use boolean values and comparisons in
conjunction with the if
, elif
, and else
keyoards as one means to achieve
this.
my_age = 10 if my_age >= 100: print("One hundred years old! Very impressive.") elif my_age <= 3: print("Awwww. Just a baby.") else: print("Ah - a very fine age indeed")
In addition to testing for truth, you can also check if conditions are not
true using the not
keyword.
favorite_team = "Vikings" if not favorite_team == "Vikings": print("Oh - how unfortunate.") else: print("Skol, Vikings!")
More complex boolean logic
Sometimes you will need to evaluate multiple conditions in your boolean logic.
For this purpose, you'll combine the and
and or
keywords. The and
keyword
compares two boolean values and returns True
if both are true. The or
keyword
compares two values and returns True
if any of the statements are true.
Let's look at an example. That uses the in
keyword to see if a string is
inside a list of values (we'll cover lists in a future article).
favs = ["Donatello", "Raphael"] if "Michelangelo" in favs and "Donatello" in favs: print("Those are my favorite ninja turtles too!") elif "Michelangelo" in favs or "Donatello" in favs: print("Well, one out of two isn't bad...") else: print("Huh - not what I would have chosen.")
Wrapping up
Booleans are an important tool in any programming language. Using boolean logic, your code can react to data inside your program, and carry out different instructions under different circumstances. Hopefully, you've learned a bit about how to work with booleans in Python 3! Stay tuned for more blog posts in this series to learn more about basic data types like strings, numbers, booleans, lists, and dictionaries.
Also, be sure to download and play TwilioQuest 3 to learn even more about Python!
Recommend
-
74
Store a associative array of booleans in the database efficiently using binary numbers.
-
49
I’ll start this article with a warning, I mention the use of Lambda Calculus, but do not let that scare you! I promise, no knowledge of λ-Calculus is required to understand or use the main concepts in this article! Phew!
-
53
In my last Functional JS article, we went over recursive patterns that allow you to operate/iterate over array values. This time, we’re going to get a bit more abstract to hopefully explain some of…
-
10
Programming with truth and statefulness. I like formal specification because I don’t really understand it. That is kind of my...
-
11
Be Careful with Async Functions that Return Booleans Here’s a fun bug I recently encountered… Let’s say we have this async JavaScript function: const isBroken = async () =>...
-
8
Compiling a Lisp: Booleans, characters, nil September 2, 2020 first –
-
4
Writing a Lisp, Part 1: Booleans October 28, 2016 Last time we wrote a simple interpreter that could read in numbers. That’s cool and all, but we’ll need to re...
-
7
Basic Data Types in Python 3: StringsThere is a lot to learn on your Python journey when you are new to the programming language. Once you are comfortable writing and exe...
-
5
« back — written by Brent on February 14, 2020 Bitwise booleans in PHP ...
-
6
Python Booleans: Leveraging the Values of Truth Understanding how Python Boolean values behave is important to programming well in Python. The Python Boolean type is one of Python’s
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK