Python & Perl: Lecture 02

1,278 views
1,211 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,278
On SlideShare
0
From Embeds
0
Number of Embeds
488
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python & Perl: Lecture 02

  1. 1. Python & Perl Lecture 02 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Editing Python Code ● Running Python Code ● Sample Programs ● Python Code Execution ● Functional Abstraction ● Tupleswww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. Review ● Pythons Strengths, Weaknesses, Features ● Python 2.X vs. Python 3.X ● Python Installation ● Python Interpreterwww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Editing Python Codewww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. White Space Matters ● .py files are plain text files so, in principle, you can use any text editor ● HOWEVER, in Python white space matters! ● Indentation, not curly braces, are used to define code blocks ● Tabs or spaces are used to indent (mixing them can sometimes get you into trouble) ● It is HIGHLY recommended that you use an editor that supports Pythonwww.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. When In Doubt, Stay IDLE ● IDLE stands for Integrated DeveLopment Environment ● Comes with many standard Python installs ● Supports indentation ● Coded in 100% Python ● Cross-platform: works on Windows/Linux/Unixwww.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. Running Python Codewww.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Sample Program: FACT.PY ● Let us create a Python file, fact.py, with the following code and run it on Windows/Linux/Unix: def fact(n): if n == 0 or n == 1: return 1 else: return n * fact(n-1) print fact(5)www.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Python from Windows Command Line ● After you install Python on Windows, the PATH variable does not change. ● If you want to run Python from the command line (cmd): – Type the full path to python.exe: ● C:python27python.exe fact.py ● C:pythoin27python fact.py – Or: Add C:python27 (this is the path to the directory with python.exe) to PATHwww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. Python from Linux Command Line ● Python interpreter on Linux is called python (do not look for python.exe) ● Most likely it is installed in one of the two directories: – /usr/bin/ – /usr/local/bin/ ● To find out where it is installed, type which python at command linewww.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Python on Mac ● Same as on Linux ● Start the UNIX terminal ● Use the Linux commands to find out where Python interpreter is located ● Type python fact.pywww.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Python Programs as Scripts on Windows ● Windows registers the .py , .pyc , and .pyw extensions, which allows you to run these files as scripts from the command line ● Suppose you have a file C:codedebugger.py and you want to run it from the command line ● CD into C:code and then type the script name to run it with .debugger.py right and press Enter/Return: ● C:PythonFiles>.debugger.pywww.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. Python Programs as Scripts on Windows ● Sometimes when you attempt to run a Python program as a script from the Windows command line (e.g., C:Pythonfact.py), the file is opened in an editor (e.g. Wordpad) ● The fix is to associate .py extension with Python: – Right click on the file – Choose Properties – Click “Change” next to “Opens with” prompt – Select Python from the list of optionswww.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Double Clicking on Windows ● If you want to make your programs clickable on Windows (execute on double click), you need to add raw_input(“Press <Enter>”) at the end: def fact(n): if n == 0 or n == 1: return 1 else: return n * fact(n-1) print fact(5) raw_input(“Press <Enter>”)www.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Python Programs as Scripts on Linux/Unix ● Place #!/usr/bin/python at the beginning of your file ● Type the script name at the prompt ($) with ./ right before it and press Enter: ● $ ./fact.py ● If you get the “permission denied” error, do: ● $ chmod u+x fact.py ● If you have a shebang in the first line, you can still run the file with the interpreter at the command line: ● $ pyhon fact.pywww.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Sample Program: FIB.PY ● Let us create a Python file, fib.py, with the following code and run it on Windows/Linux/Unix: def fact(n): if n == 0 or n == 1: return 1 else: return n * fact(n-1) print fact(5)www.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. def fib(n): Sample Program: FACT_FIB.PY if n == 0 or n == 1: return 1 else: return fib(n-1) + fib(n-2) def fact(n): if n == 0 or n == 1: return 1 else: return n * fact(n-1) def fib_fact(n): return fib(fact(n)) def fact_fib(n): return fact(fib(n))www.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. Python Code Executionwww.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. Python Virtual Machine (PVM) ● The Python Virtual Machine (PVM) runs the byte code ● The PVM operates on the same principles as the Java Virtual Machine (JVM) ● The byte code is not Assembly, which is why it can be slower then C/C++ which compile directly into Assemblywww.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. How Python Runs Programs ● The interpreter compiles your programs into byte code ● Byte code is platform independent, similar to Java bytecode ● The PVM is platform dependent ● In some cases, the interpreter will compile your source into the .pyc file ● If the .pyc file is newer than the source .py file, Python will run .pyc ● If the .pyc file is older, the .py file is recompiledwww.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. Functional Abstraction Newtons Square Root Approximationwww.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Next Guess To compute n , start with some initial guess g 0 . The next guess is computed : n g i− 1 + g i− 1 gi = ,i > 0 2 The i - th guess is good enough when n − ( g i ) ≤ ε , where ε 2 is some small value.www.youtube.com/vkedco www.vkedco.blogspot.com
  23. 23. Next Guess: Example Suppose we want to compute 2 and start with the initial guess g 0 = 1. The next guess is computed : 2 1+ g1 = 1 = 1.5 2 2 1.5 + g2 = 1.5 = 1.4167 2 etc.www.youtube.com/vkedco www.vkedco.blogspot.com
  24. 24. Next Guess: Implementation def square(n): return n * n def average(x, y): return (x + y)/2.0 def next_guess(prev_guess, n): return average(prev_guess, n/prev_guess) def is_good_enough(guess, n, error): return abs(n - square(guess)) <= errorwww.youtube.com/vkedco www.vkedco.blogspot.com
  25. 25. Newtons Square Root: Pseudocode ● Compute a guess (e.g., 1) ● If the guess is good enough, return it ● If the guess is not good enough, go to step 1www.youtube.com/vkedco www.vkedco.blogspot.com
  26. 26. Newtons Square Root: Implementation def newton_square_root(guess, n, error): if is_good_enough(guess, n, error): return guess else: return newton_square_root(next_guess(guess, n), n, error) def nsqrt(n): return newton_square_root(1, n, 0.0001)www.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. Tupleswww.youtube.com/vkedco www.vkedco.blogspot.com
  28. 28. Tuples: Creation ● Tuples are immutable sequences; they are defined with ( ) >>> t1 = () ## t1 is an empty tuple >>> t1 () >>> t2 = (1, 2, 3) >>> t2 (1, 2, 3)www.youtube.com/vkedco www.vkedco.blogspot.com
  29. 29. Tuples: Element Access ● Individual elements are accessed with [ ]; item as- signment is not supported: >>> t2 = (1, 2, 3) >>> t2[1] 2 >>> t2[1] = 100 errorwww.youtube.com/vkedco www.vkedco.blogspot.com
  30. 30. Reading & References ● www.python.org. ● Ch. 01, M. L. HetLand. Beginning Python From Novice to nd Professional, ,2 Ed., APRESS. ● H. Abelson and J. Sussman. Structure and Interpretation of Computer Programs, 2nd Ed., MIT Press.www.youtube.com/vkedco www.vkedco.blogspot.com
  31. 31. Feedback Errors, bugs, comments to vladimir dot kulyukin at usu dot eduwww.youtube.com/vkedco www.vkedco.blogspot.com

×