18
loading...
This website collects cookies to deliver better user experience
35e3
.True
and False
written out all the time, but rather the evaluation of an expression that reduces to one of those values. Keep in mind I am a beginner, so part of this information is my observation and should not be taken as gospel. print('Are you legal drinking age? ', 18 < 21)
False
, since the above expression evaluates to a falsy value.18
and 21
would be examples of Integer Literals, and <
would be an example of an Operator. If we wanted to make our expression more flexible and inclusive, we could use a variable. For instance, if a user inputs their age (for me: 30, for you it may be different), we can store that number in a variable then use it in our example like this: age < 21
. If we were to do this, we would then also be using an Identifier.18 < 21 and 24 < 21
evaluates to the Boolean data type False, because with an and
operator, both statements must be True.
18 < 21 or 24 < 21
evaluates to True, because with an or
operator, at least one statement must be True, and the above essentially states False or True
.
not(1 < 0 and 2 < 0)
evaluates to True, because the not
operator reverses a result. If you are familiar with PEMDAS, Python follows this and so contents within a parentheses are evaluated before exponents, subtraction, etc. In this case, the inner contents evaluate to False, so reversing False evaluates to True.
sister, brother = 21, 21 # sister is brother
- If we were to print(sister is brother) we would read True, because we are essentially asking Python to compare 21 to 21 to see if they are the same object.
print('sister' is not 'brother')
evaluates to True. Here, we are comparing two strings to see if both sides are the same object. I am not certain, but I believe Python String characters are comprised of ASCII encoding, which uses numbers to present English characters(17, 18). As you may have inferred, the characters in each of the above strings would boil down to different number sequences, therefore it is True
that, in essence, a is not b
.
True
. Conversely, Falsy values include Constants such as False
or None
, 0
of any numeric type, and anything empty of the following: lists []
, tuples ()
, dictionaries {}
, sets set()
, strings ""
or ranges range(0)
(10, 20). friends = ['suchan', 'ross', 'desirée']
. List items are indexed and start at 0. If I wanted to access 'Suchan' above, I could do this: print(friends[0])
. List items have a defined order that will not change (25). I won't say it's a best practice, but I will say that it is very common that a list will all contain the same data type. With that being said, your code won't break simply by having a float, integer and string within a list(26). Bear in mind that lists and other sequence and mapping types are iterable, meaning it is capable of returning its members/elements one at a time, permitting it to be iterated over in a for-loop
(27). While there may be no issue looping over a list of [1, 'two', False]
, note that data types have specific built-in methods that are only applicable to that data structure. So for instance, if you were looping over my friends list with the .capitalize()
, that would be fine because the first letter of each element could be capitalized. However, as seen with my mixed data-type example, applying the same method would result in a Python error: AttributeError: 'int' object has no attribute 'capitalize'
.parents = { "mom": "Marie" }
, though a dictionary's values can be of any data type(33). By comparison, a dictionary's keys must be of an immutable data type such as strings, numbers or tuples (34). A list of Immutable and Mutable Data Types in Python can be found here(35).