46
1 of 46 Module 2 : Introduction to Python Introduction to Computational Thinking Module 2 : Introduction to Python Asst Prof Chi-Wing FU, Philip Office: N4-02c-104 email: cwfu[at]ntu.edu.sg

Lecture 2 introduction to python

Embed Size (px)

Citation preview

Page 1: Lecture 2  introduction to python

1 of 46Module 2 : Introduction to Python

Introduction to

Computational Thinking

Module 2 :

Introduction to Python

Asst Prof Chi-Wing FU, Philip

Office: N4-02c-104

email: cwfu[at]ntu.edu.sg

Page 2: Lecture 2  introduction to python

2 of 46Module 2 : Introduction to Python

Topics

• What is Python?

• Download and install Python

• Running a Python program

• Interpreter VS Compiler

Page 3: Lecture 2  introduction to python

3 of 46Module 2 : Introduction to Python

What is Python?

• A programming language

(a computer language designed to express computations

that can be performed by a computer)

• Developed by Guido van Rossum

• Released in 1991

• Current (popular) versions: 2.7 & 3.2

• Open Source - You can find the source code here:

http://www.python.org/download/source/

• Python official website: http://www.python.org/

Page 4: Lecture 2  introduction to python

4 of 46Module 2 : Introduction to Python

Origin of its name

• Named after Monty Python’s Flying Circus,

which is a BBC TV sketch comedy series

broadcasted from 1969 to 1974

Page 5: Lecture 2  introduction to python

5 of 46Module 2 : Introduction to Python

Some Useful Websites

• Python - Official Website:

http://www.python.org/

• Beginner’s Guide to Python

http://wiki.python.org/moin/BeginnersGuide

• Common Pitfall for Beginners:

http://wiki.python.org/moin/BeginnerErrorsWithPythonProg

ramming (you may visit it after you learn more Python)

Page 6: Lecture 2  introduction to python

6 of 46Module 2 : Introduction to Python

Use of Python

• Web Development

• Google app engine and search spiders

• Facebook (Tornado web server)

• Games

• AI Programming

• Team balancing

• Score keeping

• Scripting in Graphics/Rendering

• Scripting in Blender 3D

……

Page 7: Lecture 2  introduction to python

7 of 46Module 2 : Introduction to Python

Characteristics of Python

• Designed to be simple yet powerful

• Great emphasis on readability (indentation)

• Rapid prototyping

• Interpreted and interactive

(Python looks at program instructions one at a time, and

turns it into something that can be run on the computer)

• Object-oriented programming

(In Python, all data are objects; see next course on OOP)

Not understanding? See later in this module/course!

* Allows fast coding!!!

Page 8: Lecture 2  introduction to python

8 of 46Module 2 : Introduction to Python

Characteristics of Python

Moreover…

• High productivity in program development

• Lots of built-in resources to do this and that

• Runs on Windows, Linux/Unix, Mac OS X

• FREE to use, even for commercial products

Page 9: Lecture 2  introduction to python

9 of 46Module 2 : Introduction to Python

Why Python?

There are so many programming languages!!!

• Fortran, C, C++, Java, Perl, Pascal, Logo, C#,

Visual Basic, Basic, Objective C, ……

Why Python in this course???

… Remember the focus of this course???

Computational Thinking! Problem solving!

Page 10: Lecture 2  introduction to python

10 of 46Module 2 : Introduction to Python

Why Python?Since

• Programming languages are ever changing,

but computational thinking is more persistent

Problem Algorithm Program

Run on

Computational

Thinking

Programming

Data (Star Trek)

Talk to computer?

Page 11: Lecture 2  introduction to python

11 of 46Module 2 : Introduction to Python

Why Python?Since

• Once you master computational thinking in a

language like Python, you can apply the same

problem-solving skill with other languages

• In real world, your supervisor will likely ask you

to use a computer language that you haven’t

learnt before… and you will likely have to learn

and use it in a tight schedule

Page 12: Lecture 2  introduction to python

12 of 46Module 2 : Introduction to Python

Why Python?Since…

• Python is simple but yet powerful and is a good

tool for rapid-prototyping programming

Hence, we can focus less on the language but

more on problem solving!!!

Problem Algorithm Program

Run onComputational

Thinking

Programming

Page 13: Lecture 2  introduction to python

13 of 46Module 2 : Introduction to Python

Same Algorithm: Python, C & Java

Python

Java

All of them ask user input for N,compute factorial of N, and then

print out the result

C

Page 14: Lecture 2  introduction to python

14 of 46Module 2 : Introduction to Python

Python 2 vs 3

• Python 3 is not backward compatible with 2

• Older libraries not compatible

• Python 2 was standard, Python 3 is 'shiny new'

