Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.

Similar presentations


Presentation on theme: "Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1."— Presentation transcript:

1 Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1

2 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 ) and more complex data (seen later in the year) Values have a type, 1 and 1.0 are not represented the same way Autumn 2013 TPOP 2

3 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, Integer ( int ), number with no decimal point 1, 124, 0 Autumn 2013 TPOP 3 >>> type(Lilian Blot) >>> type(25) >>> type(190.0) Python shell

4 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 2013 TPOP 4 >>> name = Lilian Blot >>> age = 25 >>> height_cm = Python shell

5 Lilian Blot Variables and Data Types Variables have types The type of a variable is the type of the value it refers to Autumn 2013 TPOP 5 >>> type(name) >>> type(age) >>> type(height_cm) Python shell

6 Lilian Blot State Diagram Autumn 2013 TPOP 6 Lilian Blot name age height_cm >>> name = Lilian Blot >>> age = 25 >>> height_cm = Python shell >>> print name Lilian Blot Python shell

7 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 2013 TPOP 7

8 Lilian Blot Variable Name and Keyword Languages rule and structure are defined by keywords Keywords cannot be used as variable names Autumn 2013 TPOP 8 andasassertbreakclasscontinuedefdel elifelseexceptexecforfinallyfromglobal ifimportinisnotlambdaorpass printraisereturntrywhilewithyield Pythons 31 keywords

9 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 2013 TPOP 9 >>> Dr + name Dr Lilian Blot Python shell >>> name = Lilian Blot >>> print name Lilian Blot Python shell

10 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 * 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 2013 TPOP 10

11 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 2013 TPOP 11

12 Lilian Blot Operator Precedence Autumn 2013 TPOP 12

13 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 2013 TPOP 13

14 Lilian Blot Assignment Operator However, the left-hand side of the assignment operator has to be a variable, not an expression Autumn 2013 TPOP 14 >>> Dr + name = title_name SyntaxError: can't assign to operator >>> Python shell

15 Lilian Blot Assignment and Expressions 15 Autumn 2013 TPOP >>> firstname = Lilian >>> Python shell Lilian

16 Lilian Blot Assignment and Expressions 16 Autumn 2013 TPOP >>> firstname = Lilian >>> Python shell firstname Lilian

17 Lilian Blot Assignment and Expressions 17 Autumn 2013 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> Python shell firstname lastname title Lilian Dr Blot

18 Lilian Blot Assignment and Expressions 18 Autumn 2013 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> name = firstname + + lastname >>> Python shell firstname lastname title Lilian Dr Blot Lilian + + Blot firstname lastname

19 Lilian Blot Assignment and Expressions 19 Autumn 2013 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> name = firstname + + lastname >>> Python shell firstname lastname title Lilian Dr Blot Lilian Blot name

20 Lilian Blot Assignment and Expressions 20 Autumn 2013 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> name = firstname + + lastname >>> name Lilian Blot >>> Python shell firstname lastname title name Lilian Dr Blot Lilian Blot

21 Assignment and Expressions 21 Autumn 2013 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

22 Lilian Blot Assignment and Expressions 22 Autumn 2013 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

23 Lilian Blot Summary Values, Variables and Data Types Operators and the rules of precedence Autumn 2013 TPOP 23

24 Lilian Blot Assignment and Expressions 24 Autumn 2013 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

25 Lilian Blot Assignment and Expressions 25 Autumn 2013 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

26 Lilian Blot Assignment and Expressions 26 Autumn 2013 TPOP >>> firstname = Lilian >>> lastname = Blot >>> title = Dr >>> name = firstname + + lastname >>> name Lilian Blot >>> name = title + + name >>> name Dr Lilian Blot Python shell


Download ppt "Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1."

Similar presentations


Ads by Google