Boolean & None
In this section, we introduce two more data types, namely boolean (bool
) and None (NoneType
). Let's start with the latter one,
NoneType
.
None (NoneType
)
NoneType
is a special data type in Python that represents the absence
of a value.
... which outputs:
None
can be used as a placeholder for a variable which will be
assigned a value later on. Furthermore, if a program was not able to return
a value, None
can be used as a return value.
Later, None
will play a bigger role.
For now, we simply keep in mind that None
is a thing.
Detour: TypeError
Often, you’ll want to use a variable’s value within a message. For example, say you want to wish someone a happy birthday. You might write code like this:
... results in.
Traceback (most recent call last):
File "C:\\IPython\core\interactiveshell.py", line 3577, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-34-80a141e301d6>", line 2, in <module>
message = "Happy " + age + "rd Birthday!"
~~~~~~~~~^~~~~
TypeError: can only concatenate str (not "int") to str
You might expect this code to print the simple birthday greeting, Happy
23rd birthday!
. But if you run this code, you’ll see that it generates an
error.
This is a TypeError
. It means Python encounters an unexpected
type in age
, as strings were mixed with integers in the expression. We will
easily fix the TypeError
in the next section.
Casting
When you use integers within strings like this, you need to specify explicitly
that you want Python to use the integer as a string of characters.
You can do this by wrapping the variable in the str()
function, which tells Python
to represent non-string values as strings:
Changing the type of age
to string is called casting.
Info
A TypeError
can stem from various 'sources'. This is just one
common example.
In the following example the integers 3
and 2
were
implicitly cast to floating point numbers, to calculate the result,
which is a floating point number.
With the function int()
any value can be explicitly cast into an
integer, if possible. The value to be cast is passed as the input parameter.
To explicitly cast a value into a float, use the function float()
.
Casting
For each of the given variables, check whether you can cast them to
another type. First, print the type of each variable. Then, use
int()
, and float()
to cast the variables.
Note
Remember the f-string (f"..."
) from the previous section?
Try a slightly modified example from above.
You'll notice, that there's no need for any explicit casting of age
.
Whenver, you want to include a variable in a string, remember that f-strings might be more convenient. 😉
Booleans
Computers work with binary (e.g. True
or False
).
Such information can be stored in a single bit. A boolean is either
True
or False
. Booleans are often used to keep
track of certain conditions, such as whether a game is running or whether a
user can edit certain content on a website:
Recap
In this section, we have introduced two more data types in Python:
- None (
NoneType
) - and Booleans (
bool
)
Now, we have covered all basic types! 🎉 With that knowledge, we can already start to do comparisons and logical operations.