1 of 46Module 2 : Introduction to PythonIntroduction toComputational ThinkingModule 2 :Introduction to PythonAsst Prof Chi...
2 of 46Module 2 : Introduction to PythonTopics• What is Python?• Download and install Python• Running a Python program• In...
3 of 46Module 2 : Introduction to PythonWhat is Python?• A programming language(a computer language designed to express co...
4 of 46Module 2 : Introduction to PythonOrigin of its name• Named after Monty Python’s Flying Circus,which is a BBC TV ske...
5 of 46Module 2 : Introduction to PythonSome Useful Websites• Python - Official Website:http://www.python.org/• Beginner’s...
6 of 46Module 2 : Introduction to PythonUse of Python• Web Development• Google app engine and search spiders• Facebook (To...
7 of 46Module 2 : Introduction to PythonCharacteristics of Python• Designed to be simple yet powerful• Great emphasis on r...
8 of 46Module 2 : Introduction to PythonCharacteristics of PythonMoreover…• High productivity in program development• Lots...
9 of 46Module 2 : Introduction to PythonWhy Python?There are so many programming languages!!!• Fortran, C, C++, Java, Perl...
10 of 46Module 2 : Introduction to PythonWhy Python?Since• Programming languages are ever changing,but computational think...
11 of 46Module 2 : Introduction to PythonWhy Python?Since• Once you master computational thinking in alanguage like Python...
12 of 46Module 2 : Introduction to PythonWhy Python?Since…• Python is simple but yet powerful and is a goodtool for rapid-...
13 of 46Module 2 : Introduction to PythonSame Algorithm: Python, C & JavaPythonJavaAll of them ask user input for N,comput...
14 of 46Module 2 : Introduction to PythonPython 2 vs 3• Python 3 is not backward compatible with 2• Older libraries not co...
15 of 46Module 2 : Introduction to PythonTopics• What is Python?• Download and install Python• Running a Python program• I...
16 of 46Module 2 : Introduction to PythonDownload#1: Visit http://www.python.org/ and click DOWNLOAD
17 of 46Module 2 : Introduction to PythonDownload#2: After that, scroll down and you will see: Select theplatform ofyour c...
18 of 46Module 2 : Introduction to PythonDownload and Install#3: Save the installer file on your computer and double-click...
19 of 46Module 2 : Introduction to PythonInstall#4: You will be prompted with a number of dialog boxes1 2Just a security c...
20 of 46Module 2 : Introduction to PythonInstall#5: Install directory and customize the installation3Install to where? Up ...
21 of 46Module 2 : Introduction to PythonInstall#6: Installation complete, and you will find:5 6
22 of 46Module 2 : Introduction to PythonTopics• What is Python?• Download and install Python• Running a Python program• I...
23 of 46Module 2 : Introduction to PythonWhat is a program?Recall…• A program is an implementation of analgorithm in a par...
24 of 46Module 2 : Introduction to PythonExample Python Program #1• Hello World Program in PythonYou may use your favorite...
25 of 46Module 2 : Introduction to PythonUse IDLE• Start IDLE, which is a Python Graphical User Interface(assume you’ve al...
26 of 46Module 2 : Introduction to PythonUse IDLE to load & run program• And you will see. Then, select open under the Fil...
27 of 46Module 2 : Introduction to PythonUse IDLE to load & run program• Then, open helloclass.py in the dialog box and yo...
28 of 46Module 2 : Introduction to PythonUse IDLE to load & run program• After you click on “Run Module,” go back to the i...
29 of 46Module 2 : Introduction to PythonUse IDLE to run program directly• In fact, you may actually type your program lin...
30 of 46Module 2 : Introduction to PythonWhy? Interpreted• This tells us two characteristics about Python:PrintoutFirst, P...
31 of 46Module 2 : Introduction to PythonWhy? InteractiveSecond, Python is an interactive language• We can type in command...
32 of 46Module 2 : Introduction to PythonWhy? InteractiveMoreprint outHere we add one more instruction that combines the t...
33 of 46Module 2 : Introduction to PythonWhy? InteractiveLet’s trymoreLike aninteractivecalculatorOk with Python 2but not ...
34 of 46Module 2 : Introduction to PythonNote• Using IDLE, if you save the file withouta .py suffix, it will stop colorizi...
35 of 46Module 2 : Introduction to PythonExample Python Program #2• Computing Factorial in Python:ReadinputDisplayresultTh...
36 of 46Module 2 : Introduction to PythonSame Algorithm in C• More initialization and formality are required in Ccompared ...
37 of 46Module 2 : Introduction to PythonSame Algorithm in Java• How about Java? Similar to C…As mentioned earlier, Python...
38 of 46Module 2 : Introduction to PythonTopics• What is Python?• Download and install Python• Running a Python program• I...
39 of 46Module 2 : Introduction to PythonMachine Language• Different CPUs (Central Processing Unit)have different instruct...
40 of 46Module 2 : Introduction to PythonMachine Language• To run a program on a computer, the programinstructions must be...
41 of 46Module 2 : Introduction to PythonTwo possible approachesApproach #1: Compiler [translate first and run later]• Fir...
42 of 46Module 2 : Introduction to PythonTwo possible approachesApproach #2: Interpreter [translate and run together]• Rep...
43 of 46Module 2 : Introduction to PythonPython – Interactive and Interpreted
44 of 46Module 2 : Introduction to PythonCompiler VS Interpreter• Compiler• Translation is done off-line (before execution...
45 of 46Module 2 : Introduction to PythonTake Home Messages• Python is a simple, interpreted, and interactiveprogramming l...
46 of 46Module 2 : Introduction to PythonReading Assignment• TextbookChapter 0: The Study of Computer Science0.3, 0.5 to 0...
Upcoming SlideShare
Loading in …5
×

Lecture 2 introduction to python

942 views

Published on

Published in: Technology, Education
  • Be the first to comment

Lecture 2 introduction to python

  1. 1. 1 of 46Module 2 : Introduction to PythonIntroduction toComputational ThinkingModule 2 :Introduction to PythonAsst Prof Chi-Wing FU, PhilipOffice: N4-02c-104email: cwfu[at]ntu.edu.sg
  2. 2. 2 of 46Module 2 : Introduction to PythonTopics• What is Python?• Download and install Python• Running a Python program• Interpreter VS Compiler
  3. 3. 3 of 46Module 2 : Introduction to PythonWhat is Python?• A programming language(a computer language designed to express computationsthat 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. 4 of 46Module 2 : Introduction to PythonOrigin of its name• Named after Monty Python’s Flying Circus,which is a BBC TV sketch comedy seriesbroadcasted from 1969 to 1974
  5. 5. 5 of 46Module 2 : Introduction to PythonSome Useful Websites• Python - Official Website:http://www.python.org/• Beginner’s Guide to Pythonhttp://wiki.python.org/moin/BeginnersGuide• Common Pitfall for Beginners:http://wiki.python.org/moin/BeginnerErrorsWithPythonProgramming (you may visit it after you learn more Python)
  6. 6. 6 of 46Module 2 : Introduction to PythonUse 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. 7 of 46Module 2 : Introduction to PythonCharacteristics 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, andturns 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. 8 of 46Module 2 : Introduction to PythonCharacteristics of PythonMoreover…• 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. 9 of 46Module 2 : Introduction to PythonWhy 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. 10 of 46Module 2 : Introduction to PythonWhy Python?Since• Programming languages are ever changing,but computational thinking is more persistentProblem Algorithm ProgramRun onComputationalThinkingProgrammingData (Star Trek)Talk to computer?
  11. 11. 11 of 46Module 2 : Introduction to PythonWhy Python?Since• Once you master computational thinking in alanguage like Python, you can apply the sameproblem-solving skill with other languages• In real world, your supervisor will likely ask youto use a computer language that you haven’tlearnt before… and you will likely have to learnand use it in a tight schedule
  12. 12. 12 of 46Module 2 : Introduction to PythonWhy Python?Since…• Python is simple but yet powerful and is a goodtool for rapid-prototyping programmingHence, we can focus less on the language butmore on problem solving!!!Problem Algorithm ProgramRun onComputationalThinkingProgramming
  13. 13. 13 of 46Module 2 : Introduction to PythonSame Algorithm: Python, C & JavaPythonJavaAll of them ask user input for N,compute factorial of N, and thenprint out the resultC
  14. 14. 14 of 46Module 2 : Introduction to PythonPython 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 languagecompared to 2.7, e.g., print as a function, etc.http://wiki.python.org/moin/Python2orPython3http://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 usePython 3 (other than this, contents are similar to 1st ed.)In this course, we use version 3.2.3Check your own version for assignment!!!
  15. 15. 15 of 46Module 2 : Introduction to PythonTopics• What is Python?• Download and install Python• Running a Python program• Interpreter VS Compiler
  16. 16. 16 of 46Module 2 : Introduction to PythonDownload#1: Visit http://www.python.org/ and click DOWNLOAD
  17. 17. 17 of 46Module 2 : Introduction to PythonDownload#2: After that, scroll down and you will see: Select theplatform ofyour computerand click on it
  18. 18. 18 of 46Module 2 : Introduction to PythonDownload and Install#3: Save the installer file on your computer and double-click to run the installerNote: here is inmy windows 32-bitplatform (win XP)
  19. 19. 19 of 46Module 2 : Introduction to PythonInstall#4: You will be prompted with a number of dialog boxes1 2Just a security check. No worryInstall for all? Up to you
  20. 20. 20 of 46Module 2 : Introduction to PythonInstall#5: Install directory and customize the installation3Install to where? Up to youJust click “Next” if you don’t care4
  21. 21. 21 of 46Module 2 : Introduction to PythonInstall#6: Installation complete, and you will find:5 6
  22. 22. 22 of 46Module 2 : Introduction to PythonTopics• What is Python?• Download and install Python• Running a Python program• Interpreter VS Compiler
  23. 23. 23 of 46Module 2 : Introduction to PythonWhat is a program?Recall…• A program is an implementation of analgorithm in a particular languageIn more detail…• A program consists of a sequence ofinstructions to perform an algorithm
  24. 24. 24 of 46Module 2 : Introduction to PythonExample Python Program #1• Hello World Program in PythonYou may use your favorite text editor (or just use the IDLEeditor) to create a text file, type in the python instructionsline by line, and then save it as in a file with file extension .pyFourinstructionsonly
  25. 25. 25 of 46Module 2 : Introduction to PythonUse IDLE• Start IDLE, which is a Python Graphical User Interface(assume you’ve already installed it)
  26. 26. 26 of 46Module 2 : Introduction to PythonUse IDLE to load & run program• And you will see. Then, select open under the File menu
  27. 27. 27 of 46Module 2 : Introduction to PythonUse IDLE to load & run program• Then, open helloclass.py in the dialog box and youwill see a pop up window that open the python programyou created in the text editor.So… You may actually open an existing python programlike this, or start a new python program here with IDLErun it!!!
  28. 28. 28 of 46Module 2 : Introduction to PythonUse IDLE to load & run program• After you click on “Run Module,” go back to the initialPython Shell window you have; you will find theprogram output (messages print out) here. In fact,program print out normally will be displayed here.Printout
  29. 29. 29 of 46Module 2 : Introduction to PythonUse IDLE to run program directly• In fact, you may actually type your program line by lineinto Python shell (or copy and paste), you will see:The print out is displayed immediately after eachprint instruction in the Python program… Why?Printout
  30. 30. 30 of 46Module 2 : Introduction to PythonWhy? Interpreted• This tells us two characteristics about Python:PrintoutFirst, Python is an interpreted language (like Java, etc.)• When we run a Python program, the system translateseach line of Python instruction into machine code thatthe CPU can understand and run on -> Interpreted!!!• Hence, the printed message “Hello Class!” was printedimmediately after the corresponding print instruction
  31. 31. 31 of 46Module 2 : Introduction to PythonWhy? InteractiveSecond, Python is an interactive language• We can type in commands in the Shell and the commandscan be incrementally and interactively run in the Shell• Let’s continue the program…(see next slide)Printout
  32. 32. 32 of 46Module 2 : Introduction to PythonWhy? InteractiveMoreprint outHere we add one more instruction that combines the two strings“str1” and “str1”; then, we print out the result!!! Here we canactually read back the values of the strings!!!One moreinstruction(Note: you will learn this + operator for string later in this course: module 8)
  33. 33. 33 of 46Module 2 : Introduction to PythonWhy? InteractiveLet’s trymoreLike aninteractivecalculatorOk with Python 2but not Python 3Need parenthesis
  34. 34. 34 of 46Module 2 : Introduction to PythonNote• Using IDLE, if you save the file withouta .py suffix, it will stop colorizing andformatting 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. 35 of 46Module 2 : Introduction to PythonExample Python Program #2• Computing Factorial in Python:ReadinputDisplayresultThealgorithmNote: you will learn detail on if, else, print, for, etc. later
  36. 36. 36 of 46Module 2 : Introduction to PythonSame Algorithm in C• More initialization and formality are required in Ccompared to Python
  37. 37. 37 of 46Module 2 : Introduction to PythonSame Algorithm in Java• How about Java? Similar to C…As mentioned earlier, Python can allow simple codeso that we can focus more on problem solving
  38. 38. 38 of 46Module 2 : Introduction to PythonTopics• What is Python?• Download and install Python• Running a Python program• Interpreter VS Compiler
  39. 39. 39 of 46Module 2 : Introduction to PythonMachine Language• Different CPUs (Central Processing Unit)have different instruction sets, definingdifferent languages, called themachine language• Machine languages are• usually referred to as low level languages which arenot possible for human to directly program (becausethey are bits and bytes, not human readable)• machine dependent, so we cannot take machinecode executable on one platform and run it onelsewhere, e.g., Mac -> Windows
  40. 40. 40 of 46Module 2 : Introduction to PythonMachine Language• To run a program on a computer, the programinstructions must be in the correspondingmachine language so that the instructions canbe understood and run by the CPU• But… we write programs in Python, C, Java,etc. So… how to???MachineCoderun
  41. 41. 41 of 46Module 2 : Introduction to PythonTwo possible approachesApproach #1: Compiler [translate first and run later]• First translate the entire program code in a high levellanguage 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 themachine code fileMachineCoderunStore as a filecompileProgramCode in C
  42. 42. 42 of 46Module 2 : Introduction to PythonTwo possible approachesApproach #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 immediatelySo… interpreter appears to run the high-level code directlyMachineCoderunInterpretertranslatePythonProgram
  43. 43. 43 of 46Module 2 : Introduction to PythonPython – Interactive and Interpreted
  44. 44. 44 of 46Module 2 : Introduction to PythonCompiler VS Interpreter• Compiler• Translation is done off-line (before execution)• Execution is usually fast because no need to docode 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, themachine code file cannot)
  45. 45. 45 of 46Module 2 : Introduction to PythonTake Home Messages• Python is a simple, interpreted, and interactiveprogramming language that allows rapid prototypingand experiments• Hence, it allows us to focus more on computationalthinking 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 compilerFor your interest: how about Java? … more complicated
  46. 46. 46 of 46Module 2 : Introduction to PythonReading Assignment• TextbookChapter 0: The Study of Computer Science0.3, 0.5 to 0.9Chapter 1: Beginnings1.1Note: Though some material in textbook is notdirectly related to the lecture material, you canlearn more from them.

×