|
| 1 | +title: Basic Data Types in Python 3: Booleans |
| 2 | +slug: python-basic-data-types-booleans |
| 3 | +meta: Learn to use boolean (true and false) values in your Python 3 code |
| 4 | +category: post |
| 5 | +date: 2019-11-15 |
| 6 | +modified: 2019-11-15 |
| 7 | +newsletter: False |
| 8 | +headerimage: /img/191115-python-basic-data-types-booleans/header.jpg |
| 9 | +headeralt: Learn basic Python data types in TwilioQuest 3 - Booleans |
| 10 | +author: Kevin Whinnery |
| 11 | +authorlink: https://www.twilio.com/quest |
| 12 | + |
| 13 | +Welcome back to our ongoing series of blog posts on basic data types in Python 3! |
| 14 | +Last time, we explored the functionality of |
| 15 | +[strings](/blog/python-basic-data-types-strings.html). Today, we dive in to |
| 16 | +another key data type - booleans. Booleans (and "boolean logic") are an |
| 17 | +important concept in programming, representing the concept of "true" and "false". |
| 18 | + |
| 19 | +If you're learning Python, you might also want to |
| 20 | +[check out TwilioQuest 3](https://www.twilio.com/quest/download). |
| 21 | +You'll learn about basic data types like the boolean, and much more about |
| 22 | +Python programming. |
| 23 | + |
| 24 | +Ready to learn how to use booleans in Python 3? Let's get started! |
| 25 | + |
| 26 | +## Booleans in Python 3 |
| 27 | + |
| 28 | +[Booleans](https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not) |
| 29 | +are a concept that exists in every programming language. A boolean represents |
| 30 | +the idea of "true" or "false". When you are writing a program, there |
| 31 | +are often circumstances where you want to execute different code in different |
| 32 | +situations. Booleans enable our code to do just that. |
| 33 | + |
| 34 | +You can declare a boolean value in your code using the keywords `True` and |
| 35 | +`False` (note the uppercase). The following code would create two boolean |
| 36 | +values and assign them to variables. |
| 37 | + |
| 38 | +```python |
| 39 | +mullet_looks_good = False |
| 40 | +python_is_fun = True |
| 41 | +``` |
| 42 | + |
| 43 | +More commonly, a boolean value is returned as a result of some kind of |
| 44 | +comparison. The following code example would store a boolean value of `False` |
| 45 | +in the `have_same_name` variable after using the |
| 46 | +[equality comparison operator](https://docs.python.org/3/library/stdtypes.html#comparisons), |
| 47 | +the `==` symbol. |
| 48 | + |
| 49 | +```python |
| 50 | +my_name = "Wammu" |
| 51 | +your_name = "Kars" |
| 52 | + |
| 53 | +have_same_name = my_name == your_name |
| 54 | +``` |
| 55 | + |
| 56 | +### Boolean logic |
| 57 | + |
| 58 | +Booleans are used in your code to make it behave differently based on current |
| 59 | +conditions within your program. You can use boolean values and comparisons in |
| 60 | +conjunction with the `if`, `elif`, and `else` keyoards as one means to achieve |
| 61 | +this. |
| 62 | + |
| 63 | +```python |
| 64 | +my_age = 10 |
| 65 | + |
| 66 | +if my_age >= 100: |
| 67 | + print("One hundred years old! Very impressive.") |
| 68 | +elif my_age <= 3: |
| 69 | + print("Awwww. Just a baby.") |
| 70 | +else: |
| 71 | + print("Ah - a very fine age indeed") |
| 72 | +``` |
| 73 | + |
| 74 | +In addition to testing for truth, you can also check if conditions are not |
| 75 | +true using the `not` keyword. |
| 76 | + |
| 77 | +```python |
| 78 | +favorite_team = "Vikings" |
| 79 | + |
| 80 | +if not favorite_team == "Vikings": |
| 81 | + print("Oh - how unfortunate.") |
| 82 | +else: |
| 83 | + print("Skol, Vikings!") |
| 84 | +``` |
| 85 | + |
| 86 | +### More complex boolean logic |
| 87 | + |
| 88 | +Sometimes you will need to evaluate multiple conditions in your boolean logic. |
| 89 | +For this purpose, you'll combine the `and` and `or` keywords. The `and` keyword |
| 90 | +compares two boolean values and returns `True` if both are true. The `or` keyword |
| 91 | +compares two values and returns `True` if any of the statements are true. |
| 92 | + |
| 93 | +Let's look at an example. That uses the `in` keyword to see if a string is |
| 94 | +inside a **list** of values (we'll cover lists in a future article). |
| 95 | + |
| 96 | +```python |
| 97 | +favs = ["Donatello", "Raphael"] |
| 98 | + |
| 99 | +if "Michelangelo" in favs and "Donatello" in favs: |
| 100 | + print("Those are my favorite ninja turtles too!") |
| 101 | +elif "Michelangelo" in favs or "Donatello" in favs: |
| 102 | + print("Well, one out of two isn't bad...") |
| 103 | +else: |
| 104 | + print("Huh - not what I would have chosen.") |
| 105 | +``` |
| 106 | + |
| 107 | +## Wrapping up |
| 108 | + |
| 109 | +Booleans are an important tool in any programming language. Using boolean logic, |
| 110 | +your code can react to data inside your program, and carry out different |
| 111 | +instructions under different circumstances. Hopefully, you've learned a bit |
| 112 | +about how to work with booleans in Python 3! Stay tuned for more blog posts in |
| 113 | +this series to learn more about basic data types like strings, numbers, |
| 114 | +booleans, lists, and dictionaries. |
| 115 | + |
| 116 | +Also, be sure to |
| 117 | +[download and play TwilioQuest 3](https://www.twilio.com/quest/download) |
| 118 | +to learn even more about Python! |
0 commit comments