accelerating information technology...

60
Accelerating Information Technology Innovation http://aiti.mit.edu/program/philippines-summer-2012/ Philippines Summer 2012 Lecture 1 – Introduction to Python June 19, 2012

Upload: others

Post on 15-Mar-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Accelerating Information Technology

Innovation

http://aiti.mit.edu/program/philippines-summer-2012/

Philippines Summer 2012 Lecture 1 – Introduction to Python

June 19, 2012

Page 2: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Agenda

•  About the Course •  What is Python? and Why Python? •  Basic Syntax •  Strings •  User Input •  Useful Data Structures •  Introduction to Functions

2

Page 3: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

About the Course

3

Page 4: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Administrivia •  Lectures and labs will be posted at:

–  http://aiti.mit.edu/materials/philippines-summer-2012/

•  We will create an Official Mailing List for the course and announce it on Thursday.

•  Lab sessions are from 6-9pm. We will be available during that time to check your homework.

•  Please do the assigned readings before the class. It will help you understand the material covered in class better. 4

Page 5: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Course Expectations

•  Attend class every day •  Collaborate •  Teach others as much as you can •  Do everything you can in the labs •  Ask questions!

5

Page 6: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Technical Course Outline

•  Week 1 – Python •  Week 2 – Rapid Mobile Web Application

Development with Django •  Week 3 – Android App Development •  Week 4 – SMS and IVR Application

Development •  Weeks 5 & 6 – Final Project •  Week 7 – Project Showcase

6

Page 7: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

What is Python?

7

Page 8: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Python is…

•  …interpreted. Languages like C/C++ need to translate high-level code to machine code…

8

Compiler

High-Level Code

Machine Code

a = b + c;

… ld $r1, a ld $r2, b add $r3, $r1, $r2 st a, $r3 …

Page 9: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

•  …which means that a program has to be compiled separately for each type of machine:

9

program

compiler compiler

compiler

Win Mac

Unix

machine code machine code machine code

Page 10: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Python is… •  Python code is compiled to an

intermediate format called bytecode, which is understood by a virtual machine/interpreter.

10

Python Source (.py)

compiler

Python Bytecode (.pyc)

Page 11: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Python is…

11

Python Program

compiler

Python bytecode

Win

Mac

Unix

Interpreter

Interpreter

Interpreter

Page 12: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Why Python?

12

Page 13: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Python because…

•  Portable and architecture-agnostic •  Convenient built-in functions and data

structures •  Syntax is readable and fast to write

13

if (x) {

if (y) { a(); } b();

}

if x: if y: a() b()

Page 14: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Python because…

•  Great for rapid prototyping – No separate compile step – No need to explicitly specify method argument

types beforehand (due to dynamic typing)

14

Page 15: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Python for us, because…

•  We want each of you to reach millions of users, and don’t want to waste time building the pipes and plumbing

•  Python is supported by a number of good frameworks, led by – Django – Heroku – Google AppEngine

15

Page 16: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

The (Ideal) Development Cycle

•  Clearly specify the problem: –  Inputs, input manipulation, outputs

•  Design the solution: – E.g. what algorithms, data structures

•  Implementation •  Test

16

Page 17: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

The (Real) Development Cycle

•  As above, but faster. – Python, as a dynamically typed, programming

language is perfect for rapid prototyping •  Be prepared to throw away one (or more!)

prototypes – Often you learn crucial things about the

problem as you code which cannot be fixed without starting from scratch.

17

Page 18: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Basic Syntax

18

Page 19: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Syntax

•  Blocks are delimited with whitespace: specifically, four spaces (and no tabs)

19

if x: if y: a() b()

count = 0 for i in range(0:5) count += i

Page 20: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Syntax

•  Semicolons are only used to separate multiple statements on the same line, which is discouraged:

20

if (x) {

a(); b();

}

if x: a(); b()

if x: a()

b()

No

Yes

Page 21: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Syntax

