Q1 This is on five minutes before class begins. Start it when you arrive.
Last Time We learned about the basic scalar types in Python: numbers (int, float, complex), booleans, and strings.
If you like Wordle, this is Required Viewing
Casting
A cast is a temporary request to regard an object of one type as being of another. Be warned: not all casts make sense. Make a bad one and Python will throw an error, resulting in program death. Note, in the table below, everything casts to a string.
from/to | int | float | bool | str |
---|---|---|---|---|
int | * | "obvious" unless the integer is too big | False if 0, True otherwise | numerical string |
float | truncates fractional part | * | False if 0, True otherwise | numerical string |
bool | 1 for True and 0 for False | 1 for True and 0 for False | * | boolean string |
str | the value given, if the string is a parseable integer | the value given, if the string is a parseable float | if the string is empty, False , True otherwise |
* |
How do I find out ... ?
Python Documentation Here are links to some useful pages that will help you learn Python.
- Python builtin types page Documentation on both scalar and containter types can be found here.
- Python builtin functions page This is organized alphbetically. Visit this page from time to time, pick out something that looks interesting or something at random, and learn how to use it.
String Varieties
Special Characters
\n
This is a newline character.\t
This is a tab.\r
This is a tab (Windoze uses this).\\
This is a whack.\xYY
This prints out the character whose ascii value is YY in hex.\xYYYY
This prints out the character whose unicode value is YYYY in hex.
Format Strings
Raw Strings
Triple-Quoted Strings
Make us!
* ** *** **** *****
* *** ***** ******* *********
* * * * * * * * * * * * * * * * * * * * * * * * *
Using input