#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 9/20/2022 DS 2000 Lecture 4 - boolean, conditionals, intro to data viz Logistics: - Quiz 2 goes out today @ 4:30pm, due Fri @ 9:50am - Homework 2 due Fri @ 9pm - we updated the write-up yesterday evening to include some more hints about problem 1 - think about applying to be a HASTAC scholar if you want to explore digital humanities more (https://www.hastac.org/initiatives/hastac-scholars) - remote attendance (https://bit.ly/remote-ds2000-muzny) Three ways to participate (please do one of these!) 1) via the PollEverywhere website: https://pollev.com/muzny 2) via text: text "muzny" to the number 22333 to join the session 3) via Poll Everywhere app (available for iOS or Android) Warm-up 0 - HW 2 status --- Where are you on HW 2 currently? A. haven't looked at it B. looked at the write up C. started coding D. finished a few problems E. finished Warm-up 1 - opening files to read from in python --- What is the first step to opening a file to read from in python? A. put the file and the python program in the same folder -> "zeroeth" step, must be done before we start anything else B. open the file -> first programmatic step infile = open(filename, "r") C. call the file.readline() function line = infile.readline() # first line line = infile.readline() # second line D. close the file -> last step (programmatically) infile.close() E. something else Warm-up 2 - opening files to read from in python --- What is the output of the following code, assuming that the file was opened successfully? # open the connection to the file animal_file = open("animals.txt", "r") # reads the first line of the file # we didn't save it in a variable so we can't # access it animal_file.readline() # reads the second line of the file and returns it # to the print function to be displayed print(animal_file.readline()) # close the file animal_file.close() A. prints the first line of the file B. prints the second line of the file <---- C. prints the first and the second lines of the file D. prints no lines of the file E. Error """ """ review: program structure --- - main(): the central function that we look at to know what our program is going to do For now, this is the only (you-defined) function that you'll have. def main(): # code must be indented # don't forget the function call main() """ """ review: built-in functions we know so far --- these are provided with python print(things that we want displayed) answer = input(prompt) number = int(string version of an int) number = float(string version of a float) word = str(int/float/other version of a value) type_info = type(value) infile = open(filename, mode) line = infile.readline() infile.close() rounded_num = round(original, places) + max(), min() max_value = max(things we want the max of) What happens if you do min(numbers as strings) """ # # program flow/execution # # first line # | # | # | # v # # last line # # no options/choice about what is executed # # we'd like to be able to have our flow look # # more like: # # # # first line # | \ # | \ # (min) (calculate a max) # | / # | / # # next line # # last line # # first line # | \ # | \ # | (calculate a max) # | / # | / # # next line # # last line """ Making decisions ----- (Deitel & Deitel 3.4/3.5) There are some key super powers that we need to write programs with lots of abilities. 1) Memory (variables) 2) Re-using code (functions) 3) Branching (conditionals) <---- today~! 4) Repetition (on Friday, loops) """ """ Boolean data type --- A new data type! Like string, int, float, except this data type is extra special! Values: True, False Casting function: bool(value to convert) """ # examples of variables w/ boolean values # important that it is True and not true likes_rain = True print("Do you like rain:", likes_rain) print("type info", type(likes_rain)) # anything not a 0 is True print(bool(-12367)) print(bool(0)) print() """ Boolean expressions --- Much like we can have arithmetic expressions (3 + 7 ** 2) evaluates to a numerical answer, we can have boolean expressions. Expressions that evaluate to a boolean answer. comparison operators: <, <=, >, >=, == """ # examples of boolean expressions print("boolean expressions") print(3 < 7) print(5 >= 7) print(7 >= 7) print(7 == 7) # equal print(7 != 7) # not equal """ Logical operators --- Comparison operators combine two values of type: anything with <, >, etc defined result in: boolean value Logical operators combine two values of type: boolean result in: boolean """ print() # examples of logical operators felix_likes_rain = True ds2000_sec2_likes_rain = False # and -> True if both values are True print(felix_likes_rain and ds2000_sec2_likes_rain) # or -> True if either value is True print(felix_likes_rain or ds2000_sec2_likes_rain) # not (reverses a boolean value) print(not felix_likes_rain) print() """ Conditionals 1 --- Conditionals -> put branches in our code Syntax: if boolean expression: # code to run if the boolean expression is True # code to run after the conditional """ # examples print("conditional 1") age = int(input("Age? ")) if age >= 21: print("You can come into this bar") print("Our bar is called ds 2000 bar") print("it is exciting") print("program end") """ Conditionals 2 --- Syntax: if boolean expression: # code to run if the boolean expression is True else: # code to """ # examples age = int(input("Age? ")) if age >= 21: print("You can come into this bar") print("Our bar is called ds 2000 bar") print("it is exciting") else: print("We will go to the ice cream palace") print("it is delicious") print("program end") """ Updating zoo.py ---- From last lecture: Write a program, zoo.py, that represents a zoo that we're starting. First, this program should greet the user (by name, of course), then tell them what animals we have in our zoo. Update: Now, we'll ask the user whether they are interested in visiting the reptile house or the aviary, then tell them the animals in the appropriate place based on their answers. (We'll have two data files—aviary.txt and reptile.txt) """ """ Introduction to data viz --- We'll use the library matplotlib.pyplot to create graphs in this class Basics: 1) import the matplotlib library 2) use plt.plot(x coordinate, y coordinate) to plot a point 3) when you're all done, call plt.show() """ # imports the library (an extra set of tools) # give it the nickname "plt" import matplotlib.pyplot as plt # parameters: x coordinate, y coordinate, marker type plt.plot(0, 1, "o") # circle plt.plot(1, 2, ".") # point plt.plot(2, 5, "s") # square # show the plot plt.show() # We'll start here on Friday! """ Example problem: --- Write a program, distance.py, that reads the data from the two files days.txt and distances.txt and graphs them accordingly on a scatter plot. """ """ Example problem, updated: --- Write a program, distance.py, that reads the data from the *three* files: days.txt, distances.txt, and modes.txt graphs them accordingly on a scatter plot. Color days that were a run with one color and days that were a bike ride a different color. """ # if we have time """ random module ---- """