Numbers, Bools and Strings

Numbers

What can we do with ints and floats and other number types?

Operators

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)

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

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

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.