6.2. Data Type for
In order for us to build code that can make decisions, we need to understand how programming languages represent true and false.
If we run the following code, we would see in the following output.
1 2 3 4
print('dog' == 'cat') print(3 < 4) print(3 > 10) print('dog' != 'cat')
False True False True
In the code above, we make four comparisons and then print the results to the
console. Python evaluates each comparison as being either
- In line 1, the equality operator
==compares the strings
'cat'. Since these are NOT the same, the comparison returns the value
- In line 2, the operator
<compares the values of 3 and 4. Since 3 is indeed less than 4, comparison returns the result
- The comparison in line 3 returns
False, since 3 is NOT larger than 10.
- In line 4, the
!=operator stands for “not equal”, so
'dog' != 'cat'returns
True, while something like
3 != 3would return
Recall that the
type() function tells us the data type of what’s inside
1 2 3 4 5
print(3 < 4) print(type(3 < 4)) print('dog' == 'cat') print(type('dog' == 'cat'))
True <class 'bool'> False <class 'bool'>
In the previous chapter, we learned about three data types—
string. The first two deal with numbers, while
deals with collections of characters.
We can now add the data type
bool, which stands for
boolean value, to our list.
22.214.171.124. Boolean Values¶
There are only two boolean values—
Capitalization matters! Since Python is case-sensitive,
false are NOT valid boolean values.
False are NOT strings. We can see this by printing
another set of
1 2 3
print(type(True)) print(type("True")) print(True == "True")
<class 'bool'> <class 'str'> False
Putting quotes around boolean values (
"False") makes them
strings, just like
"1234" is a string rather than an
int data type.
Line 3 shows that even though they look similar,
NOT the same!
bool are different data types.
126.96.36.199. Data Type Review¶
- The string (
str) data type represents a collection of characters.
- The integer (
int) data type represents a whole number.
- The float (
float) data type represents a decimal value.
- The boolean (
bool) data type represents