•  Single line comments are denoted with hash (#), multiline with three quotes ”””

21

# This is a comment foo()

””” This is a longer comment ””” foo()

Page 22: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Interaction •  Python has an interactive console which is

great for tinkering

•  …etc

22

$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type “help”, “copyright”, “credits” or “license” for more information >>> a = 1 >>> a 1 >>> type(a) <type ‘int’> >>>

Page 23: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Variables •  Strings >>>  x  =  ‘Hello  World’  

•  Numerics >>>  x  =  3.1415  

•  Booleans  >>>  x  =  True  

•  Lists >>>  x  =  [‘Hello’,  True,  3.1415]  

•  And many more…

23

Page 24: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Variables •  Python is a “dynamically typed” language

–  A variable’s data type is not declared. –  “Statically typed” languages like Java must declare a

variable’s data type

String  x  =  “Hello  World”;  

•  Get a variable’s data type with the type function  >>>  x  =  ‘Hello  World’    >>>  type(x)    <type  'str'>  

24

Page 25: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Strings

25

Page 26: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Strings

•  A string is a piece of text. •  Encase with quotes

– Single-quotes  >>>  x  =  ‘abc’  – Double-quotes  >>>  x  =  “abc”  – Triple single-quotes or triple double-quotes  >>>  x  =  ‘‘‘abc’’’   >>>  x  =  “““abc”””  

26

Page 27: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Strings

•  Use double-quotes to encase text containing single-quotes  >>>  “It’s  a  string  with  a  single-­‐quote!”  

•  What is wrong with this statement?  >>>  x  =  abc  

27

Page 28: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

28

String as a sequence •  You can access the characters one at a time

using the bracket [] operator

28

fruit = “banana” letter = fruit[1] print letter

1 2 3!

b � a� n � a� n � a�

0 1 2 3 4 5 index!

Page 29: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

29

String operators •  Applied to strings, produce strings

29

str1 = 'kit ' str2 = 'kat ' str3 = str1 + str2 str4 = str3 * 2 c = str1[0] c = str1[4]!

1 2 3 4 5 6!

'kit kat ' 'kit kat kit kat '

'k' !IndexError: string index

out of range!

k � i� t �

0 1 2 3

str1!

index!

Page 30: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

30

•  Returns the part of the string from the "m-th" character to the "n-th" character, including the first but excluding the last.

0 1 2 3 4 5 6 7 8 9 10

The slicing operator [m : n]

fruit � S T R A W B E R R Y

index�

str1 = fruit[2:5] str1 = fruit[:5] str1 = fruit[5:] str1 = fruit[6:-1] !

1 2 3 4!

'RAW' 'STRAW' 'BERRY'

'ERR'!

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1

Page 31: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

User Input

31

Page 32: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

32

User Input •  raw_input prints a prompt to the user and

assigns the input to a variable as a string

•  input can be used when we expect the input to be a number

name = raw_input('What is your name?') !

age = input('How old are you?') !

Page 33: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Control Statements

33

Page 34: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

34

Control statements

•  Conditionals: control which set of statements is executed. –  if / else

•  Iteration: control how many times a set of statements is executed. –  while loops –  for loops

34

Page 35: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

35

The if statement

•  If the condition is True, the body gets executed. •  Otherwise, nothing happens.

35

if CONDITION: BODY!

any boolean expression �

any set of statements�

if x < 0: print 'x is negative'!

indentation is important �

Page 36: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

36

The if/else statement

•  If the condition is True, body1 gets executed. •  Otherwise, body2 gets executed.

if CONDITION: BODY1 else: BODY2!

if x < 0: print 'x is negative' else: print 'x is positive or zero'!

any set of statements

Page 37: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

37

Chained conditionals

•  If the condition1 is True, body1 gets executed. •  Otherwise, if condition2 is True, body2 gets executed. •  If neither condition is True, body3 gets executed.

37

if CONDITION1: BODY1 elif CONDITION2: BODY2 else: BODY3!

any set of statements�

another boolean expression �

Page 38: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

38

An example a = False b = True if a and b: print 'I love red.' elif a or b: print 'I love green.' else: print 'I love blue.' print 'I also love purple.'!

What does this output? I love green.!

Page 39: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

39

An example a = False b = True if a and b: print 'I love red.' elif a or b: print 'I love green.' else: print 'I love blue.' print 'I also love purple.'!

What does this output?� I love green. I also love purple.!

Page 40: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

40

Nested conditionals

•  Can get confusing. Indentation helps to keep the code readable and the python interpreter happy!

40

if is_adult: if is_senior_citizen: print 'Admission $2 off.' else: print 'Full price.' else: print 'Admission $5 off.'!

if is_adult: if is_senior_citizen: print 'Admission $2 off.' else: print 'Full price.' else: print 'Admission $5 off.'!

if is_adult: if is_senior_citizen: print 'Admission $2 off.' else: print 'Full price.' else: print 'Admission $5 off.'!

outer conditional inner conditional

Page 41: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

41

The while loop

•  As long as the condition is true, the body gets executed repeatedly.

•  The first time the condition is false, execution ends.

41

while CONDITION: BODY!

any boolean expression �

any set of statements�indentation is important �

Page 42: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

42

The while loop

•  What does this output?

i = 0 while i < 3: print i i = i + 1!

0 1 2!

Page 43: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

43

The break statement •  Immediately exits the innermost loop. while True: line = raw_input('>>> ') if line == 'done': break print line print 'Done!'!

>>> not done not done >>> done Done!!

Page 44: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Useful Data Structures

44

Page 45: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

45

•  A list is a sequence of values. •  Each element (value) is identified by an index. •  The elements of the list can be of any type.

Lists

tens = [10, 20, 30, 40] cities= [’Manila', ‘Cebu', ‘Boracay’] empty = []!

mixed = ['hello', 2.0, 5, [10, 20]]!

•  Lists can have mixed types in them, even other lists (nested).

Page 46: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

46

•  Use the [] brackets

Creating a list

list_of_ints = [10,20,30,50]!

list_of_ints!10! 20! 30! 50!

four int values

only one name

Page 47: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

47

•  Individual elements are accessed using the [] operator.

Accessing list elements

list_of_ints[0] = 17!

list_of_ints!17! 20! 30! 50!now has value 17

Lists are mutable! Assigns the first element to 17

List indexing starts at 0, not 1!

new_var = list_of_ints[0]! accesses the value of the first element

30! 50!17!list_of_ints! 20!

new_var!17! now also has value 17

index

0 1 2 3!

Page 48: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

48

•  We can use the print function to output the contents of the list:

Printing a list

cities = [’Manila', ‘Cebu', ‘Boracay’] numbers = [17, 123] empty = [] print cities, numbers, empty!

[’Manila', ’Cebu', ’Boracay'] [17, 123] [ ]!

Page 49: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

49

Lists vs. Strings

•  Lists are mutable - their contents can be modified

•  Strings are immutable

49

name = 'Lenny' name[0] = 'J'!

TypeError: object doesn't support item assignment!

Page 50: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Control Structures

50

Page 51: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

51

The for loop

•  Example:

51

for ELEMENT in SEQUENCE: BODY!

any set of statements

for i in [0,1,2,3]: print i!

sequence element Sequence of values – list, string, etc.

0 1 2 3!

indentation is important

Page 52: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

52

Using range

•  What does this output? 0 0 1 1 2 4 3 9!

for i in range(4): sq = i * i print i, sq!

for INDEX in range(n): BODY!

any set of statements

index variable generates sequence of n values starting at 0 and incrementing by 1

Page 53: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

53

Using range

•  What does this output?

1 3 5 !

for i in range(1, 7, 2): print i!

for INDEX in range([start], stop, [step]): BODY!

any set of statements

index variable generates sequence of values start and step are optional

Page 54: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

54

For loop and strings

•  Iterating through the characters of a string

54

str1 = 'stressed' for c in str1: print c,!

s t r e s s e d!

Page 55: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

55

For vs While

•  For loop is primarily used •  for iterating over a sequence of values •  when we know the number of iterations in advance

•  While loop is primarily used •  when we don't know the number of iterations in

advance (they could be controlled by user input)

55

Page 56: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Introduction to Functions

56

Page 57: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

57

Functions •  A function is a sequence of statements that has been

given a name.

57

def NAME (PARAMETERS): STATEMENTS!

any set of statements�

function name�list of function arguments�

function definition �

function signature�

Page 58: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Now you are all set to work on Lab 1!

58

Page 59: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Lab 1 1.  Calculate Fibonacci number

fib(n) 2.  Display the day of the week given a date

zellers() 3.  Implement the Rock Paper Scissors game

rock_paper_scissors() 4.  Encode a given string using the Caesar

cipher cipher()

59

Page 60: Accelerating Information Technology Innovationgsl-archive.mit.edu/media/programs/philippines-summer... · 2014. 1. 21. · 10 Python Source (.py) compiler Python Bytecode (.pyc) Python

Next Class

•  More on Functions •  Object Oriented Programming •  Exceptions •  Regular Expressions •  How to be a Python Ninja!

60