Basic data types in python 3
Welcome back to our ongoing series of blog posts on 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). 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.
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. 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.
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.
Strings in Python 3
One of the most common data types in any programming language is a string
. A string represents a series of characters, which you would use to represent usernames, blog posts, tweets, or any text content in your code.
Strings are “immutable”
In Python, strings are considered immutable — once you create them, they can’t be changed. You can, however, use a variety of methods to create new strings from existing strings. This type of work in programming is called string manipulation. Some web developers joke that at the end of the day, their job is just mashing strings together — and this isn’t far from the truth!
Here are some common tasks you might undertake when using strings in your code.
Common task — combining strings together
Combining strings together — concatenating them — is a very common task. In Python 3, you can use the +
operator for this purpose. You can use the +
operator multiple times to concatenate multiple strings.
Common task — inserting data into strings
Another common task with strings is inserting data into a specific place within a string. In programming, we call this string interpolation. Python 3 provides a handy tool for doing this called “f” strings. The “f” in “f strings” stands for format — you can insert other data from your program into a string when you define it rather than doing complex string concatenation as demonstrated previously.
Here is an example of creating a formatted string — note the letter f
is included just before the first double quote when defining the message
variable. When you want to insert data from your program into the string, you can include it between two "curly braces" - the {
and }
characters.
Common task — using built-in string methods to manipulate strings
String objects have a number of methods to perform common tasks, like changing the case of strings or trimming their content. Below, you’ll find a few examples. In two of these examples, we are creating a string variable, and then assigning the same variable a new value, which is the result of calling a method on a string object.
Type casting
Frequently, you will want to convert data from one type into another. In programming, we call this process type casting. There are a number of functions built in to Python which allow us to do these type conversions on basic data types.