IT 116: Introduction to Scripting
Class 3
General Advice
Course Work
Tips and Examples
Review
New Material
Microphone
Trouble? Questions?
Has anyone had any trouble doing something for this class
or have any questions?
Readings
If you have the textbook you should read Chapter 2,
Input, Processing and Output,
from the textbook, Starting Out with Python.
Homework 2
I have posted homework 2
here.
It is due this coming Sunday at 11:59 PM.
Let's take a look at it.
Today's Class
Today I will talk about the basic components of the Python language.
And how they are used to write programs
General Advice
Do You Have Enough Time to Do the Work for This Course?
- Many of you work, either part or full time
- This cuts down on the time you have to study
- Technical courses are filled with details where accuracy is very important
- They also introduce concepts which may take some time to fully understand
- Such courses require a significant amount of study time ...
- to learn the material and do well in class
- This studying should not be done on one day ...
- but spread out over the days of the week
- You should not be taking this course ...
- if you can't study several hours a week for it
- Too many students rush to finish a degree ...
- without doing the work to really learn the material
- These people are setting themselves up for professional failure
- If you try to do too much ...
- you may end up accomplishing too little
- Today is the last day of Add/Drop ...
- when dropping the class won't mean losing money
- Be honest with yourself
- Looking at all your other commitments ...
- do you have enough time for the work I will demand of you?
Course Work
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
First Graded Quiz
- The first Graded Quiz will be given on Tuesday next week
- I will pass out the exam papers after my lecture
- Questions on the Graded Quiz come from the Class Quizes
- Usually the quizzes from the previous week
- Unless I tell you otherwise ...
- you should expect a Graded Quiz each week on Tuesday
- You will be able to see your scores on Gradescope
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
Some Skills Only Come With Work
- This course requires you to learn and understand a number
of concepts
- You will find a list of them
here
- You must know them on a deep and fundamental level ...
- so that you can use them to accomplish a task
- It is not enough to memorize a definition ...
- though that is a first step
- Each of these concepts is a tool
- And the best way to learn a tool is to use it
- That is why there are so many assignments in this class
- You will be writing small programs for this course
- When you write them, think of the concepts you are learning
- And what other things you can do with them
- You might want to play around with a concept
- Write a little something of your own ...
- to better see how they work
- Understanding comes with effort
- The more you work with a concept ...
- the more you can do with it
Class Exercises
Getting Help
- This course makes you do things that you may not have done before
- Whenever you do something new ...
- there is a good chance you will make a mistake
- When this happens, the first thing you should do is reread the instructions
- Mistakes often happen when we read too fast
- If you cannot fix the problem in a reasonable amount of time ...
- ask for help
- There are a number of ways you can do this
- Post a question in the class discussion area
- Email the Class Assistant
- Ask other students in the class
- Come to Zoom Office Hours
- DO NOT EMAIL ME with your question
- I have close to 100 student each semester
- With that workload I must be very careful how I spend my time
- I would rather respond to your question on Piazza than in an email
- Only one student will see an email response
- But an answer on Piazza can be seen by the entire class
- Only send me an email when you have an issue that affects you and you alone
- Like being out of the country due to a family emergency
Describing a Problem
Don't Use Screenshots in Piazza
- Many students paste screenshots of code into Piazza
- This is not a good idea
- I have a hard time reading some of the screen shots
- And I can't copy the code into the Python interpreter to test it
- Instead, copy the code to your class directory on
pe15.cs.umb.edu ...
- and type the name of your file into your Piazza entry
- The only time you paste a screenshot into Piazza ...
- is when you are having problems with FileZilla
Login Failure
- If you enter the wrong password when connecting to pe15 ...
- you will be blocked from logging in for 10 minutes
- Wait 10 minutes and try again
- If you have forgotten or misremembered your password, you can change it
- Go to
https://portal.cs.umb.edu/password/
- If you have trouble with your account send an email to operator@cs.umb.edu
Review
IDLE Has Two Different Parts
- IDLE is a single program ...
- that has two different components
- The first part allows you to write a single Python statement ...
- hit Enter or Return ...
- and have the Python interpreter run that statement
- This happens in the first window you see when you open IDLE
- In this window you are talking directly to the Python interpreter
- The second part is the editor
- In this window you write your Python programs ...
- and save them to a directory on your computer
- After you have saved them ...
- you run them in the interpreter window ...
- to see if they work properly
Attendance
New Material
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 useful
- I like to think of a function as package of Python statements
- You will be using a number of function in this class
- And you will learn to write your own
- When you want to ask a function to do something ....
- you use a particular kind of statement ....
- called a
function call
- The statement 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
ASCII
- To use strings on a computer ...
- we need a table of all the characters ...
- and a number that represents the charcter
- In the early days of computing the most frequently used table was ASCII
- ASCII stands for American Standard Code for Information Interchange
- You will find the table for ASCII here
- ASCII was developed in the United States for use with teletype machines ...
- which were used to send written words over telegraph wires
- ASCII had 128 entries which stood for 95 characters ...
- and 33 control codes
- Each character can be stored in 7
bits
of memory
- A bit is a binary digit
- Lowercase and uppercase letters have different numeric values in ASCII
>>> ord("a")
97
>>> ord("A")
65
>>>
- The digits 0 to 9 are also characters
- So each of them is included in the ASCII table
>>> ord("1")
49
>>> ord("2")
50
>>> ord("3")
51
- ASCII also includes a number of symbols
>>> ord("#")
35
>>> ord("$")
36
>>> ord("%")
37
>>> ord("&")
38
Unicode
- ASCII was good enough for English
- But lacks characters used in many European languages ...
- let alone Asian and African languages
- Nowadays we use a new character table called Unicode
- Unicode represents all the characters in most world writing systems
- The first 128 Unicode characters are the ASCII characters
- So ASCII is included in Unicode
- The latest version of Unicode contains 128,237 characters
- They cover 135 modern and ancient writing system
- Most of the major world writing systems are contained in Unicode
- But Chinese characters pose a challenge
- Altogether there are over 50,000 Chinese characters
- Though a good modern dictionary will rarely list over 20,000
- An educated Chinese person will know about 8,000 characters
- But you only need to know about 2-3,000 to read a newspaper
- Unicode currently has 74,605 CJK characters which are used in writing
- Chinese
- Japanese Kanji
- Korean Hanja
- Vietnamese Chu Nom
- A writing system is not the same thing as a language
- Cuneiform was a writing system developed in the Near East around
3,200 BCE
- It was used for writing on clay tablets
- The following languages were written in cuneiform
- Sumerian
- Akkadian
- Eblaite
- Elamite
- Hattic
- Hittite
- Hurrian
- Luwian
- Urartian
- Old Persian
- Unicode includes the Cueiform characters
Representing Unicode Values
- There are so many Unicode characters that you can't represent all of
them in 7 bits ...
- as you can with ASCI
- Some characters require 17 bits
- But if you used 17 bits to represent every character ...
- text documents would be extremely large
- So the numbers that represent Unicode characters can be written
in different ways
- Each of these schemes is called an encoding
- The most commonly used Unicode encoding is UTF-8
- This is the encoding Python uses by default
- UTF-8 uses a variable number of bytes to represent each Unicode character
- Either 1, 2, 3 or 4 bytes
- It uses the smallest number of bytes it can for each character
- A byte is 8 bits
- If you stick to English, each character is one byte long
- You will find a listing on Unicode characters represented in UTF-8
here
Ranges of Characters in ASCII
- The ASCII characters fall into certain ranges
- The first 31 characters are control characters
- The characters from 32 to 47 are symbols
32 (Space)
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
- The digits come next
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
- Then some more symbols
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
- Then the capital letters
65 A
...
90 Z
- Some more symbols
91 [
92 \
93 ]
94 ^
95 _
96 `
- The lower case letters
97 a
...
122 z
- And the last few symbols
123 {
124 |
125 }
126 ~
- Both Unix and Python are
case sensitive
- Which means that UPPERCASE and lowercase letters are totally different
characters
Variables
- What do you do with something you need to use ...
- but not at this moment?
- You put it somewhere it won't get lost ...
- so you can find it later
- Some old desk had slots to hold such things
- The slots were call pigeon holes
- 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 stores a single value
- In some languages, such as Java, C and C++ ...
- you must specify what type of value the variable holds
- You must do this before you use the variable
- And the type of value it holds cannot be changed
- If you declared the variable number to be a decimal
- You could not store a string in it
- This is not true in Python ...
- or other scripting languages like Perl and PHP
- You can store a string in a Python variable one moment
- Then store an integer in the same variable some time later
Creating Variables with Assignment Statements
- In Python you create a variable by giving it its first value
- This is done with an
assignment statement
- An assignment statement has the general form
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"
- If I wanted to define the variable
rate with the value .03, I would write
rate = .03
- Once you have defined a variable ...
- you can change its value with another assignment statement
rate = .05
- That is why they are called variables
More on Characters
- In some computer languages, like C and Java ...
- each type of quote has a special purpose
- But not in Python
- Why?
- What if you wanted to print the following
What's for dinner?
- If you enclosed the string inside single quotes you would get an error
>>> print('Hello world!')
Hello world!
>>> print('What's for dinner?')
File "<stdin>", line 1
print('What's for dinner?')
^
SyntaxError: invalid syntax
- When the interpreter saw the ' in what's ...
- it thought the string was over ...
- and it had no idea what to do with the rest of the line
- To get around this we can enclose the string in double quotes,
",
>>> print("What's for dinner?")
What's for dinner?
- Similarly, if the string contained double quotes ...
- we would enclose the string literal in single quotes
>>> print('He cried "Help!"')
He cried "Help!"
- I tend to use doubles quotes because they are easier to see
- But you should feel free to use either kind of quotes
Class Exercise
Class Quiz