lilian blot to programming & python introduction autumn 2012 tpop 1
TRANSCRIPT
Lilian BlotLilian Blot
TO PROGRAMMING&
PYTHON
Introduction
Autumn 2012TPOP
1
Lilian Blot
Declarative vs. Imperative Knowledge
Declarative knowledge Statement of facts, does not tell you how to get to the
fact, but does enable you to check the result is correct
Imperative knowledge Tell you how to accomplish something, how to solve a
problem, a kind of a recipe. Recipe to approximate the solution for example
Autumn 2012TPOP
2
Lilian Blot
What is an Algorithm?
An Algorithm is a description on how to perform a computation
Instructions Flow of Control Termination Condition Does it converged? Find a solution
A computation is the process of taking input and following a step-by-step algorithm to get a specific output.
Autumn 2012TPOP
3
Lilian Blot
Computational Process
A computational process, in a correctly working computer, executes programs precisely and accurately
In other words:The computer will always do what you tell them to do. So if a program doesn’t work it is YOUR fault.
Autumn 2012TPOP
4
Lilian Blot
Types of Errors
Crashes
Never stops => infinite loop
Runs to completion & produces the wrong answer http://www.devtopics.com/20-famous-software-disasters/
Autumn 2012TPOP
5
Lilian Blot
What is a Program?
Given a small set of instruction you can build any kind of program that you want Alan Turing showed that in fact there are 6 primitive instructions
that are needed to do anything (computable) with a computer http://
www.alanturing.net/turing_archive/pages/reference%20articles/Turing's%20O-Machines.html
A Programming language, such as Python, provides a set of primitive functions
Interpreter Is a program that can execute any legal set of instruction. Can be
used to describe anything a computer can doAutumn 2012TPOP
6
Lilian Blot
What is a Program?
A program is a sequence of instructions that specifies how to perform a computation.
Few basic instructions are needed to write any kind of program: input: Get data from the keyboard, a file, or some other device. output: Display data on the screen or send data to a file or other
device. math: Perform basic mathematical operations like addition and
multiplication. conditional execution: Check for certain conditions and execute the
appropriate sequence of statements. repetition: Perform some action repeatedly, usually with some
variation.
Autumn 2012TPOP
7
Lilian BlotLilian Blot
CORE ELEMENTSPART I
Python Language
Autumn 2012TPOP
8
Lilian Blot
A Powerful Programming Language
Primitive expressions Represent the simplest entities with which the
language is concerned
Means of combination Compound expression are built from simpler ones
Means of abstraction Compound objects can be named and manipulated as
units
Autumn 2012TPOP
9
Lilian Blot
Formal Language
Formal Languages are language designed for specific application
Python is a formal Language
Python is unambiguous
Python has strict syntax rules Syntax rules with regard to tokens, basic elements of the
language Syntax rules with regard to structure, the way tokens are
arranged in a statement
Autumn 2012TPOP
10
Lilian Blot
Syntax & Semantic
Syntax Tell us which sequence of characters and symbols
constitute a well-formed stringStatic semantic
Which well-formed strings have a meaningSemantics
What that meaning is Programs have only one meaning, no ambiguity
Autumn 2012TPOP
11
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 2012TPOP
12
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 2012TPOP
13
>>> 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 2012TPOP
14
>>> 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 2012TPOP
15
>>> type(name)<type 'str'>>>> type(age)<type 'int'>>>> type(height_cm)<type 'float'>
Python shell
Lilian Blot
State Diagram
Autumn 2012TPOP
16
“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 2012TPOP
17
Lilian Blot
Variable Name and Keyword
Language’s rule and structure are defined by keywords
Keywords cannot be used as variable names
Autumn 2012TPOP
18
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 2012TPOP
19
>>> “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 2012TPOP
20
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 2012TPOP
21
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 2012TPOP
22
Lilian Blot
Assignment Operator
However, the left-hand side of the assignment operator has to be a variable, not an expression
Autumn 2012TPOP
23
>>> “Dr ” + name = title_nameSyntaxError: can't assign to operator>>>
Python shell
Lilian Blot
Assignment and Expressions25
Autumn 2012TPOP
>>> firstname = ‘Lilian’>>>
Python shell
‘Lilian’
Lilian Blot
Assignment and Expressions26
Autumn 2012TPOP
>>> firstname = ‘Lilian’>>>
Python shell
firstname
‘Lilian’
Lilian Blot
Assignment and Expressions27
Autumn 2012TPOP
>>> firstname = ‘Lilian’>>> lastname = ‘Blot’>>> title = ‘Dr’>>>
Python shell
firstname lastname
title
‘Lilian’
‘Dr’
‘Blot’
Lilian Blot
Assignment and Expressions28
Autumn 2012TPOP
>>> 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 Expressions29
Autumn 2012TPOP
>>> 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 Expressions30
Autumn 2012TPOP
>>> 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 Expressions31
Autumn 2012TPOP
>>> 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 Expressions32
Autumn 2012TPOP
>>> 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
Assignment and Expressions33
Autumn 2012TPOP
>>> 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’
‘Lilian Blot’
‘Dr Lilian Blot’
Lilian Blot
Assignment and Expressions34
Autumn 2012TPOP
>>> 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’
‘Lilian Blot’
‘Dr Lilian Blot’
Lilian Blot
Summary
What is a program
Python a programming language Formal language with syntactic rules
Values, Variables and Data Types
Operators and the ‘rules of precedence’
Autumn 2012TPOP
35