#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 9/13/2022 DS 2000 Lecture 2 - print, input, type casting, round Logistics: - Your first due dates will be on Friday (9/16)! - Quiz 1 (will be released today @ 4:30pm, due Friday @ 9:50 AM) - Homework 1 (due Friday @ 9pm) 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 --- Question: What is the value of the variable "x" at the end of the following lines of code? x = 10 # create the variable x, store the value 10 in it x = 10 * 2 # update the value in the variable x to 20 x = x + 10 # look up the value of x (20), add 10, then # store that new value in x print(x) A. 10 B. 20 C. 30 < ----- D. 40 E. Error """ """ Warm-up --- (Deitel & Deitel 2.3) Question: What does the ** operator do? A. multiply but only for decimal numbers B. multiply by 10 (raise to the power of 10) C. calculate an exponent <----- D. calculate a logarithmic (log) power E. calculate a remainder To explore on your own: // - double divide % - modulus """ # print("this is the first line") # print() # blank line # print("this is the second line") """ review: python "commands" ---- Last lecture: we learned about using the print() command to display information to the user. print() is actually a kind of python *function*. function: a pre-packaged group code that does one job for you. python provides a number of very useful functions. syntax: in python, functions *always* are used by writing the function name and then a set of parentheses () In general, to use a function, you need to know three things: 1. Does this function give me any information back? 2. what is this function's name? 3. what information does this function need to run? For print(): 1. Does this function give me any information back? - no! 2. what is this function's name? - print 3. what information does this function need to run? - thing(s) to print - arguments/parameter Vocabulary: - function - arguments/parameters """ """ Getting fancy with print() ---- (Deitel & Deitel 2.4) To print multiple pieces of information on the same line, we can pass multiple parameters to the print() function. """ # print() examples print("I have a pet cat", "her name is Choux") dogs = 3 print("Number of dogs:", dogs) print("cat", "dog") # pass two arguments to print print("cat" + "dog") # pass one argument """ input() ---- (Deitel & Deitel 2.6) input() is a python *function*. Its job is to get information from the user. Let's answer the three function questions first: 1. Does this function give me any information back? - yes! the user's response to the prompt - return/return value 2. what is this function's name? - input 3. what information does this function need to run? - a string prompt Vocabulary: - return/return value """ # input() examples name = input("what is your name? ") print("Welcome", name) # the type() function tells us what type # of value is stored in a variable print(type(name)) celcius = input("what temp? (c) ") print(celcius) print(type(celcius)) celcius = float(celcius) print(type(celcius)) # error because celcius is a string # if we don't convert it to int or float fahrenheit = ((9 / 5) * celcius) + 32 print("Fahrenheit", fahrenheit) """ Question: What is the data type of celcius after the following lines of code? A. number B. string <---- C. something else """ """ data types ---- all *values* in python have a *data type* string: any sequence of letters "they can go between double quotes" '12367.786 is a number' "Felix's cat is named Choux" int: integer, any whole number (neg or pos) float: decimals, can be neg or pos There are others, but these are the ones that we'll focus on for now! """ """ type conversion (casting) ---- string, int, and float all have special python functions that convert values of a different type into the target type. For example, we have an int() function: 1. Does this function give me any information back? - yes! the target value as the new type 2. what is this function's name? - int (also float and str) 3. what information does this function need to run? - the target value to convert """ # int() examples celcius = "20" print(celcius) print(type(celcius)) num_celcius = int(celcius) print(num_celcius) print(type(num_celcius)) print() print(celcius) print(type(celcius)) # str # common pattern, using str() # as an example x = 57 x = str(x) # float() height = "67.23" height = float(height) """ input() + types --- input ALWAYS returns a string """ """ Example problems from last Friday! """ """ Example problem 1: Write a program that asks the user how old they are, then prints this number out. """ print("age example") age = input("how old are you? ") print("Your age:") print(age) print() """ Example problem 2: Write a program that asks the user how many apples they would like to eat each day, then calculates how many apples they should buy per week. """ print("apples per day example") apples_per_day = int(input("apples per day? ")) # if you don't immediately cast the input to an int #apples_per_day = int(apples_per_day) apples_per_week = apples_per_day * 7 print("you should buy:") print(apples_per_week) """ round() --- A python function that rounds decimal numbers 1. Does this function give me any information back? - yes, the rounded version of the provided target number 2. what is this function's name? - round 3. what information does this function need to run? - a target number - a number of decimal places to round to (default is 0) """ """ Summary (very brief) --- Functions to be familiar with: - print() - input() - int() - float() - str() - round() We also learned some more vocabulary: - function - parameter/argument - return/return value """