• 2.7 is last Python version in v2 (July 2010)

• 3.2 released more recently (May 2011)

• 3.2 has a number of changes in the Python language compared to 2.7, e.g., print as a function, etc.http://wiki.python.org/moin/Python2orPython3

http://docs.python.org/release/3.1.5/whatsnew/3.0.html#

http://www.ibm.com/developerworks/linux/library/l-python3-1/

• Examples in our new textbook (2nd edition) now use Python 3 (other than this, contents are similar to 1st ed.)

In this course, we use version 3.2.3Check your own version for assignment!!!

Page 15: Lecture 2  introduction to python

15 of 46Module 2 : Introduction to Python

Topics

• What is Python?

• Download and install Python

• Running a Python program

• Interpreter VS Compiler

Page 16: Lecture 2  introduction to python

16 of 46Module 2 : Introduction to Python

Download

#1: Visit http://www.python.org/ and click DOWNLOAD

Page 17: Lecture 2  introduction to python

17 of 46Module 2 : Introduction to Python

Download

#2: After that, scroll down and you will see: Select theplatform of

your computer

and click on it

Page 18: Lecture 2  introduction to python

18 of 46Module 2 : Introduction to Python

Download and Install

#3: Save the installer file on your computer and double-

click to run the installer

Note: here is inmy windows 32-bit

platform (win XP)

Page 19: Lecture 2  introduction to python

19 of 46Module 2 : Introduction to Python

Install

#4: You will be prompted with a number of dialog boxes

1 2

Just a security check. No worryInstall for all? Up to you

Page 20: Lecture 2  introduction to python

20 of 46Module 2 : Introduction to Python

Install

#5: Install directory and customize the installation

3

Install to where? Up to youJust click “Next” if you don’t care

4

Page 21: Lecture 2  introduction to python

21 of 46Module 2 : Introduction to Python

Install

#6: Installation complete, and you will find:

5 6

Page 22: Lecture 2  introduction to python

22 of 46Module 2 : Introduction to Python

Topics

• What is Python?

• Download and install Python

• Running a Python program

• Interpreter VS Compiler

Page 23: Lecture 2  introduction to python

23 of 46Module 2 : Introduction to Python

What is a program?

Recall…

• A program is an implementation of an algorithm in a particular language

In more detail…

• A program consists of a sequence of instructions to perform an algorithm

Page 24: Lecture 2  introduction to python

24 of 46Module 2 : Introduction to Python

Example Python Program #1

• Hello World Program in Python

You may use your favorite text editor (or just use the IDLEeditor) to create a text file, type in the python instructions line by line, and then save it as in a file with file extension .py

Fourinstructions

only

Page 25: Lecture 2  introduction to python

25 of 46Module 2 : Introduction to Python

Use IDLE

• Start IDLE, which is a Python Graphical User Interface

(assume you’ve already installed it)

Page 26: Lecture 2  introduction to python

26 of 46Module 2 : Introduction to Python

Use IDLE to load & run program

• And you will see. Then, select open under the File menu

Page 27: Lecture 2  introduction to python

27 of 46Module 2 : Introduction to Python

Use IDLE to load & run program

• Then, open helloclass.py in the dialog box and you

will see a pop up window that open the python program

you created in the text editor.

So… You may actually open an existing python program

like this, or start a new python program here with IDLE

run it!!!

Page 28: Lecture 2  introduction to python

28 of 46Module 2 : Introduction to Python

Use IDLE to load & run program

• After you click on “Run Module,” go back to the initial Python Shell window you have; you will find the

program output (messages print out) here. In fact,

program print out normally will be displayed here.

Print

out

Page 29: Lecture 2  introduction to python

29 of 46Module 2 : Introduction to Python

Use IDLE to run program directly

• In fact, you may actually type your program line by line into Python shell (or copy and paste), you will see:

The print out is displayed immediately after each

print instruction in the Python program… Why?

Print

out

Page 30: Lecture 2  introduction to python

30 of 46Module 2 : Introduction to Python

Why? Interpreted

• This tells us two characteristics about Python:

Printout

First, Python is an interpreted language (like Java, etc.)

• When we run a Python program, the system translates

each line of Python instruction into machine code that

the CPU can understand and run on -> Interpreted!!!

• Hence, the printed message “Hello Class!” was printed

immediately after the corresponding print instruction

Page 31: Lecture 2  introduction to python

31 of 46Module 2 : Introduction to Python

Why? Interactive

Second, Python is an interactive language

• We can type in commands in the Shell and the commands

can be incrementally and interactively run in the Shell

• Let’s continue the program…

(see next slide)

Print

out

Page 32: Lecture 2  introduction to python

