Python 1: Introduction to Python
Class 3
Today's Topics
Tips and Examples
Review
New Material
Reading Assignment
You should read Chapter 2, Input, Processing and Output,
from our textbook, Starting Out with Python, before next week's class.
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.
First Graded Quiz
The first graded quiz will be given on Tuesday next week.
The questions from the graded quiz are taken from the ungraded quizzes of the previous week.
Unless I tell you otherwise, you should expect a graded quiz each week.
If you miss the graded quiz on Tuesday you can take it on Thursday.
If you miss it on both Tuesday and Thursday you will get a 0 on the quiz.
Tips and Examples
Review
Working with Python
- In this class, we will be working with Python on the Linux machine users3.cs.umb.edu
- All our work for this course must be copied to this machine
- We will be using Python 3.5.1 or later
Running Python in Interactive Mode
- There are two ways of using Python
- Interactively
- Writing Python scripts
- If you enter
python3
on the Unix command line and hit Enter
you will find yourself using Python's
interactive mode
$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
- In Python interactive mode, you can type Python statements directly at the command line
- And get the results immediately
- Whenever you see >>>, Python is ready for your input
- In interactive mode, you can use the Python interpreter as a calculator
>>> 5 + 6
11
>>> 6 - 5
1
>>> 3 * 5
15
>>> 4.1 / 2
2.05
- To leave the interactive mode you must type
>>> exit()
- or hit Control D
Python Scripts
New Material
Output
Literals
- Programs are used to 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 these values we need to write them directly inside the program
- A value that is written directly into the code and does not have to be calculated
is called a literal
Strings and String Literals
- All computer programs use different types of values
- The values can be words, numbers or other things
- When the value is just text, not a number, it is called a
string
- A string is just a sequence of characters
- A string can be calculated or we can write the string directly inside the code
- When we write a string directly inside the code we have created a
string literal
- In Python, string literals must be enclosed in either single quotes, '
or double quotes, "
- You can use either single or double quotes when writing a string literal
- But you must use the same type of quote at the end of a string that you used at the beginning
- If you don't you will get an error message
- 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
- But we'll have no trouble if we enclose it 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!"
- Most of the time I use single quotes because I can write them without holding down the Shift key
- But you should feel free to use either kind of quotes
Triple Quotes
- There is another way to write string literals in Python that we will not be using in this class
- This method involves using three quotes at the beginning and end of the string
- You can use either single quotes or double quotes but whatever quote characters you use a the beginning
have to be used at the end
- You can't mix quotes
- It does not matter which type of quote you use as long as the quotes at the beginning and end are the same
- If you use triple quotes to enclose a string literal you can use both single and double quotes freely inside the string itself
>>> print('''He's crying "Help!"''')
He's crying "Help!"
- The other advantage to using triple quotes is that they can hold a string literal
that runs across several lines
>>> print("""Line 1
... Line 2
... Line 3""")
Line 1
Line 2
Line 3
- ... is the way the Python interpreter tells you it is waiting for more
- You can't do this with ordinary quotes
>>> print("Line 1
File <stdin>, line 1
print("Line 1
^
SyntaxError: EOL while scanning string literal
- This error message is saying that the interpreter can't find the end of the string
Characters
ASCII
- In order to represent characters as numbers we need a table of all the characters
and their corresponding numbers
- In the early days of computing in this country the most frequently used table of this type was called 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
- ASCII has 128 characters which stood for 95 characters
- And 33 control codes
- Each character can be stored in 7
bits of memory
- Lowercase and uppercase letters have different numeric values in ASCII
>>> ord('a')
97
>>> ord('A')
65
>>>
- Since the digits 0 to 9 are characters 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 an new character table called Unicode
- Unicode represents all the characters in most world languages
- The first 128 Unicode characters are the ASCII characters
- so ASCII is included in Unicode
- The latest version of Unicode contains 128,237 characters covering 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
- But also in Japanese Kanji Korean Hanja and Vietnamese Chu Nom
Representing Unicode Values
- With close to 130,000 characters you cannot represent all Unicode characters in only 7 bits as you can in ASCII
- Some characters would require 17 bits
- If you used 17 bits to represent every character text documents would be extremely large
- But the number values that represent different Unicode characters
can be represented inside the computer 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
- 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
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
- The numbers 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
- 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 with a name that stores a value
- In a many computer languages, such as Java you must specify what type of value a variable will hold
before you can use it
- So if you declared the variable to be a decimal number you could not store a string in it
- This is not true in Python
- You can store a string in a Python variable one moment >\then store an integer in it at some time later
Creating Variables with Assignment Statements
- You create a variable by giving it its first value
- This is done with an
assignment statement
- An assignment statement has the general form
VARIABLE_NAME = VALUE
- Whenever you see a format statement in this class where a word is in ALL CAPITALS ...
- that means that it is a placeholder
- In other words, it stands for something 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 it's value with another assignment statement
rate = .05
- That is why they are called variables
Attendance
Class Quiz