GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.fullstackpython.com — Basic Data Types in Python 3: Booleans Basic Data Types in Python 3: Booleans
Posted by
Kevin Whinnery on November 15, 2019.
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 3Booleans 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 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 my_name = "Wammu" your_name = "Kars" have_same_name = my_name == your_name Boolean logicBooleans 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 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 favorite_team = "Vikings"
if not favorite_team == "Vikings":
print("Oh - how unfortunate.")
else:
print("Skol, Vikings!")
More complex boolean logicSometimes you will need to evaluate multiple conditions in your boolean logic.
For this purpose, you'll combine the Let's look at an example. That uses the 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 upBooleans 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! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |