3.3: 🌗 Boolean Variables

Topic

3.3: 🌗 Boolean Variables

Topic Progress:

Booleans are the final type of variable that we’ll be discussing in the black hat part of this course. The name may sound a little frightening, but this is actually the simplest type of variable we’ve covered so far.

Unlike numerical and string type variables, which can have a huge range of different data (any number, any possible combination of characters), a boolean can only have two states. Booleans are always either true or false. You can think of true as “yes” and false as “no”, for now.

Here are a couple of examples of what boolean variables look like in code:

x = True
y = False

Booleans are super useful in helping computers make decisions – most big programs rely on them. This will be covered more in-depth in the next section.

For this exercise, your task is to make a change to the first line of the following code, so that the print() statement outputs True.

Note: the == in the code checks if two elements are the same.

A quick note which refers to variables more as a whole. In the Input and Output section, we met something really odd. The function (remember a function is just a block of code already written that we’re referring back too) input() could actually be used to assign a value of something. Such as:

aVarble = input()

This doesn't seem to make much sense at first. After all, if input() is just some code that someone else has written, how can it have a value? The short answer is because programmers decided they wanted it too. Some functions, like good ol' reliable 'print()' do their job happily without any need to 'give something back'. You tell it to print something, and it does. However, the input() function's job is to ask for input and let us use that in our code. So it needs some way of handing that information back to us. So, whoever wrote the code which input() uses decided that how we get that information is that the value of input() is the user's input. This is called 'returning'. So the input function runs it's code, then whatever the user gives Python makes the term 'input()' equal to that string. So then it makes sense that a variable can equal the function because we know that function (after the function has ran) will be whatever the function returns, in this case, the user's input.

@

Not recently active