Date post: | 09-Mar-2018 |
Category: |
Documents |
Upload: | trinhxuyen |
View: | 257 times |
Download: | 9 times |
Python Language
ICS1133 Natural Language Programming
Python II
Mike Rosner, Dept ICSFebruary 2012
Python II LII Natural Language Programming 1/ 16
Python Language
Contents
1 Python LanguageValues and TypesVariablesStatementsOperators and OperandsStrings
Python II LII Natural Language Programming 2/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
The Interpreter
The Python prompt is indicated by >>>
If a command requires more input, a “. . .” prompt will appear
Many interpreters allow the use of ↑ (up arrow key) to recallpreviously entered lines.
Python II LII Natural Language Programming 3/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Values and Types
In order to perform computations we need raw materials.
Values are one kind of raw material out of whichcomputations are made.
They come in different flavours or Types e.g.
Integer: 1234String: ’Hello World’
The interpreter will tell you the type of a value
>>>type(1234)<type ’int’>>>>type(’Hello’)<type ’str’>>>>type(’1234’)<type ’str’>
Python II LII Natural Language Programming 4/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Variables
A variable is an identifier that refers to a value
Identifiers can be of arbitrary length and must begin with aletter.
Attention keywords! They cannot function as variables
Illegal identifiers yield syntax errors
>>> 12three= 123Syntax error>>> mike@uom = ’nice’Syntax error>>> class = ’very exciting’Syntax error
Python II LII Natural Language Programming 5/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Statements
A statement is a unit of code that can be executed, e.g.print statement, assignment statement.In interactive mode, the statement is executed and the value,if any is printedAn assignment statement assigns a value to a variable
Examples
>>> x = ’yes’>>> print xyes>>> x = 3>>> x,y = 2,1>>> y1>>> x
2Python II LII Natural Language Programming 6/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Puzzle
What is happening here?
Weird
>>> tel = 23402505>>> tel>>> 23402505>>> tel = 01456451>>> tel>>> 417065
hint: print the values 01, 010, 0100
Leading zero indicates an octal number
Python II LII Natural Language Programming 7/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Arithmetic Operators
Python provides a list of arithmetic operators, which could beapplied to all numeric types
Operator Result
x+y Sum of x and y
x-y Difference of x and y
x*y Product of x and y
x/y Quotient of x and y
x//y Floored quotient of x and y
x%y Remainder of x / y
-x x negated
+x x unchanged
pow(x, y) x to the power y
x**y x to the power y
Python II LII Natural Language Programming 8/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Arithmetic Expressions
An expression is a combination of values, variables andoperators.
If you type an expression at Python, it gets evaluated
>>> 2 + 24
Note that the expression has a value, but doesn’t do anything.
What’s more, that value can be assigned to a variable>>> x = (2 + 2)
This is in contrast to a statement that does something butdoes not necessarily have a value.
Python II LII Natural Language Programming 9/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Precedence Rules: PEMDAS
Parentheses
Exponentiation
Multiplication and Division
Addition
Subtraction
Example
>>> 1 + 5 * 2 - 3
8>>>
Python II LII Natural Language Programming 10/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Precedence Rules: PEMDAS
Parentheses
Exponentiation
Multiplication and Division
Addition
Subtraction
Example
>>> 1 + 5 * 2 - 38>>>
Python II LII Natural Language Programming 10/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Overloading of Operators
What you see is not what you getThe same operator behaves differently depending on the typesof the
>>> type(1.0)<type ’float’>>>> type(1)<type ’int’>>>> 1/3
0>>> 1.0/3.00.3333333333333333>>> 1.0/30.3333333333333333>>> 1/3.00.3333333333333333
Python II LII Natural Language Programming 11/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Overloading of Operators
What you see is not what you getThe same operator behaves differently depending on the typesof the
>>> type(1.0)<type ’float’>>>> type(1)<type ’int’>>>> 1/30
>>> 1.0/3.00.3333333333333333>>> 1.0/30.3333333333333333>>> 1/3.00.3333333333333333
Python II LII Natural Language Programming 11/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Overloading of Operators
What you see is not what you getThe same operator behaves differently depending on the typesof the
>>> type(1.0)<type ’float’>>>> type(1)<type ’int’>>>> 1/30>>> 1.0/3.0
0.3333333333333333>>> 1.0/30.3333333333333333>>> 1/3.00.3333333333333333
Python II LII Natural Language Programming 11/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Overloading of Operators
What you see is not what you getThe same operator behaves differently depending on the typesof the
>>> type(1.0)<type ’float’>>>> type(1)<type ’int’>>>> 1/30>>> 1.0/3.00.3333333333333333
>>> 1.0/30.3333333333333333>>> 1/3.00.3333333333333333
Python II LII Natural Language Programming 11/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Overloading of Operators
What you see is not what you getThe same operator behaves differently depending on the typesof the
>>> type(1.0)<type ’float’>>>> type(1)<type ’int’>>>> 1/30>>> 1.0/3.00.3333333333333333>>> 1.0/3
0.3333333333333333>>> 1/3.00.3333333333333333
Python II LII Natural Language Programming 11/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Overloading of Operators
What you see is not what you getThe same operator behaves differently depending on the typesof the
>>> type(1.0)<type ’float’>>>> type(1)<type ’int’>>>> 1/30>>> 1.0/3.00.3333333333333333>>> 1.0/30.3333333333333333
>>> 1/3.00.3333333333333333
Python II LII Natural Language Programming 11/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Overloading of Operators
What you see is not what you getThe same operator behaves differently depending on the typesof the
>>> type(1.0)<type ’float’>>>> type(1)<type ’int’>>>> 1/30>>> 1.0/3.00.3333333333333333>>> 1.0/30.3333333333333333>>> 1/3.0
0.3333333333333333
Python II LII Natural Language Programming 11/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Overloading of Operators
What you see is not what you getThe same operator behaves differently depending on the typesof the
>>> type(1.0)<type ’float’>>>> type(1)<type ’int’>>>> 1/30>>> 1.0/3.00.3333333333333333>>> 1.0/30.3333333333333333>>> 1/3.00.3333333333333333 Python II LII Natural Language Programming 11/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Mini Exercise (Downey p.19)
1 Assume that we execute the following assignment statements:
width = 17height = 12.0delimiter = ’.’
For each of the following expressions, write the value of theexpression and the type (of the value of the expression).
1 width/22 width/2.03 height/34 1 + 2 * 55 delimiter * 5
Use the Python interpreter to check your answers.2 Find out ways in which the + and * operators work with
string and integer arguments e.g. ’foo’ + ’foo’
Python II LII Natural Language Programming 12/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Strings
Python has general machinery for handling sequences ofvalues.
Strings are sequences of characters.
Python has a special syntax for handling strings
Strings can be delimited with single, double, and triplequotation marks
>>> s1 = ’foo’>>> s2 = ’’foo’’>>> s3 = ’’’foo’’’
Strings delimited with triple quotation marks can stretchacross lines
Python II LII Natural Language Programming 13/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Indices
Python includes notation for accessing subparts of sequences.
In the case of strings this is straightforward. An integer indexis used to access individual elements
>>> s = ’eggs’>>> s[1]g>>> len(s)4
Note that the index of the first element is 0
To get the last element of a sequence:
>>> s[len(s) -1]s>>> s[-1]s
Python II LII Natural Language Programming 14/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
Subsequences: Slicing
>>> n =’0123456789’>>> n’0123456789’>>> n[1:3]’12’>>> n[1:]’123456789’>>> n[2:]’23456789’>>> n[:2]’01’>>> n[:4]’0123’
Python II LII Natural Language Programming 15/ 16
Python Language
Values and TypesVariablesStatementsOperators and OperandsStrings
References
Python II LII Natural Language Programming 16/ 16