Numbers
What can we do with ints and floats and other number types?
Operators
- +
addition
- *
multiplication
- -
subtraction, negation
-
/
true division
- //
floor division (truncates fractional remainders)
- **
exponentiation
- %
remainder
- ()
parentheses group subexpressions, but are also used to create a tuple; they don't imply multiplication without *
- +=, *=, /=,-=
operates and assigns all at once
Open a console, or shell, to use Python in interactive mode. Guess the outcome of each line of code then press return. Make sure you understand each operation before pressing return.
Operator precedence (order of operations)
- Grouping symbols
- Exponents
- Multiplicaton and division left to right
- Addition and subtraction left to right
Built-in functions to use on numbers
Try in the console, or shell:
Useful Functions from other Modules
Some functions, or methods, are not available to you unless you import the module that contains them. Try these in the shell. Do the random functions several times.
Hint: Use the up arrow to reuse a previous command. Try it now to choose another random number.
Challenge: Can you calculate temperature? Follow these guidelines.
Let's write 3 lines of code to compute the Celsius temperature if you know the Fahrenheit temperature.
This time, open a new file, name it, and create the program.
The formula is C = 5/9(F-32).
Remember that Python does not know that () can mean multiply.
Start your program by assigning a value of your choice to F.
Then on a new line, enter the formula.
Finally, print C.
Run your program and fix any errors.
Now adjust your formula to take a user's input value for F, instead of assigning a value to F.
Use the input() function as seen below. Note that the user's input comes in as a string and you'll need to change the type before computing.
Raise your hand for a check when you get to this point.
Booleans
Traditionally, bools include the values True and False, which correspond to 1 and 0.
In Python, everything has a boolean value. Most numbers, strings, lists, etc correspond to a boolean value of True.
False is the value for 0, {}, [], ‘’, None.
Strings
Strings are always in quotes. Single quotes and double quotes are interchangeable for the most part.
Common String Operations for a string S
- len(S)
length of string
- S * 3
repeat
- S + “another string”
concatenate
- S[i]
Uses index to select one character
- S[i:j]
Slices a group of characters
- S.find(‘c’)
Finds index (position) of given character
- S.rstrip(), S.lstrip(), S.strip()
remove whitespace characters from right, left, or both sides
- S.replace(‘cc’, ‘pa’)
replaces first string with second
- S.split(‘,’)
split on delimiter, in this case a comma
- S.lower(), S.upper()
change case
- S.endswith(‘es’)
returns True or False depending if the string ends with es
Try these in the shell:
Notice that the S string stays the same after all the changes. That is because strings are immutable. In order to "change" a string you actually have to write over it.
Try these:
More on Indexing and Slicing
- An index is the position of a number when counting positions starts with 0. The first character in the string has index 0.
- A slice from i to j denoted [i:j] starts at i and ends before j.
- [i:] fetches characters from i to the end of the string. [:j] fetches characters from the beginning.
- A negative index starts at the right end with -1.
- A step, k, can be used [i:j:k] in a slice. k can be positive or negative.
Try these in the shell. Before each one, try to predict what will happen.
Challenge: Slice my_string to get the following outputs:
“free”
“u”
“tnf”
Raise your hand for a check when you get to this point.