0% found this document useful (0 votes)
1 views46 pages

Lecture2introductiontopython 130525013928 Phpapp01

Module 2 introduces Python as a simple, open-source programming language designed for computational thinking and problem solving. It covers installation, running programs, and the differences between interpreters and compilers. The module emphasizes Python's characteristics, such as readability and rapid prototyping, making it suitable for various applications including web development and AI.

Uploaded by

samuela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views46 pages

Lecture2introductiontopython 130525013928 Phpapp01

Module 2 introduces Python as a simple, open-source programming language designed for computational thinking and problem solving. It covers installation, running programs, and the differences between interpreters and compilers. The module emphasizes Python's characteristics, such as readability and rapid prototyping, making it suitable for various applications including web development and AI.

Uploaded by

samuela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Introduction to

Computational Thinking
Module 2 :
Introduction to Python

Asst Prof Chi-Wing FU, Philip


Office: N4-02c-104
Module 2 : Introduction to Python email: cwfu[at]ntu.edu.sg
1 of 46
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler

Module 2 : Introduction to Python 2 of 46


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/

Module 2 : Introduction to Python 3 of 46


Origin of its name
• Named after Monty Python’s Flying Circus,
which is a BBC TV sketch comedy series
broadcasted from 1969 to 1974

Module 2 : Introduction to Python 4 of 46


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)

Module 2 : Introduction to Python 5 of 46


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
……

Module 2 : Introduction to Python 6 of 46


Characteristics of Python
• Designed to be simple yet powerful
• Great emphasis on readability (indentation)
• Rapid prototyping * Allows fast coding!!!

• 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!

Module 2 : Introduction to Python 7 of 46


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

Module 2 : Introduction to Python 8 of 46


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!

Module 2 : Introduction to Python 9 of 46


Why Python?
Since
• Programming languages are ever changing,
but computational thinking is more persistent

Problem Algorithm Program

Computational Programming
Thinking Run on

Data (Star Trek)


Module 2 : Introduction to Python
Talk to computer?
10 of 46
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

Module 2 : Introduction to Python 11 of 46


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

Computational Programming
Thinking Run on

Module 2 : Introduction to Python 12 of 46


Same Algorithm: Python, C & Java
All of them ask user input for N,
Python compute factorial of N, and then
print out the result

C
Java

Module 2 : Introduction to Python 13 of 46


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.3


Check your own version for assignment!!!
Module 2 : Introduction to Python 14 of 46
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler

Module 2 : Introduction to Python 15 of 46


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

Module 2 : Introduction to Python 16 of 46


Download
#2: After that, scroll down and you will see: Select the
platform of
your computer
and click on it

Module 2 : Introduction to Python 17 of 46


Download and Install
#3: Save the installer file on your computer and double-
click to run the installer

Note: here is in
my windows 32-bit
platform (win XP)

Module 2 : Introduction to Python 18 of 46


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

1 2

Just a security check. No worry


Install for all? Up to you
Module 2 : Introduction to Python 19 of 46
Install
#5: Install directory and customize the installation

4
3

Install to where? Up to you


Just click “Next” if you don’t care
Module 2 : Introduction to Python 20 of 46
Install
#6: Installation complete, and you will find:

5 6

Module 2 : Introduction to Python 21 of 46


Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler

Module 2 : Introduction to Python 22 of 46


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

Module 2 : Introduction to Python 23 of 46


Example Python Program #1
• Hello World Program in Python

Four
instructions
only

You may use your favorite text editor (or just use the IDLE
editor) 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
Module 2 : Introduction to Python 24 of 46
Use IDLE
• Start IDLE, which is a Python Graphical User Interface
(assume you’ve already installed it)

Module 2 : Introduction to Python 25 of 46


Use IDLE to load & run program
• And you will see. Then, select open under the File menu

Module 2 : Introduction to Python 26 of 46


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.

run it!!!

So… You may actually open an existing python program


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

Module 2 : Introduction to Python 27 of 46


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

Module 2 : Introduction to Python 28 of 46


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:

Print
out

The print out is displayed immediately after each


print instruction in the Python program… Why?
Module 2 : Introduction to Python 29 of 46
Why? Interpreted
• This tells us two characteristics about Python:

Print
out

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
Module 2 : Introduction to Python 30 of 46
Why? Interactive

Print
out

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)

Module 2 : Introduction to Python 31 of 46


Why? Interactive

One more
instruction

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!!!
(Note: you will learn this + operator for string later in this course: module 8)

Module 2 : Introduction to Python 32 of 46


Why? Interactive

Let’s try
more

Like an
interactive Ok with Python 2
calculator but not Python 3
Need parenthesis

Module 2 : Introduction to Python 33 of 46


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)
Module 2 : Introduction to Python 34 of 46
Example Python Program #2
• Computing Factorial in Python:

Read
input

The
algorithm

Display Note: you will learn detail on if, else, print, for, etc. later
result Module 2 : Introduction to Python 35 of 46
Same Algorithm in C
• More initialization and formality are required in C
compared to Python

Module 2 : Introduction to Python 36 of 46


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
Module 2 : Introduction to Python 37 of 46
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler

Module 2 : Introduction to Python 38 of 46


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

Module 2 : Introduction to Python 39 of 46


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
run
Machine
Code

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


etc. So… how to???

Module 2 : Introduction to Python 40 of 46


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

compile run
Program Machine
Code in C Code

Store as a file

Module 2 : Introduction to Python 41 of 46


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

translate run
Python Machine
Program Code

Interpreter
Module 2 : Introduction to Python 42 of 46
Python – Interactive and Interpreted

Module 2 : Introduction to Python 43 of 46


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)

Module 2 : Introduction to Python 44 of 46


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

Module 2 : Introduction to Python 45 of 46


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.

Module 2 : Introduction to Python 46 of 46

You might also like