Lecture 03 Input
Joseph Haugh
University of New Mexico
Free Recall
- Get out a sheet of paper or open a text editor
- For 2 minutes write down whatever comes to mind about the last class
- This could be topics you learned
- Questions you had
- Connections you made
Setting Up CS Circles
- Sign up for a free account on CS Circles
- This is mandatory
- Next set
jhaugh
as your “guru”
- Click “Edit my profile” in the top right menu
- Set the guru field to
jhaugh
- I will now be able to see your progress and give you credit in Canvas
Chapter 1: Variables
- Complete all exercises in chapter 1
- I will give you around 15-20 minutes for this
Different Types For Different Situations
- Data comes in many different shapes and sizes
- To try to categorize these many languages employ the use of types
- Types tell us useful information about what is held inside a variable
- So far we have encountered two different types:
- Integers:
1
- Strings:
"hello"
- Another which we will often see is floating-point number often abbreviated as float
- Floats are numbers with a decimal point, e.g.
3.14
Inspecting Types
You can ask Python what the type of a variable is by using the type() function:
x = 3
y = 3.14
z = "hello"
print(type(x))
print(type(y))
print(type(z))
Visualize
Outputs:
<class 'int'>
<class 'float'>
<class 'str'>
Explaining Types
- Types are useful for both the programmer and the computer
- The programmer can easily get a sense of what is held in a variable
- The computer can make sure that the types of variables match when necessary
- For now we will focus on 3 types:
- Integers: Whole numbers
- Floats: Numbers with a decimal point
- Strings: Sequence of characters
Test Your Understanding
What type will each variable in the following code have?
x = 3.0
y = 4.256
z = ""
print(type(x))
print(type(y))
print(type(z))
Visualize
x: Float
y: Float
z: String
Converting Between Types
- Many times you will be given data of one type but you need it to be another type
- This often happens between strings and other types
- Luckily, Python makes this fairly easy
- There is a function for each type we have discussed so far to convert to that type
- These functions are:
- int(): Converts from another type to integer
- float(): Converts from another type to float
- str(): Converts from another type to string
Int() Function
Let’s try out the int() function:
x = "33"
y = 3.14
z = 3.65
a = "hello"
print(int(x))
print(int(y))
print(int(z))
print(int(a))
Visualize
Float() Function
Next the float() function:
x = "33"
y = 3
z = "3.14"
a = "hello"
print(float(x))
print(float(y))
print(float(z))
print(float(a))
Visualize
Str() Function
Finally, the str() function:
x = 3
y = 3.14
z = "hello"
a = 'world'
print(str(x))
print(str(y))
print(str(z))
print(str(a))
Visualize
First Step To Useful Programs
So far all are programs have been static
For example:
c = 10
f = (c * (9/5)) + 32
print(f)
Visualize
The program above converts celsius to fahrenheit
Pretty useful but what if we want to convert a number other than 10
?
Right now we would have to change the program!
There is a better way…
Exercise: Sourdough Starter Ratios
- Sourdough starters are made up 3 ingredients:
- Starter from before
- Flour
- Water
- When “feeding” you need to follow a prescribed ratio of starter to flour to water
- This is usually given something like “1:5:5”
- This means 1 part starter, 5 parts flour, 5 parts water
- For example say I had 40 grams of starter
- With the ratio above I would then need to use 200 grams flour and 200 grams water
Exercise: Sourdough Starter Ratios
- Your task is to create a program which asks for 3 inputs
- These inputs are all given as ints
- How much starter? (the 40 from before)
- What is the flour ratio? (the 5 from before)
- What is the water ratio? (the other 5 from before)
- Your program then needs to print how much flour and water are needed
- Note: You need to use the int() and input() functions to gather data
- For example to gather how much starter the user wants to use you might do the following:
starter = int(input())