32 of 46Module 2 : Introduction to Python

Why? Interactive

More

print out

Here we add one more instruction that combines the two strings

“str1” and “str1”; then, we print out the result!!! Here we can

actually read back the values of the strings!!!

One more

instruction

(Note: you will learn this + operator for string later in this course: module 8)

Page 33: Lecture 2  introduction to python

33 of 46Module 2 : Introduction to Python

Why? Interactive

Let’s trymore

Like aninteractive

calculator

Ok with Python 2but not Python 3

Need parenthesis

Page 34: Lecture 2  introduction to python

34 of 46Module 2 : Introduction to Python

Note

• Using IDLE, if you save the file without a .py suffix, it will stop colorizing and formatting the file.

• Resave with the .py, everything is fine

• Shortcuts in IDLE:

<ctrl>+<space> Auto complete

<alt>+<p> Recall previous command

<alt>+<n> Back to next command

(for recalling history)

Page 35: Lecture 2  introduction to python

35 of 46Module 2 : Introduction to Python

Example Python Program #2

• Computing Factorial in Python:

Readinput

Display

result

The

algorithm

Note: you will learn detail on if, else, print, for, etc. later

Page 36: Lecture 2  introduction to python

36 of 46Module 2 : Introduction to Python

Same Algorithm in C

• More initialization and formality are required in C compared to Python

Page 37: Lecture 2  introduction to python

37 of 46Module 2 : Introduction to Python

Same Algorithm in Java

• How about Java? Similar to C…

As mentioned earlier, Python can allow simple code

so that we can focus more on problem solving

Page 38: Lecture 2  introduction to python

38 of 46Module 2 : Introduction to Python

Topics

• What is Python?

• Download and install Python

• Running a Python program

• Interpreter VS Compiler

Page 39: Lecture 2  introduction to python

39 of 46Module 2 : Introduction to Python

Machine Language

• Different CPUs (Central Processing Unit)

have different instruction sets, defining

different languages, called the

machine language

• Machine languages are

• usually referred to as low level languages which are

not possible for human to directly program (because they are bits and bytes, not human readable)

• machine dependent, so we cannot take machine code executable on one platform and run it on

elsewhere, e.g., Mac -> Windows

Page 40: Lecture 2  introduction to python

40 of 46Module 2 : Introduction to Python

Machine Language

• To run a program on a computer, the program

instructions must be in the corresponding

machine language so that the instructions can

be understood and run by the CPU

• But… we write programs in Python, C, Java,

etc. So… how to???

MachineCode

run

Page 41: Lecture 2  introduction to python

41 of 46Module 2 : Introduction to Python

Two possible approaches

Approach #1: Compiler [translate first and run later]

• First translate the entire program code in a high level

language such as C into machine code (by compiler)

• Store the resulting translated machine code as a file

• When we need to run the program, just execute the machine code file

MachineCode

run

Store as a file

compileProgramCode in C

Page 42: Lecture 2  introduction to python

42 of 46Module 2 : Introduction to Python

Two possible approaches

Approach #2: Interpreter [translate and run together]

• Repeat the followings for each program instruction

• We can translate an instruction in the program code (such as Python) to machine code,

• and execute the translated instruction immediately

So… interpreter appears to run the high-level code directly

MachineCode

run

Interpreter

translatePython

Program

Page 43: Lecture 2  introduction to python

43 of 46Module 2 : Introduction to Python

Python – Interactive and Interpreted

Page 44: Lecture 2  introduction to python

44 of 46Module 2 : Introduction to Python

Compiler VS Interpreter

• Compiler

• Translation is done off-line (before execution)

• Execution is usually fast because no need to do

code translation (compiled already)

• Machine code can be optimized (takes more time)

• Interpreter

• Translation is done online (interlaced with execution)

• Can be interactive (better for experimenting)

• Execution is usually slower (need code translation)

• Program execution can be cross-platform (for C, the

machine code file cannot)

Page 45: Lecture 2  introduction to python

45 of 46Module 2 : Introduction to Python

Take Home Messages

• Python is a simple, interpreted, and interactive programming language that allows rapid prototyping and experiments

• Hence, it allows us to focus more on computational thinking and problem solving

• Make sure you use version 3.2.3

• Ask yourself the difference and pros & cons between:– program code VS machine code

– interpreter VS compiler

For your interest: how about Java? … more complicated

Page 46: Lecture 2  introduction to python

46 of 46Module 2 : Introduction to Python

Reading Assignment

• Textbook

Chapter 0: The Study of Computer Science

0.3, 0.5 to 0.9

Chapter 1: Beginnings

1.1

Note: Though some material in textbook is not

directly related to the lecture material, you can

learn more from them.