SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Downloaden Sie, um offline zu lesen
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 of 46Module 2 : Introduction to Python
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler
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 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 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 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 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 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 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 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 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 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 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 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 of 46Module 2 : Introduction to Python
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler
16 of 46Module 2 : Introduction to Python
Download
#1: Visit http://www.python.org/ and click DOWNLOAD
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 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 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 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 of 46Module 2 : Introduction to Python
Install
#6: Installation complete, and you will find:
5 6
22 of 46Module 2 : Introduction to Python
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler
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 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 of 46Module 2 : Introduction to Python
Use IDLE
• Start IDLE, which is a Python Graphical User Interface
(assume you’ve already installed it)
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 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 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 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 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 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 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 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 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 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 of 46Module 2 : Introduction to Python
Same Algorithm in C
• More initialization and formality are required in C
compared 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
38 of 46Module 2 : Introduction to Python
Topics
• What is Python?
• Download and install Python
• Running a Python program
• Interpreter VS Compiler
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 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 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 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 of 46Module 2 : Introduction to Python
Python – Interactive and Interpreted
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 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 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.

Weitere ähnliche Inhalte

Was ist angesagt?

Python-00 | Introduction and installing
Python-00 | Introduction and installingPython-00 | Introduction and installing
Python-00 | Introduction and installingMohd Sajjad
 
What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.Nicholas Pringle
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming LanguageR.h. Himel
 
An Introduction to Python Programming
An Introduction to Python ProgrammingAn Introduction to Python Programming
An Introduction to Python ProgrammingMorteza Zakeri
 
Python Crash Course
Python Crash CoursePython Crash Course
Python Crash CourseHaim Michael
 
Numeric Data types in Python
Numeric Data types in PythonNumeric Data types in Python
Numeric Data types in Pythonjyostna bodapati
 
Myanmar Named Entity Recognition with Hidden Markov Model
Myanmar Named Entity Recognition with Hidden Markov ModelMyanmar Named Entity Recognition with Hidden Markov Model
Myanmar Named Entity Recognition with Hidden Markov Modelijtsrd
 
11 Unit 1 Chapter 02 Python Fundamentals
11  Unit 1 Chapter 02 Python Fundamentals11  Unit 1 Chapter 02 Python Fundamentals
11 Unit 1 Chapter 02 Python FundamentalsPraveen M Jigajinni
 
Python Interview questions 2020
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020VigneshVijay21
 
Presentation on python data type
Presentation on python data typePresentation on python data type
Presentation on python data typeswati kushwaha
 
Machine Learning Techniques in Python Dissertation - Phdassistance
Machine Learning Techniques in Python Dissertation - PhdassistanceMachine Learning Techniques in Python Dissertation - Phdassistance
Machine Learning Techniques in Python Dissertation - PhdassistancePhD Assistance
 
Summer Training Project On Python Programming
Summer Training Project On Python ProgrammingSummer Training Project On Python Programming
Summer Training Project On Python ProgrammingKAUSHAL KUMAR JHA
 

Was ist angesagt? (20)

Python-00 | Introduction and installing
Python-00 | Introduction and installingPython-00 | Introduction and installing
Python-00 | Introduction and installing
 
What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.
 
Python45 2
Python45 2Python45 2
Python45 2
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming Language
 
Python Data Types
Python Data TypesPython Data Types
Python Data Types
 
An Introduction to Python Programming
An Introduction to Python ProgrammingAn Introduction to Python Programming
An Introduction to Python Programming
 
Unit1 pps
Unit1 ppsUnit1 pps
Unit1 pps
 
Programming
ProgrammingProgramming
Programming
 
Python Session - 2
Python Session - 2Python Session - 2
Python Session - 2
 
Obj report
Obj reportObj report
Obj report
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Python Crash Course
Python Crash CoursePython Crash Course
Python Crash Course
 
