Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Lecture 2 introduction to python
1. 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
2. 2 of 46Module 2 : Introduction to Python
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler
3. 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/
4. 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
5. 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)
6. 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
……
7. 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!!!
8. 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
9. 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!
10. 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?
11. 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
12. 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 on
Computational
Thinking
Programming
13. 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
14. 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.3
Check your own version for assignment!!!
15. 15 of 46Module 2 : Introduction to Python
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler
16. 16 of 46Module 2 : Introduction to Python
Download
#1: Visit http://www.python.org/ and click DOWNLOAD
17. 17 of 46Module 2 : Introduction to Python
Download
#2: After that, scroll down and you will see: Select the
platform of
your computer
and click on it
18. 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 in
my windows 32-bit
platform (win XP)
19. 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 worry
Install for all? Up to you
20. 20 of 46Module 2 : Introduction to Python
Install
#5: Install directory and customize the installation
3
Install to where? Up to you
Just click “Next” if you don’t care
4
21. 21 of 46Module 2 : Introduction to Python
Install
#6: Installation complete, and you will find:
5 6
22. 22 of 46Module 2 : Introduction to Python
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler
23. 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
24. 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 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
Four
instructions
only
25. 25 of 46Module 2 : Introduction to Python
Use IDLE
• Start IDLE, which is a Python Graphical User Interface
(assume you’ve already installed it)
26. 26 of 46Module 2 : Introduction to Python
Use IDLE to load & run program
• And you will see. Then, select open under the File menu
27. 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!!!
28. 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
29. 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
30. 30 of 46Module 2 : Introduction to Python
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
31. 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
32. 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)
33. 33 of 46Module 2 : Introduction to Python
Why? Interactive
Let’s try
more
Like an
interactive
calculator
Ok with Python 2
but not Python 3
Need parenthesis
34. 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)
35. 35 of 46Module 2 : Introduction to Python
Example Python Program #2
• Computing Factorial in Python:
Read
input
Display
result
The
algorithm
Note: you will learn detail on if, else, print, for, etc. later
36. 36 of 46Module 2 : Introduction to Python
Same Algorithm in C
• More initialization and formality are required in C
compared to Python
37. 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
38. 38 of 46Module 2 : Introduction to Python
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler
39. 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
40. 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???
Machine
Code
run
41. 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
Machine
Code
run
Store as a file
compile
Program
Code in C
42. 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
Machine
Code
run
Interpreter
translate
Python
Program
43. 43 of 46Module 2 : Introduction to Python
Python – Interactive and Interpreted
44. 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)
45. 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
46. 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.