Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.
-
Upload
antoine-borden -
Category
Documents
-
view
213 -
download
0
Transcript of Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.
Lilian BlotLilian Blot
CORE ELEMENTSPART I
Python Language
Autumn 2013TPOP
1
Lilian Blot
Values and Data Types
A value is one of the fundamental things that a program manipulates
A value can be: a letter ‘L’ a word ‘Lilian’ a number 1, 3.14, 1.2e3 (e.g. 1200.0) and more complex data (seen later in the year)
Values have a type, 1 and 1.0 are not represented the same way
Autumn 2013TPOP
2
Lilian Blot
Values and Data Types
Words and letters are strings (‘str’ in Python) ‘lilian blot’ or “lilian blot”
Numbers with a decimal point are called float (‘float’) 124.0, 124e2, 0.123
Integer (‘int’), number with no decimal point 1, 124, 0
Autumn 2013TPOP
3
>>> type(“Lilian Blot”)<type 'str'>>>> type(25)<type 'int'>>>> type(190.0)<type 'float'>
Python shell
Lilian Blot
Variables
A critical aspect of programming is the means it provides for using names to refer to computational objects
We say that the name identifies a variable whose value is the object
Python uses the assignment statement =
Autumn 2013TPOP
4
>>> name = “Lilian Blot”>>> age = 25>>> height_cm = 190.0
Python shell
Lilian Blot
Variables and Data Types
Variables have types
The type of a variable is the type of the value it refers to
Autumn 2013TPOP
5
>>> type(name)<type 'str'>>>> type(age)<type 'int'>>>> type(height_cm)<type 'float'>
Python shell
Lilian Blot
State Diagram
Autumn 2013TPOP
6
“Lilian Blot”
190.0
25
name
age
height_cm
>>> name = “Lilian Blot”>>> age = 25>>> height_cm = 190.0
Python shell
>>> print nameLilian Blot
Python shell
Lilian Blot
Variable Names
variable names must start with a letter or an underscore
variable names can contain letters, numbers and underscores
variable names can be arbitrary long
Python is case sensitive, so size and Size are two different variables
by convention uppercase letters are not used
multiple words names use underscore, e.g. book_title, book_price, …
Autumn 2013TPOP
7
Lilian Blot
Variable Name and Keyword
Language’s rule and structure are defined by keywords
Keywords cannot be used as variable names
Autumn 2013TPOP
8
and as assert break class continue def del
elif else except exec for finally from global
if import in is not lambda or pass
print raise return try while with yieldPython’s 31 keywords
Lilian Blot
Statements & Expressions
A statement is an instruction that the Python interpreter can execute
An expression is a combination of values, variables, and operators
Autumn 2013TPOP
9
>>> “Dr ” + name‘Dr Lilian Blot’
Python shell
>>> name = “Lilian Blot”>>> print nameLilian Blot
Python shell
Lilian Blot
Operators
Operators are special symbols that represent computations like addition and multiplication.
The values the operator uses are called operands.
As in Mathematics, when more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. the result of 3 + 4 * 2 is 11, not 14
Parentheses have the highest precedence the result of (3 + 4) * 2 is 14, not 11 use of parentheses is encouraged in long and complex expressions
Autumn 2013TPOP
10
Lilian Blot
Operator Precedence
Operator Name
Precedence
+, - Unary plus and minus (e.g. -10)
** Exponentiation
*, /, //, % Multiplication, division, integer division, and remainder
+, - Binary plus and minus (e.g. 3-10)
= Assignment operators
Autumn 2013TPOP
11
Lilian Blot
Operator Precedence
If operators with the same precedence are next to each other, their associativity determines the order of evaluation ALL binary operators are left-associative
If in doubt, use parenthesis for clarity
Autumn 2013TPOP
12
Lilian Blot
Composition
Expression can be combined to create a more complex expression
For example:<expression_1> operator <expression_2> (age * 10) + (height_cm / 100)print (‘Dr’ + name) +(“>”* 3)
Autumn 2013TPOP
13
Lilian Blot
Assignment Operator
However, the left-hand side of the assignment operator has to be a variable, not an expression
Autumn 2013TPOP
14
>>> “Dr ” + name = title_nameSyntaxError: can't assign to operator>>>
Python shell
Lilian Blot
Assignment and Expressions15
Autumn 2013TPOP
>>> firstname = ‘Lilian’>>>
Python shell
‘Lilian’
Lilian Blot
Assignment and Expressions16
Autumn 2013TPOP
>>> firstname = ‘Lilian’>>>
Python shell
firstname
‘Lilian’
Lilian Blot
Assignment and Expressions17
Autumn 2013TPOP
>>> firstname = ‘Lilian’>>> lastname = ‘Blot’>>> title = ‘Dr’>>>
Python shell
firstname lastname
title
‘Lilian’
‘Dr’
‘Blot’
Lilian Blot
Assignment and Expressions18
Autumn 2013TPOP
>>> firstname = ‘Lilian’>>> lastname = ‘Blot’>>> title = ‘Dr’>>> name = firstname + ‘ ‘ + lastname>>>
Python shell
firstname lastname
title
‘Lilian’
‘Dr’
‘Blot’
‘Lilian’ + ‘ ‘ + ‘Blot’
firstname lastname
Lilian Blot
Assignment and Expressions19
Autumn 2013TPOP
>>> firstname = ‘Lilian’>>> lastname = ‘Blot’>>> title = ‘Dr’>>> name = firstname + ‘ ‘ + lastname>>>
Python shell
firstname lastname
title
‘Lilian’
‘Dr’
‘Blot’
‘Lilian Blot’
‘Lilian Blot’
name
Lilian Blot
Assignment and Expressions20
Autumn 2013TPOP
>>> firstname = ‘Lilian’>>> lastname = ‘Blot’>>> title = ‘Dr’>>> name = firstname + ‘ ‘ + lastname>>> name‘Lilian Blot’>>>
Python shell
firstname lastname
title
name
‘Lilian’
‘Dr’
‘Blot’
‘Lilian Blot’
Lilian Blot
Assignment and Expressions21
Autumn 2013TPOP
>>> firstname = ‘Lilian’>>> lastname = ‘Blot’>>> title = ‘Dr’>>> name = firstname + ‘ ‘ + lastname>>> name‘Lilian Blot’>>> name = title + ‘ ‘ + name
Python shell
firstname lastname
title
name
‘Lilian’
‘Dr’
‘Blot’
‘Lilian Blot’
‘Dr Lilian Blot’
‘Dr’ + ‘ ‘ + ‘Lilian Blot’
Lilian Blot
Assignment and Expressions22
Autumn 2013TPOP
>>> firstname = ‘Lilian’>>> lastname = ‘Blot’>>> title = ‘Dr’>>> name = firstname + ‘ ‘ + lastname>>> name‘Lilian Blot’>>> name = title + ‘ ‘ + name>>> name ‘Dr Lilian Blot’
Python shell
firstname lastname
title
name
‘Lilian’
‘Dr’
‘Blot’
‘Dr Lilian Blot’
Lilian Blot
Summary
Values, Variables and Data Types
Operators and the ‘rules of precedence’
Autumn 2013TPOP
23