How might you do a case-insensitive search through text? In other words, how can you search through a string for another string, ignoring capitalization (e.g. finding 'zeus' in 'My name is Zeus!')?
Programs are often written by a programmer and used by someone else. So far, we have been using the interpreter as a way of performing operations for ourselves and not writing programs to be used by others. We can prompt the user to enter information using the input function. The input
function takes one optional argument, a prompt to display to the user. In the example below (and in all future examples), user input looks like this:
.
Note that input
always returns a string. If you want a number from the user, you must convert it to a number.
Write a short program that:
An example run is:
Python comes with a lot of pre-written code for us to use. This course will cover only a very small portion of this pre-written code. This pre-written code is refered to as the "Python Standard Library" and can be found on Python's website: https://docs.python.org/3/library/index.html.
To gain access to a module, you must import it. Imports generally occur at the top of a source code file. To import the math module:
Once a module is imported, you can call a function by using moduleName.functionName
. For example, to calculate the base-10 logarithm of 1000, we can use the log10
function:
If you will be using a function a lot, it might be good to just import that function from the module. For example, if a source code file may want to end the program, it could import just the exit
function from the sys module:
From IDLE, the exit
function does not do anything. It only works when run from a source code file or from the command line.
Boolean expressions are expressions that evaluate to either True
or False
values. These are very useful for if statements, while loops, and other common tasks. Before we get to these though, we first need to discuss Boolean expressions. Boolean expressions can be:
These are the values True
or False
.
Relational operators evaluate the relationship between two values. There are six operators:
Operator | Meaning | Example |
---|---|---|
< | less than |
>>> y = 9 >>> x < y True |
<= | less than or equal to |
>>> y = 9 >>> x <= y True |
> | greater than |
>>> y = 9 >>> x > y False |
>= | greater than or equal to |
>>> y = 9 >>> x >= y False |
== | equal |
>>> y = 9 >>> x == y False |
!= | not equal |
>>> y = 9 >>> x != y True |
Note that the equal operator (==
) is different from the assignment operator (=
).
You can only compare compatible types. Comparing a string and a float will not work, even if the string looks like a number. Comparing an int and a float will work though.
It is possible to compare strings using these relational operators. However, Python cares about capitalization. So, 'cat' and 'Cat' are not equal. Additionally, 'Cat' comes before 'cat' because of the underlying Unicode representation of the characters. For this course, just know that string comparisons are possible, but capitalization makes things a little inconvenient. How can you do comparisons without worrying about capitalization?
Comparing floats needs to be done carefully. You should rarely (never) use the equality operator because two numbers are equal only if their underlying binary representations are equal. Mathematically, 9.3/7.8 is equal to (10*9.3)/(10*7.8). However, in Python (and most other programming languages), they are not:
To compare floats, decide on a tolerance/threshold and see if the absolute value of the difference between the numbers is below that threshold:
Certain methods return a boolean value. Some examples are shown below.
Of course, other methods can also be part of a boolean expression, but you'll need to use the relational operators to determine a True
or False
value.
Logical operators manipulate the result of boolean expressions. Python provides the standard three operators:
Operator | Effect | Example |
---|---|---|
and | Connects two (or more) boolean expressions. Both expressions must be true for result to be true |
>>> 'cat' in a and a[0].isupper() and a.endswith('.') True >>> #this looks like a sentence about a cat |
or | Connects two (or more) boolean expressions. At least one expression must be true for result to be true |
>>> option == 3 or option == 5 or option == 7 True >>> #option is one of the prime numbers less than 10 |
not | Reverses the truth of a boolean expression |
>>> not a.endswith('.') False >>> #a does not end with a period |
<< Previous Notes | Next Notes >> |