class: title-slide, left, bottom # Lecture 5 ---- ## **DANL 100: Programming for Data Analytics** ### Byeong-Hak Choe ### September 13, 2022 --- # Announcement ### <p style="color:#00449E">Accounting Expo</p> - Are you interested in learning about Accounting, Consulting, Audit or Tax? - Stop in for pizza and meet 20+ Firms (alumni and employers) from the Big 4, National Players and Regional firms! - When? September 15th, 5:00 PM-7:00 PM - Where? Ballroom - Dress code? Business Casual - Practice, prep or questions? In-person drop-ins! - South 110 (or 112) on September 13, 9:00 AM-Noon - South 110 (or 112) on September 14, 9:00 AM-4:00 PM --- # Announcement ### <p style="color:#00449E">How to use Canvas?</p> - Here is a brief introductory video of using Canvas: - [Link](https://vimeo.com/74677642) --- class: inverse, center, middle # Workflow <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --- # Workflow ### <p style="color:#00449E"> Shortcuts </p> - **F9** runs a current line (where the blinking cursor bar is) or selected lines. - **Home/End** moves the blinking cursor bar to the beginning/end of the line. - **Fn + **
/
works too. .pull-left[ ### <p style="color:#00449E"> Mac </p> - **command + N** opens a new script. - **command + 1** is the shortcut for #. - **command + 4** is the shortcut for block comment. ] .pull-right[ ### <p style="color:#00449E"> Windows </p> - **Ctrl + N** opens a new script. - **Ctrl + 1** is the shortcut for #. - **Ctrl + 4** is the shortcut for block comment. ] --- # Workflow ### <p style="color:#00449E"> More Shortcuts </p> - **Ctrl** (**command** for Mac Users) **+ Z** undoes the previous action. - **Ctrl** (**command** for Mac Users) **+ Shift + Z** redoes when undo is executed. - **Ctrl** (**command** for Mac Users) **+ F** is useful when finding a phrase in the script. - **Ctrl** (**command** for Mac Users) **+ R** is useful when replacing a specific phrase with something in the script. - **Ctrl** (**command** for Mac Users) **+ D** deletes a current line. --- # Workflow ### <p style="color:#00449E"> Code and comment style </p> - The two main principles for coding and managing data are: - Make things easier for your future self. - Don't trust your future self. - The `#` mark is Spyder's comment character. - The `#` character has many names: `hash`, `sharp`, `pound`, or `octothorpe`. - `#` indicates that the rest of the line is to be ignored. - Write comments before the line that you want the comment to apply to. - Consider using block commenting for separating code sections. - `#%%` defines a coding block in Spyder. <!-- - Break down long lines and long algebraic expressions with backslash `\`. --> --- class: inverse, center, middle # Data: Types, Values, Variables, and Names <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Python’s basic data types </p> <img src="../lec_figs/int-py-tab2-1.png" width="57%" style="display: block; margin: auto;" /> - **Type** contains the Python name of that type. - **Mutable?** indicates whether the value can be changed after creation. --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Mutability </p> - The type determines whether the data value contained by the box can be changed (mutable) or is constant (immutable). - A mutable object is like a box with a lid: not only can we see the value inside, we can also change it; - However, we can’t change the type of an object. .pull-left[ ```python a = (1, 2, 3) # tuple a[0] a[0] = 5 a ``` ] .pull-right[ ```python b = [1, 2, 3] # list b[0] b[0] = 5 b ``` ] --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Assignment </p> - In Python, we use `=` to assign a value to a variable. ```python # Here we assign the integer value 5 to the variable x. x = 5 # Now we can use the variable x in the next line. y = x + 12 y ``` - In math, `=` means *equality* of both sides. - In programs, `=` means **assignment**: *assign the value on the right side to the variable on the left side*. --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Variables Are Names, Not Places </p> - In Python, *variables* are just names. <div class="figure" style="text-align: center"> <img src="../lec_figs/int-py-fig2-3.png" alt="Names point to objects (variable `a` points to an integer object with value `7`)" width="29%" /> <p class="caption">Names point to objects (variable `a` points to an integer object with value `7`)</p> </div> --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Copying </p> - Assignment does not copy a value; - It just attaches a name to the object that contains the data. - What value is *assigned* to variable `y` at the end of the following code? ```python # Here we assign the integer value 5 to the variable x. x = 5 # Now we can use the variable x in the next line. y = x + 12 y x = 8 ``` --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Copying </p> - Assigning an existing variable `a` to a new variable named `b` just makes `b` point to the same object that `a` does. <div class="figure" style="text-align: center"> <img src="../lec_figs/int-py-fig2-4.png" alt="Copying a name (now variable `b` also points to the same integer object)" width="29%" /> <p class="caption">Copying a name (now variable `b` also points to the same integer object)</p> </div> --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Copying </p> - A **list** is a *mutable* array of values. - In the code below, `a` and `b` each points to a list with three integer members: ```python a = [2, 4, 6] b = a a[1] = 0 ``` - List members (`a[0]`, `a[1]`, and `a[2]`) are themselves like names, pointing to integer objects with the values 2, 4, and 6. - If both names point to a mutable object, we can change the object’s value via either name, and we’ll see the changed value when we use either name. --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Choose Good Variable Names </p> - It’s surprising how important it is to choose good names for your variables. - In many of the code examples so far, I’ve been using throwaway names like `a` and `x`. - In real programs, you’ll have many more variables to keep track of at once, and you’ll need to balance *brevity* and *clarity*. - For example, it’s faster to type `num_loons` rather than `number_of_loons` or `blueberry_inventory`, but it’s more explanatory than `n`. --- # Data: Types, Values, Variables, and Names ### <p style="color:#00449E"> Class Exercises </p> 1. Assign the integer value 33 to the variable `queen`, and print it. 2. What type is the value `3`? 3. What type is the value `3.0`? 4. What type is the expression `8 + 3.0`? 5. Modify the following line of code, so that a variable of string type data (`<class 'str'>`) is created. ```python var_created = 1.5 print( "The data type of var_created is: ", type(var_created) ) ``` --- class: inverse, center, middle # Numbers <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --- # Numbers - Let's look at the following Python’s simplest built-in data types in detail: - **Booleans** (which have the value `True` or `False`) - **Integers** (whole numbers such as `10` and `28`) - **Floats** (numbers with decimal points such as `3.141592`, or sometimes exponents like `1.0e8`, which means one times ten to the eighth power, or `100000000.0`) --- # Numbers ### <p style="color:#00449E"> Booleans </p> - The only values for the boolean data type are `True` and `False`. - The special Python function `bool()` can convert any Python data type to a boolean. - The `bool()` function takes any value as its argument and returns the boolean equivalent. .pull-left[ ```python bool(True) bool(1) bool(4) bool(-4) ``` ] .pull-right[ ```python bool(False) bool(0) bool(0.0) ``` ] --- # Numbers ### <p style="color:#00449E"> Integers </p> - Any sequence of digits in Python represents *a literal integer*: .pull-left[ - We can’t have an initial 0 followed by a digit between 1 and 9: ```python 0 01 ``` ] .pull-right[ - We can’t have any commas as a digit separator in the integer. - We can use the underscore (`_`) character as a digit separator ```python 1,000,000 # We get a tuple! billion = 1_000_000_000 billion 3_2_1 ``` ] --- # Numbers ### <p style="color:#00449E"> Integer Operations </p> .pull-left[ - Python can be a simple calculator. - Here is a table of the math operators: <img src="../lec_figs/int-py-ch3-operation.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right[ - Dividing by zero with either kind of division causes a Python exception ```python 12 + 8 12 - 8 13 * 1.2 2 + 3 * 4 2 ** 3 2 / 0 2 // 0 ``` ] --- # Numbers ### <p style="color:#00449E"> Integers and Variables </p> - In Python, the expression on the right side of the `=` is calculated first, and then assigned to the variable on the left side. .pull-left[ - Subtract `2` from `a`. - Assign the result of that subtraction to a temporary variable, `tmp`. - Assign the value of the temporary variable to `a`. ] .pull-right[ ```python a = 72 tmp = a - 2 a = tmp ``` ] --- # Numbers ### <p style="color:#00449E"> Integers and Variables </p> - We can combine the arithmetic operators with assignment by putting the operator before the `=`. .pull-left[ ```python a = 72 a -= 2 # This is like a = a - 2 ``` ] .pull-right[ ```python b = 62 b += 2 # This is like b = b + 2 ``` ] .pull-left[ ```python c = 72 c *= 2 # This is like c = c * 2 ``` ] .pull-right[ ```python d = 62 d /= 2 # This is like d = d / 2 ``` ] --- # Numbers ### <p style="color:#00449E"> Type Conversions </p> - To change other Python data types to an integer, use the `int()` function. - `int()` keeps the whole number and discard any fractional part. - `True` and `False` are converted to integer values `1` and `0`. .pull-left[ ```python int(True) int(False) ``` ] .pull-right[ ```python bool(1) bool(0) ``` ] .pull-left[ ```python int(72.3) int(1.0e4) ``` ] .pull-right[ ```python bool(1.0) bool(0.0) ``` ] --- # Numbers ### <p style="color:#00449E"> Floats </p> - Integers are whole numbers, but floating-point numbers (called **floats** in Python) have decimal points: .pull-left[ ```python 4. 4.0 04.0 ``` ] .pull-right[ ```python 4e0 4e1 4.0e1 4.0 * (10 ** 1) ``` ] --- # Numbers ### <p style="color:#00449E"> Floats </p> - To convert other types to floats, we use the `float()` function. .pull-left[ ```python float(True) float(False) ``` ] .pull-right[ ```python float(88) float('89') ``` ] --- # Workflow ### <p style="color:#00449E"> Continue Lines with `\` </p> - Programs are more readable when lines are reasonably short. - The recommended maximum line length is 80 characters. - If you can’t say everything you want to say in that length, you can use the continuation character: `\` (backslash). - Just put `\` at the end of a line, and Python will suddenly act as though you’re still on the same line. --- # Workflow ### <p style="color:#00449E"> Continue Lines with `\` </p> .panelset[ .panel[.panel-name[With `\`] ```python sum = 1 + \ 2 + \ 3 + \ 4 sum ``` ] .panel[.panel-name[Without `\`] ```python sum = 1 + ``` ] .panel[.panel-name[With `()`] - If you’re in the middle of paired parentheses (or square or curly brackets), Python doesn’t squawk about line endings: ```python sum = ( 1 + 2 + 3 + 4 ) sum ``` ] ] --- # Numbers ### <p style="color:#00449E"> Class Exercises </p> 1. Multiply the number of seconds in a minute (`60`) by the number of minutes in an hour (also `60`) using Python. 2. Assign the result from the previous task (seconds in an hour) to a variable called `seconds_per_hour`. 3. How many seconds are in a day? Use your `seconds_per_hour` variable. 4. Calculate seconds per day again, but this time save the result in a variable called `seconds_per_day` 5. Divide `seconds_per_day` by `seconds_per_hour`. Use floating-point (`/`) division. --- class: inverse, center, middle # Choose with `if` <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --- # Choose with `if` ### <p style="color:#00449E"> Compare with `if`, `elif`, and `else` </p> - Now, we finally take our first step into the *code structures* that weave data into programs. - The following Python program checks the value of the boolean variable `disaster` and prints an appropriate comment: ```python disaster = True if disaster: print("Woe!") else: print("Whee!") ``` --- # Choose with `if` ### <p style="color:#00449E"> Compare with `if`, `elif`, and `else` </p> - The `if` and `else` lines are Python *statements* that check whether a condition is a boolean `True` value, or can be evaluated as `True`. ```python disaster = True if disaster: print("Woe!") else: print("Whee!") ``` --- # Choose with `if` ### <p style="color:#00449E"> Compare with `if`, `elif`, and `else` </p> - Python expects you to be consistent with code within a section—the lines need to be indented the same amount, lined up on the left. - The recommended style is to use *four* spaces. - When changing a line by hitting the key *enter/return*, Spyder takes care of indention pretty well. ```python disaster = True if disaster: print("Woe!") else: print("Whee!") ``` --- # Choose with `if` ### <p style="color:#00449E"> Compare with `if`, `elif`, and `else` </p> - We did a number of things here: ```python disaster = True if disaster: print("Woe!") else: print("Whee!") ``` - Assigned the boolean value `True` to the variable named `disaster`. - Performed a conditional comparison by using `if` and `else`, executing different code depending on the value of `disaster`. - Called the `print()` function to print some text. --- # Choose with `if` ### <p style="color:#00449E"> Compare with `if`, `elif`, and `else` </p> - We can have tests within tests, as many levels deep as needed: ```python furry = True large = True if furry: if large: print("It's a yeti.") else: print("It's a cat!") else: if large: print("It's a whale!") else: print("It's a human. Or a hairless cat.") ``` --- # Choose with `if` ### <p style="color:#00449E"> Compare with `if`, `elif`, and `else` </p> - Indentation determines how the `if` and `else` sections are paired. ```python furry = True large = True if furry: if large: print("It's a yeti.") else: print("It's a cat!") else: if large: print("It's a whale!") else: print("It's a human. Or a hairless cat.") ``` --- # Choose with `if` ### <p style="color:#00449E"> Compare with `if`, `elif`, and `else` </p> If there are more than two possibilities to test, use `if` for the first, `elif` (meaning *else if*) for the middle ones, and `else` for the last: ```python color = "mauve" if color == "red": print("It's a rasberry") elif color == "green": print("It's a green chili") elif color == "bee purple": print("I don't know what it is, but only bees can see it") else: print("I've never heard of the color", color) ``` --- # Choose with `if` ### <p style="color:#00449E"> Comparison Operators </p> - Here are Python's comparison operators: <img src="../lec_figs/int-py-ch4-comparison.png" width="24%" style="display: block; margin: auto;" /> --- # Choose with `if` ### <p style="color:#00449E"> Comparison Operators </p> - Here are Python's comparison operators: ```python # Assign x to 7 x = 7 x == 5 # Test equality x == 7 5 < x x < 10 ``` --- # Choose with `if` ### <p style="color:#00449E"> Comparison Operators </p> - Logical operators have lower *precedence* than the chunks of code that they’re comparing. - This means that the chunks are calculated first, and then compared. ```python 5 < x and x < 10 ``` - The easiest way to avoid confusion about *precedence* is to add parentheses ```python (5 < x) and (x < 10) ``` --- # Choose with `if` ### <p style="color:#00449E"> Comparison Operators </p> - Here are some other tests: ```python 5 < x or x < 10 5 < x and x > 10 5 < x and not x > 10 ``` - If you’re `and`-ing multiple comparisons with one variable, Python lets you do this: ```python 5 < x < 10 # It’s the same as 5 < x and x < 10 ``` --- # Choose with `if` ### <p style="color:#00449E"> What Is `True`? </p> - What does Python consider `True` and `False`? - A `False` value doesn’t necessarily need to explicitly be a boolean `False`. - The followings are all considered `False`: .pull-left[ <img src="../lec_figs/int-py-ch4-false1.png" width="36%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="../lec_figs/int-py-ch4-false2.png" width="36%" style="display: block; margin: auto;" /> ] - Anything else is considered `True`. --- # Choose with `if` ### <p style="color:#00449E"> What Is `True`? </p> - Python programs use these definitions of "truthiness" and "falsiness" to check for empty data structures as well as `False` conditions: ```python some_list = [] if some_list: print("There's something in here") else: print("Hey, it's empty!") ``` --- # Choose with `if` ### <p style="color:#00449E"> Do Multiple Comparisons with `in` </p> - Suppose that you have a letter and want to know whether it’s a vowel. - One way would be to write a long `if` statement: ```python letter = 'o' if letter == 'a' or letter == 'e' or letter == 'i' \ or letter == 'o' or letter == 'u': print(letter, 'is a vowel') else: print(letter, 'is not a vowel') ``` --- # Choose with `if` ### <p style="color:#00449E"> Do Multiple Comparisons with `in` </p> - Whenever you need to make a lot of comparisons like that, separated by or, use Python’s *membership operator* `in`, instead. ```python vowels = 'aeiou' letter = 'o' letter in vowels if letter in vowels: print(letter, 'is a vowel') ``` --- # Choose with `if` ### <p style="color:#00449E"> Do Multiple Comparisons with `in` </p> - Here are some examples of how to use `in` with some data types: .panelset[ .panel[.panel-name[Set] ```python letter = 'o' vowel_set = {'a', 'e', 'i', 'o', 'u'} letter in vowel_set ``` ] .panel[.panel-name[List] ```python vowel_list = ['a', 'e', 'i', 'o', 'u'] letter in vowel_list ``` ] .panel[.panel-name[Tuple] ```python vowel_tuple = ('a', 'e', 'i', 'o', 'u') letter in vowel_tuple ``` ] .panel[.panel-name[Dictionary] ```python vowel_dict = {'a': 'apple', 'e': 'elephant', 'i': 'impala', 'o': 'ocelot', 'u': 'unicorn'} letter in vowel_dict ``` - For the dictionary, `in` looks at the keys (the left-hand side of the `:`) instead of their values. ] .panel[.panel-name[String] ```python vowel_string = "aeiou" letter in vowel_string ``` ] ] --- # Choose with `if` ### <p style="color:#00449E"> Walrus Operator </p> - The *walrus operator* looks like this: <img src="../lec_figs/int-py-ch5-walrus.png" width="24%" style="display: block; margin: auto;" /> .panelset[ .panel[.panel-name[Two-step assignments] ```python tweet_limit = 280 tweet_string = "Blah" * 50 diff = tweet_limit - len(tweet_string) if diff >= 0: print("A fitting tweet") else: print("Went over by", abs(diff)) ``` ] .panel[.panel-name[Walrus] ```python tweet_limit = 280 tweet_string = "Blah" * 50 if ( diff := tweet_limit - len(tweet_string) ) >= 0: print("A fitting tweet") else: print("Went over by", abs(diff)) ``` ] ] --- # Choose with `if` ### <p style="color:#00449E"> Class Exercises </p> 1. Choose a number between 1 and 10 and assign it to the variable `secret`. Then, select another number between 1 and 10 and assign it to the variable `guess`. Next, write the conditional tests (`if`, `else`, and `elif`) to print the string '`too low`' if `guess` is less than `secret`, '`too high`' if greater than `secret`, and '`just right`' if equal to `secret`. 2. Assign `True` or `False` to the variables `small` and `green`. Write some `if`/`else` statements to print which of these matches those choices: cherry, pea, watermelon, pumpkin.