IT 116: Introduction to Scripting
Class 4
General Advice
Course Work
Tips and Examples
Review
New Material
Microphone
Questions? Issues?
Does anybody have any questions before I begin?
Homework 2
I have posted homework 2
here.
It is due this coming Sunday at 11:59 PM.
Today's Class
In today's class I will talk more about important concepts
in writing Python programs.
I will also talk more about variables and data types.
Then I will show you how to input data into a Python script.
Make a Study Schedule
- One of the most important thing you can do to do better in class ...
- is to make a study schedule ...
- and keep to it
- A consistant study routine is the single most important thing
you can do ...
- to improve your grade
- You should set aside regular chunks of time for this course ...
- and stick to it as best you can
- The semester is long - 14 weeks
- Most of you have many responsibilites
- Conflicting demands on your time can cause you to lose focus
- Having a regular schedule can keep you on track ...
- and prevent panic attacks before exams
- Time is a finite resource
- Once you have wasted an hour ...
- you can never get it back
- If you find your schedule is not working ...
- make some changes
- But do not use that as an excuse to abandon a schedule
- There is an old saying
- If you fail to plan ...
- you are planning to fail
Course Work
First Graded Quiz
- The first graded quiz will be given on Tuesday next week
- The question on the quiz are taken from the ungraded quizzes ...
- of the previous week
- You should expect a Graded Quiz each week on Tuesday
- If you do not take the quiz when it is offered ...
- you cannot take it at another time
- If you miss a quiz but have an excuse, send me an email
Gradescope
- In order to receive your scores in Gradescope ...
- you need to sign in to my Gradescope section
- You do not have to register with Gradescope
- In the next few days I will enter all your UMB addresses
into Gradescope
- You will then receive an email from Gradescope
- What happens next depends on whether you are already
registered with Gradescope
- If you are new to Gradescope
- Click the "Set your password" link
- Confirm your password
- Click Save
- You will then be taken to the page for your course
- If you are already registered with Gradescope
- Click the link to the course
- At the login screen enter your UMB email
address and your Gradescope password
- If you have forgotten you password, click the "Forgot
your password"link
- You will find more information about Gradescope
here.
Class Quizzes
- From now on, each class will have a Class Quiz
- The Class Quiz is ungraded
- It is designed to help you remember the material covered in class
- Here is how it works
- Go to the listing of Class Quizzes you will find
here
- Click on the link for the day's quiz
- Get a blank piece of paper
- Write the answers to the questions on the paper
- Go to the listing of Class Quiz Answers you will find
here
- Check your answers
- Do not give me the paper with your answers
- Class quizzes are not graded
- They are designed to help you learn the material covered in class
Homework and it116 Directories
- When you first log in, you will be in your home directory
- This directory belongs to your account ...
- and no other account can make changes to it
- The name of this directory is your Unix account name
- If you registered for this class, you should see a
it116 entry here
- It looks like a directory ...
- but it is really something called a link
- If you use
cd to enter it116 ...
- you will be inside your IT 116 class directory
- In Class Exercise 2 your were told to create two directories
- An hw directory for homework scripts
- And an ex for Class Exercises
- For each new assignment you must create a new directory inside
either hw pathname ex
- So today you will create an ex3 directory
inside your ex directory
- The files you create for this exercise should be created in
this directory
Tips and Examples
Creating Directories with FileZilla
- You need to create a new directory for each Class Exercise and
homework assignment
- You can do this in FileZilla
- First connect to pe15.cs.umb.edu
- Then drill down to either the ex
or hw directory
- Double click on the directory
- Right click on the blank area of the bottom panel
- This will bring up a menu
- Select "Create directory"
- You can also move files and directories in FileZilla by clicking
and dragging
Keeping Your Scripts Organized
- Organization is an important part of IT work
- You should keep all the scripts for this class in one directory
- Go to your Documents directory and create a
python directory
- Then create all your Python scripts in this directory
- This will make it easier to find them in FileZilla ...
- when you need to move them to pe15.cs.umb.edu
Using FileZilla to Fix Problems
- You may receive an email from me saying I cannot find an assignment
- If you know you created it, it's probably in the wrong directory ...
- or the file has the wrong name
- Either way, you can use FileZilla to fix it
- If a file is in the wrong place, click and drag it to the right directory
- If the file has the wrong name right click on it to bring up a menu ...
- and select Rename
Use Flash Cards to Prepare for Graded Quizzes
- To studying for the graded quizzes you need to study the Class Quizzes for the
previous week
- The best way to do this is to use flash cards
- You take an index card and write the question on one side ...
- and the answer on the other
- You should make a set of flash cards for each Class Quiz
- Label them with the Class Number
- And put a rubber band around them
- I did this when taking courses for my Masters degree
- I would go through them on my way to class on the T
- There are programs that you can use to create virtual flash cards
- But it is better if you write your own cards
- The simple act of writing each card, helps you remember the material
- You can also use flash cards in reverse
- Turn them over, read the answer and guess the question
- This can be helpful when studying for the midterm or final
Recorded Class Meetings
- All lectures for this class are recorded
- Use this resource if you miss a class ...
- or don't quite understand something mentioned in class
- You will find links to the recordings of class meetings
here
Review
Statements
Functions
- The progrm hello.py consists of a single
line
print("Hello world!")
print is a
function
- A function is a group of Python statements ...
- that has been given a name and does something usefull
- It's a package of statements
- When you want to use a function you use a
function call
- The function call has two parts
- The function name
- Values inside parentheses ( )
- The values are called
arguments
- A function call always has parentheses ...
- even if there is nothing inside them
Data Types
Strings
Characters
- Everything in a computer's memory is a
binary number
- Binary number are written in powers of 2
- And appear using the digits 1 and
0
- To represent a character in the computer's memory, we need a number
- We get that number from a table
- The table contains a unique number for each character
Unicode
- The table of numbers and characters that we use in Python is called Unicode
- Unicode represents all the characters in most world writing systems
- There are many ways to represent the numbers that make up Unicode
- These representations are called encodings
- Python uses the
UTF-8
encoding
Variables
- In programming we often need to store a value that we will use later
- To do this, we create a
variable
- A variable is a location in the computer's memory that has a name ..
- and holds a single value
- You create a variable with an
assignment statement
...
- which has the following format
LOOP_VARIABLE = VALUE
- Whenever a word appears in ALL CAPITALS ...
- it is a
placeholder
- In other words, it stands for a value that you have to supply
- If I wanted to create the variable name and give it
my name as a value I would write
name = "Glenn Hoffman"
Attendance
New Material
Literals
- Programs often calculate values
- But some values don't need to be calculated
- We know that there are 7 days in a week ...
- and 24 hours in a day
- When we write programs that need specific values ...
- these values are written directly inside the script
- A value that is written directly into the code is called a
literal
- In the
print statement above, "Hello world!"
is a literal
- When I write
print(5)
5 is a literal
Expressions
Variable Naming Rules
Choosing Good Variable Names
Printing Multiple Values
- The
print function can accept more than one argument
>>> room_1 = 92
>>> print("My room number is", room_1)
My room number is 92
- Notice that we used both a string literal ...
- and a variable separated by a comma
- Also notice that Python automatically put a space between the string ...
- and the value of the variable
- There is no limit to the number of arguments
print will accept
>>> room_2 = 93
>>> print("My room number is", room_1, "and my friend is staying in", room_2)
My room number is 92 and my friend is staying in 93
- You can also give
print no arguments
- In that case it will print a blank line
$ cat print_01.py
# Prints three lines, one blank
print("Line 1")
print()
print("Line 2")
$ python3 print_01.py
Line 1
Line 2
Numeric Data Types and Literals
- A Python variable can hold many different types of values
- Strings
- Integers
- Decimals
- Each type of value is stored in the computer's memory in a different format
- So the binary number that represent the string "5" ...
- is different from the binary number that represent the integer 5 ...
- and the one that represent the decimal value 5.0
- The technical term for each of these different representations is a
data type
- Any Python variable can hold any kind of value
- 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 you assign a value to a variable
- Python can detect what data type it should use
- You can see this using the Python function
type
>>> type("5")
<class 'str'>
>>> type(5)
<class 'int'>
>>> type(5.0)
<class 'float'>
- The
type function also works on variables
>>> score = 85
>>> type(score)
<class 'int'>
>>> score = 85.0
>>> type(score)
<class 'float'>
>>> score = "85"
>>> type(score)
<class 'str'>
- Notice that Python thinks that these three values ...
- all have different data types
- Even though we would think of them as all the same value
Why You Need to Know About Data Types
- It might seem that data types are an unneeded complication
- After all, you can give the
print function any kind of
value you want ...
- and it will print the correct output
>>> value = "5"
>>> type(value)
<type 'str'>
>>> print(value)
5
>>> value = 5
>>> type(value)
<type 'int'>
>>> print(value)
5
>>> value = 5.0
>>> type(value)
<type 'float'>
>>> print(value)
5.0
- But there are many situations where the data type is important
- The data type of a value determines what you can do with it
- You can divide an
int or a float with another int or a float
>>> 30 / 5
6
>>> 30.0 / 5.0
6.0
- But you can't divide strings
>>> "30" / "5"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'str'
- Or divide a string by an integer
>>> "6" / 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'int'
- Or add an integer to a string
>>> 5 + "6"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
- Or subtrack them
>>> 5 - "6"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'int' and 'str'
Conversion Functions
- Python has functions that will convert from one data type to another
int() will try to convert a value into an integer
>>> int("5")
5
int() will also work with decimals
>>> int(5.1)
5
- But there is a limit to what it can do
>>> int("five")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'five')
- To convert integers or decimals into a string use the
str() conversion function
>>> str(5)
'5'
>>> str(5.35)
'5.35'
- There are conversion functions for all major data types
- A function gets the values it needs to do its job from its arguments
- But how does a Python script get values from the user?
- One way is to read input from the keyboard
- This is done with the
input function
input takes one argument
- That argument is the text that will
prompt
the user for a value
- This prompt tells the user what information is needed
input is used in an
assignment statement like this
LOOP_VARIABLE = input(PROMPT)
- When the Python interpreter comes upon a statement using
input it
- Prints the prompt
- Waits for the user to type something and hit Enter
- Assigns the text entered by the user to the variable
- The prompt string should make it clear what type of value is wanted
- Here is an example
>>> team = input("Enter a team name: ")
Enter a team name: Red Sox
>>> print("Go", team)
Go Red Sox
- The text in blue is entered by the user
- Notice that there is a space at the end of the prompt string
- There should be a gap between the prompt and the value entered by the user
- The
input function gets input from the user ...
- and turns it into a value that a Python program can use
- The value produced by
input function is always a string
>>> number = input("Please enter a number: ")
Please enter a number: 55
>>> type(number)
<class 'str'>
- The text in blue is entered by the user
- Remember, every value has a data type
- If we want the value we get from
input to be a different
data type ...
- we have to use a conversion function
- To convert the value in number to an integer use
int
>>> number = int(number)
>>> type(number)
<class 'int'>
- To convert it to a decimal, we can use
float
>>> number = float(number)
>>> type(number)
<class 'float'>
- If you run a conversion function on a value that it can't handle ...
- you will get an error
>>> number = input("Please enter a number: ")
Please enter a number: fifty-five
>>> number = int(number)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'fifty-five'
Class Exercise
Class Quiz