Boolean variables
So far we have seen textual variables as well as numeric integer variables. We will cover
boolean
variables and their use cases in the coming few exercises.Boolean variables can only take two values
True
and False
. They can represent if a statement (condition) is true or false.is_better = True
sometimes = False
playing_football = True
hello = False
print(sometimes, playing_football)
This program would print
False True
as the value of sometimes
is set to be False
and the value of playing_football
is set to be True
.To get types of variables we can use the
type
function.a = True
b = 123
c = 'hello'
d = 'False'
print(type(a), type(b), type(c), type(d), type(1234))
This program would print
<class 'bool'> <class 'int'> <class 'str'> <class 'str'> <class 'int'>
. Meaning that:a
is of typebool
(a boolean variable which can be eitherTrue
orFalse
)
b
is of typeint
(a numeric integer variable)
c
of typestr
(a textual/string
variable)
d
of typestr
(a textual/string
variable) - not a boolean
1234
is of type int (a numeric value)
Challenge
Create a numeric variable
favorite
and assign your favorite number to it. Create a variable best
and assign True
to it. Your program should print the types of variables
favorite
and best
in the output as well as the types of True
, False
, and 'long long text'
.Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB