IT 116: Introduction to Scripting
Class 5
General Advice
Course Work
Tips and Examples
Review
New Material
Microphone
Questions
Are there any questions before I begin?
Homework 3
I have posted homework 3 here.
It is due this coming Sunday at 11:59 PM.
Today's Class
Today I'll talk about operators and how to
use different data types in calculations
General Advice
Learn to Read Carefully
- Most of us spend a large part of each day reading
- Most of this reading is done quickly ...
- because the material is familiar ...
- and the details are not that important
- Most of the time we read, we know what to expect
- For example, if you get text from a friend ...
- you don't linger over each word
- You have some idea of what your friend is going to say ...
- so you can read it quickly
- The same thing happens if you read the about your favorite singer
- The details aren't important ...
- just the overall picture
- The reading you must do for this course ...
- is fundamentally different
- Technical work is detailed work
- If you read technical material too quickly ...
- you will miss important details
- You must learn to read technical material slowly and carefully
- You will sometime have to read something a second or third time ...
- until you fully understand it
- Learning how to read technical material ...
- may be the most important thing you take away from this class
Course Work
Graded Quiz Today
- After I finish my lecture today I will hand out papers for today's
graded quiz
- Write your name clearly at the top
- When you finish the Quiz hand it to me
- Then you can work on the Class Exercise
Late Penalty
- You will lose 2 points for every day that your assignment is late
- Every Unix file has a time stamp that changes every time the file is modified
- My scripts look at this date stamp to determine whether a submission is late
- If your assignment is not working by the due date ...
- you can continue to work on it ...
- but you will lose a few points
- If your assignment is working, do not go back and change it
- Any changes you make will change the date stamp ...
- which will cost you points
Do Not Email Me about Missing Assignments
- If you get an email from me saying an assignment is missing
- do not email me about this
- I get far too many emails
- Instead of sending me an email, fix the problem
- If you do not know how to fix it
- Come to Zoom Office Hours
- Post a question of Piazza
- Contact the Class Assistant
- I collect homework assignments and check Class Exercises several times during the week
- I will check or collect your assignment later in the week.
Importance of Exams
- Many people find exams difficult
- But the best way for me to know if you have really learned
the material is by giving exams
- It is too easy to cheat on Class Exercises and homework assignments
- This is why the two exams account for 50% of your grade
- Too many students do well on the graded quizzes, Class Exercises
and homework assignment ...
- only to do poorly on the exams
- This lowers their grade considerably
- The purpose of the Class Exercises and homework assignments
is to give you hands on experience with the material
- And to better prepare you for the exams
Preparing for Exams
- Both the midterm and final exams follow the same format
- 60% of the points on the exams come from questions taken
from the ungraded class quizzes
- The remaining 40% of the points come from 4 code questions
- These questions ask you to write short segments of Python
code
- Two things will help you prepare for exams
- Flash cards
- Class Exercises and homework assignments
- Flash cards should be made for all questions from Ungraded
Class Quizzes
- You should review them frequently throughout the semester
- Pay attention when you are working on the Class Exercises and
homeworks
- Work on these assignments is teaching you how to write Python code
- This will come in handy for answering the code questions on
the midterm and final
Tips and Examples
Do Not Fall Behind in This Course
- Technical courses require that you master a great deal of detail
- You will need to study for this course several times during the week
- Studying once a week is usually not enough
- The key to success in a technical course is steady, consistant, work
- It should be spread out over several days
- Some important concepts do not sink in immediately
- It may take several days for something new to become clear
- Material in this course often depends on what came before
- So it is critical that you not fall behind
- If you fall behind, it can be VERY hard to catch up
Read the Homework Assignments Carefully
- Homework assignments may ask you to do many things
- So you may need to read the assignment more than once
- A single reading might not be enough to understand what I want you to do
- Take the time to read these documents slowly
- This will help you understand all the details
- If you don't, you will make mistakes and lose points
- Technical work is detailed work
- So you must learn to read documents with a lot of detail
- If you do not understand something post a question on Piazza
- NEVER GUESS at the meaning of something I have written
Representation of Data
- Python has different
data types
for different kinds of values
- Each data type has a different way of storing its value in memory
- The string "5" appears in memory as the binary number 110101 ...
- which is 53 in decimal ...
- and is the number associated with the Unicode symbol "5"
- The integer 5 appears as the binary number 101
- The data type of a value determins what you can do it with
- You can use an integer in arithmetic
- But you can't do that with a string
Review
Expressions
Variable Naming Rules
Choosing Good Variable Names
Numeric Data Types and Literals
- A Python variable can hold many different types of values
- Strings
- Integers
- Decimals
- Each of these value types is stored in the computer's memory ...
- using a different binary pattern
- The technical term for each of these different representations is a
data type
- When a string is stored in memory, its data type is
str
- When an integer is stored in memory, it has the data type
int
- When a decimal is stored, it has data type
float
- Float is short for
floating point
- Whenever we write a value directly into a Python statement ...
- that value is called a
literal
- The data type of a value determines what you can do with it
- You can add, subtract, multiply or divide integers and floats
- But you cannot divide or subtract strings
Conversion Functions
- Python has functions that convert from one data type to another
- Use
int() to convert a value into an integer
>>> type('5')
<class 'str'>
>>> int('5')
5
>>> type(5)
<class 'int'>
- Use
str() to convert a value into a string
>>> type(5)
<class 'int'>
>>> str(5)
'5'
>>> type('5')
<class 'str'>
- Use
float() to convert a value into a float
>>> type('5.3')
<class 'str'>
>>> float('5.3')
5.3
>>> type(5.3)
<class 'float'>
- There are some values that cannot be converted
>>> int("five")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'five')
- A Python script can get a value from the user using the
input
function
input has one argument
- That argument is text that will prompt
the user for input
input is used in an assignment statement like this
LOOP_VARIABLE = input(PROMPT)
- When the interpreter gets to an assignment statement using
input it
- Prints the prompt
- Waits for the user to enter text and hit Enter
- Stores the text entered at the command line into
the variable
- Here is an example
>>> team = input("Please enter a team name: ")
Please enter a team name: Red Sox
>>> print("Go", team)
Go Red Sox
The text in blue was entered by the user
- Notice that I put a space at the end of the prompt string
- It's good to have a gap between the prompt and the value entered by the user
Attendance
New Material
Operators
New Arithmetic Operators
- The first 4 operators
should be familiar to you
- Though mulitplication uses * instead of × ...
- (the symbol × is not the same as the letter x
- And division uses / in place of ÷)
- Because you won't find × or ÷
on the keyboard
- But you may not have come across the last three operators
- It turns out we need all 7 operatos ...
- to do arithmetic in Python
Normal Division
Integer Division
- Remember long division from arithmatic class?
- To divide 17 by 3 you would write
- Where 5 was the quotient ...
- and 2 was the remainder
- The // operator gives us the quotient ...
- which is the integer part of the answer
- So // is integer division
>>> 17 // 3
5
Remainder Operator
Exponent Operator
- When we raise a number to a power we multiply it by itself ...
- a certain number of times
- When we multiply a number by itself ...
- we get the square of a number
>>> 2 * 2
4
>>> 3 * 3
9
>>>
- When we multiply a number by itself and then itself again ...
- we get the cube of a number
>>> 2 * 2 * 2
8
>>> 3 * 3 * 3
27
>>> 4 * 4 * 4
64
- ** is the exponent operator
- The number before this operator is raised to the power of the second number
- To get the square of a number we raise it to the 2nd power
>>> 2 ** 2
4
>>> 3 ** 2
9
>>> 4 ** 2
16
- To get the cube of a number we raise it to the 3rd power
>>> 2 ** 3
8
>>> 3 ** 3
27
>>> 4 ** 3
64
Operator Precedence
Grouping with Parentheses
- But what if we wanted to evalaute the operators ...
- in the order in which they were written?
- What if we wanted to first add 4 and 3
- Then multiply the result by 5
- Then square that result?
- We can use operators in an order different from the order of precedence
- We do this using parentheses, ( )
- To first add 2 + 3 then multiply by 5 we write
(4 + 3) * 5 = 35
- Parentheses are not operators
- But they override the normal order of precedence
- Calculations inside parentheses must be done first ...
- before using operators outside the parentheses
- I'm told that nowadays arithmetic teachers no longer use "My Dear Aunt Sally"
- Instead they tell their student to remember PEMDAS
P - Parentheses
E - Exponent (raising to a power)
M - Multiplication
D - Division
A - Addition
S - Subtractions
- This mnemonic (memory aid) is useful because it includes exponentiation
- But parentheses are not operators ...
- because they perform no action
Expressions inside Expressions
Converting Algebra Into Python
- You may have noticed that when we write the Python expression
a + b
it looks like something you saw in algebra
- But there are important difference between Python and algebra
- In algebra variable names are always a single letter
- It is legal to have a Python variable whose name is a single letter
- But it is not good practice
- Variable names should say something about the value they hold
- There are other differences
- In algebra when we write
- We mean that the value of a is multiplied by the
value of b ...
- and that result is in turn is multiplied by the value of
c
- In Python we would write this as
a * b * c
- Similarly, the algebraic expression
- would be written like this in Python
12 * 4
- The following algebraic formula
- Would look like this in Python
y = 3 * x / 2
- Similarly the formula
- in Python would be
a = (x + 12) / (13 * b)
- Notice that we had to put both the numerator
x + 12
- and the denominator
13 * b
- inside parentheses
- We have to calculate the value of each of these expressions before dividing
them
- This is were the rules of precedence swing into action
- The financial formula
- Would become the following in Python
P = F / (1 + r) ** n
Class Exercise
Class Quiz