Numeric Data types in Python
Numeric Data types in PythonNumeric Data types in Python
Numeric Data types in Python
 
Myanmar Named Entity Recognition with Hidden Markov Model
Myanmar Named Entity Recognition with Hidden Markov ModelMyanmar Named Entity Recognition with Hidden Markov Model
Myanmar Named Entity Recognition with Hidden Markov Model
 
11 Unit 1 Chapter 02 Python Fundamentals
11  Unit 1 Chapter 02 Python Fundamentals11  Unit 1 Chapter 02 Python Fundamentals
11 Unit 1 Chapter 02 Python Fundamentals
 
Python Interview questions 2020
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Presentation on python data type
Presentation on python data typePresentation on python data type
Presentation on python data type
 
Machine Learning Techniques in Python Dissertation - Phdassistance
Machine Learning Techniques in Python Dissertation - PhdassistanceMachine Learning Techniques in Python Dissertation - Phdassistance
Machine Learning Techniques in Python Dissertation - Phdassistance
 
Summer Training Project On Python Programming
Summer Training Project On Python ProgrammingSummer Training Project On Python Programming
Summer Training Project On Python Programming
 

Ähnlich wie Lecture 2 introduction to python

CrashCourse: Python with DataCamp and Jupyter for Beginners
CrashCourse: Python with DataCamp and Jupyter for BeginnersCrashCourse: Python with DataCamp and Jupyter for Beginners
CrashCourse: Python with DataCamp and Jupyter for BeginnersOlga Scrivner
 
PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python courseEran Shlomo
 
Introduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdfIntroduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdfVaibhavKumarSinghkal
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Niraj Bharambe
 
PYTHO programming NOTES with category.pdf
PYTHO programming NOTES with category.pdfPYTHO programming NOTES with category.pdf
PYTHO programming NOTES with category.pdfdeivasigamani9
 
PYTHON PROGRAMMING NOTES.pdf
PYTHON PROGRAMMING NOTES.pdfPYTHON PROGRAMMING NOTES.pdf
PYTHON PROGRAMMING NOTES.pdfRajathShetty34
 
Introduction to python 3
Introduction to python 3Introduction to python 3
Introduction to python 3Youhei Sakurai
 
Introduction to Python.pdf
Introduction to Python.pdfIntroduction to Python.pdf
Introduction to Python.pdfRahul Mogal
 
Programming with Python: Week 1
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1Ahmet Bulut
 
Python tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyPython tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyTIB Academy
 
Python Programmimg language in Gurugram
Python  Programmimg language in GurugramPython  Programmimg language in Gurugram
Python Programmimg language in Gurugramdigitallynikitasharm
 

Ähnlich wie Lecture 2 introduction to python (20)

Python lecture 01
Python lecture 01Python lecture 01
Python lecture 01
 
CrashCourse: Python with DataCamp and Jupyter for Beginners
CrashCourse: Python with DataCamp and Jupyter for BeginnersCrashCourse: Python with DataCamp and Jupyter for Beginners
CrashCourse: Python with DataCamp and Jupyter for Beginners
 
PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python course
 
Introduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdfIntroduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdf
 
Introduction to python3.pdf
Introduction to python3.pdfIntroduction to python3.pdf
Introduction to python3.pdf
 
Class 1 python.pptx
Class 1 python.pptxClass 1 python.pptx
Class 1 python.pptx
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
PHYTON-REPORT.pdf
PHYTON-REPORT.pdfPHYTON-REPORT.pdf
PHYTON-REPORT.pdf
 
PYTHO programming NOTES with category.pdf
PYTHO programming NOTES with category.pdfPYTHO programming NOTES with category.pdf
PYTHO programming NOTES with category.pdf
 
PYTHON PROGRAMMING NOTES.pdf
PYTHON PROGRAMMING NOTES.pdfPYTHON PROGRAMMING NOTES.pdf
PYTHON PROGRAMMING NOTES.pdf
 
