Download presentation
Presentation is loading. Please wait.
Published byConner Beaumont Modified over 10 years ago
1
Lilian Blot TO PROGRAMMING & PYTHON Introduction Autumn 2012 TPOP 1
2
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 2012 TPOP 2
3
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 2012 TPOP 3
4
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 doesnt work it is YOUR fault. Autumn 2012 TPOP 4
5
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 2012 TPOP 5
6
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%20artic les/Turing's%20O-Machines.html http://www.alanturing.net/turing_archive/pages/reference%20artic les/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 do Autumn 2012 TPOP 6
7
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 2012 TPOP 7
8
Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2012 TPOP 8
9
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 2012 TPOP 9
10
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 2012 TPOP 10
11
Lilian Blot Syntax & Semantic Syntax Tell us which sequence of characters and symbols constitute a well-formed string Static semantic Which well-formed strings have a meaning Semantics What that meaning is Programs have only one meaning, no ambiguity Autumn 2012 TPOP 11
12
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 2012 TPOP 12
13
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 2012 TPOP 13 >>> type(Lilian Blot) >>> type(25) >>> type(190.0) Python shell
14
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 2012 TPOP 14 >>> name = Lilian Blot >>> age = 25 >>> height_cm = 190.0 Python shell
15
Lilian Blot Variables and Data Types Variables have types The type of a variable is the type of the value it refers to Autumn 2012 TPOP 15 >>> type(name) >>> type(age) >>> type(height_cm) Python shell
16
Lilian Blot State Diagram Autumn 2012 TPOP 16 Lilian Blot 190.0 25 name age height_cm >>> name = Lilian Blot >>> age = 25 >>> height_cm = 190.0 Python shell >>> print name Lilian Blot Python shell
17
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 2012 TPOP 17
18
Lilian Blot Variable Name and Keyword Languages rule and structure are defined by keywords Keywords cannot be used as variable names Autumn 2012 TPOP 18 andasassertbreakclasscontinuedefdel elifelseexceptexecforfinallyfromglobal ifimportinisnotlambdaorpass printraisereturntrywhilewithyield Pythons 31 keywords
19
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 2012 TPOP 19 >>> Dr + name Dr Lilian Blot Python shell >>> name = Lilian Blot >>> print name Lilian Blot Python shell
20
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 2012 TPOP 20
21
Lilian Blot Operator Precedence OperatorName +, - 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 2012 TPOP 21
22
Lilian Blot Composition Expression can be combined to create a more complex expression For example: operator (age * 10) + (height_cm / 100) print (Dr + name) +(>* 3) Autumn 2012 TPOP 22
23
Lilian Blot Assignment Operator However, the left-hand side of the assignment operator has to be a variable, not an expression Autumn 2012 TPOP 23 >>> Dr + name = title_name SyntaxError: can't assign to operator >>> Python shell
24
Lilian Blot Assignment and Expressions 24 Autumn 2012 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> name = firstname + + lastname >>> name Lilian Blot >>> name = title + + name >>> name Dr Lilian Blot Python shell
25
Lilian Blot Assignment and Expressions 25 Autumn 2012 TPOP >>> firstname = Lilian >>> Python shell Lilian
26
Lilian Blot Assignment and Expressions 26 Autumn 2012 TPOP >>> firstname = Lilian >>> Python shell firstname Lilian
27
Lilian Blot Assignment and Expressions 27 Autumn 2012 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> Python shell firstname lastname title Lilian Dr Blot
28
Lilian Blot Assignment and Expressions 28 Autumn 2012 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> name = firstname + + lastname >>> Python shell firstname lastname title Lilian Dr Blot Lilian + + Blot firstname lastname
29
Lilian Blot Assignment and Expressions 29 Autumn 2012 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> name = firstname + + lastname >>> Python shell firstname lastname title Lilian Dr Blot Lilian Blot name
30
Lilian Blot Assignment and Expressions 30 Autumn 2012 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> name = firstname + + lastname >>> name Lilian Blot >>> Python shell firstname lastname title name Lilian Dr Blot Lilian Blot
31
Assignment and Expressions 31 Autumn 2012 TPOP >>> 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
32
Lilian Blot Assignment and Expressions 32 Autumn 2012 TPOP >>> 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
33
Lilian Blot Assignment and Expressions 33 Autumn 2012 TPOP >>> 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
34
Lilian Blot Assignment and Expressions 34 Autumn 2012 TPOP >>> 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
35
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 2012 TPOP 35
36
Lilian Blot Quizzes Autumn 2012 TPOP 36
37
Lilian Blot Exercises Autumn 2012 TPOP 37
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.