python unit2.pptx
python unit2.pptxpython unit2.pptx
python unit2.pptx
 
Introduction to python 3
Introduction to python 3Introduction to python 3
Introduction to python 3
 
Introduction to Python.pdf
Introduction to Python.pdfIntroduction to Python.pdf
Introduction to Python.pdf
 
Programming with Python: Week 1
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1
 
Python tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyPython tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academy
 
Python Programmimg language in Gurugram
Python  Programmimg language in GurugramPython  Programmimg language in Gurugram
Python Programmimg language in Gurugram
 

Mehr von alvin567

Make hyperlink
Make hyperlinkMake hyperlink
Make hyperlinkalvin567
 
Lecture 12 exceptions
Lecture 12  exceptionsLecture 12  exceptions
Lecture 12 exceptionsalvin567
 
Lecture 10 user defined functions and modules
Lecture 10  user defined functions and modulesLecture 10  user defined functions and modules
Lecture 10 user defined functions and modulesalvin567
 
Lecture 9 composite types
Lecture 9  composite typesLecture 9  composite types
Lecture 9 composite typesalvin567
 
Lecture 8 strings and characters
Lecture 8  strings and charactersLecture 8  strings and characters
Lecture 8 strings and charactersalvin567
 
Lecture 7 program development issues (supplementary)
Lecture 7  program development issues (supplementary)Lecture 7  program development issues (supplementary)
Lecture 7 program development issues (supplementary)alvin567
 
Lecture 6.2 flow control repetition
Lecture 6.2  flow control repetitionLecture 6.2  flow control repetition
Lecture 6.2 flow control repetitionalvin567
 
Lecture 6.1 flow control selection
Lecture 6.1  flow control selectionLecture 6.1  flow control selection
Lecture 6.1 flow control selectionalvin567
 
Lecture 5 numbers and built in functions
Lecture 5  numbers and built in functionsLecture 5  numbers and built in functions
Lecture 5 numbers and built in functionsalvin567
 
Lecture 1 computing and algorithms
Lecture 1  computing and algorithmsLecture 1  computing and algorithms
Lecture 1 computing and algorithmsalvin567
 
Lecture 11 file management
Lecture 11  file managementLecture 11  file management
Lecture 11 file managementalvin567
 

Mehr von alvin567 (11)

Make hyperlink
Make hyperlinkMake hyperlink
Make hyperlink
 
Lecture 12 exceptions
Lecture 12  exceptionsLecture 12  exceptions
Lecture 12 exceptions
 
Lecture 10 user defined functions and modules
Lecture 10  user defined functions and modulesLecture 10  user defined functions and modules
Lecture 10 user defined functions and modules
 
Lecture 9 composite types
Lecture 9  composite typesLecture 9  composite types
Lecture 9 composite types
 
Lecture 8 strings and characters
Lecture 8  strings and charactersLecture 8  strings and characters
Lecture 8 strings and characters
 
Lecture 7 program development issues (supplementary)
Lecture 7  program development issues (supplementary)Lecture 7  program development issues (supplementary)
Lecture 7 program development issues (supplementary)
 
Lecture 6.2 flow control repetition
Lecture 6.2  flow control repetitionLecture 6.2  flow control repetition
Lecture 6.2 flow control repetition
 
Lecture 6.1 flow control selection
Lecture 6.1  flow control selectionLecture 6.1  flow control selection
Lecture 6.1 flow control selection
 
Lecture 5 numbers and built in functions
Lecture 5  numbers and built in functionsLecture 5  numbers and built in functions
Lecture 5 numbers and built in functions
 
Lecture 1 computing and algorithms
Lecture 1  computing and algorithmsLecture 1  computing and algorithms
Lecture 1 computing and algorithms
 
Lecture 11 file management
Lecture 11  file managementLecture 11  file management
Lecture 11 file management
 

Kürzlich hochgeladen

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 

Kürzlich hochgeladen (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
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.