146x Filetype PDF File size 0.51 MB Source: mcsp.wartburg.edu
What is Python? Python: A free, portable, dynamically-typed, object-oriented scripting language. Teaching Computer Science with Python Combines software engineering features of traditional systems languages with power and flexibility of scripting languages. Workshop #4 SIGCSE 2003 Note: Named after Monty Python’s Flying Circus John M. Zelle Wartburg College Outline Why Python? Why Python? Educational Apps Traditional languages (C++, Java) evolved for large-scale Functional Programming Basic Structures GUIs programming I/O Graphics Emphasis on structure and discipline Assignment Internet Programming Simple problems != simple programs Numbers Databases Strings Op Systems Functions Others Decisions Scripting languages (Perl, Python, TCL) designed for Loops Python Resources Collections simplicity and flexibility. Software Engineering Simple problems = simple, elegant solutions Modules/Packages More amenable to experimentation and incremental development Classes Exceptions Library Python: Ideal first language, useful throughout curriculum First Program (Java Version) Running Python Programs Assignment: Print "Hello SIGCSE" on screen Hybrid compiled/interpreted architecture public class Hello{ Options: public static void main(String args){ Start Interpreter from command line (>>> ) System.out.println("Hello SIGCSE"); Type program statements } Import script file } Start interpreter with file as command line arg Note: Must be in "Hello.java" Configure filetype to launch interpreter on file Unix pound-bang trick Directly from IDE (IDLE) First Program (Python Version) "Real" Program: Chaos.py Assignment: Print "Hello SIGCSE" on screen #File: chaos.py print "Hello SIGCSE" # A simple program illustrating chaotic behavior. def main(): print "This program illustrates a chaotic function" Or... x = input("Enter a number between 0 and 1: ") for i in range(10): x = 3.9 * x * (1 - x) def main(): print x print "Hello SIGCSE" main() main() Python Features Basic Output Statement Comment convention "#" to end of line print, , ..., Nesting indicated by indentation Notes: Statements terminated by end of line Prints expressions on one line Explicit continuation with backslash Implicit continuation to match parens Successive values separated by a space No variable declarations Advances to next line (unless comma follows) For loop iterates through a sequence All Python built-in types have printable reps Example Output Assignment Statements $ python chaos.py Simple Assignment This program illustrates a chaotic function = Enter a number between 0 and 1: .5 myVar = oldValue * foo + skip 0.975 0.0950625 0.335499922266 Simultaneous Assignment 0.869464925259 0.442633109113 , , ... = , , ... 0.962165255337 a,b = b,a 0.141972779362 0.4750843862 0.972578927537 0.104009713267 Assigning Input $ input( ) myVar = input("Enter a number: ") x,y = input("Enter the coordinates (x,y): ") Example Program: Fibonacci Teaching Tip: Indentation as Syntax Pluses # fibonacci.py less code clutter (; and {}) # This program computes the nth Fibonacci number eliminates most common syntax errors promotes and teaches proper code layout n = input("Enter value of n ") cur,prev = 1,1 Minuses for i in range(n-2): occasional subtle error from inconsistent spacing cur,prev = prev+cur,cur will want an indentation-aware editor print "The nth Fibonacci number is", cur Bottom-line: Good Python editors abound. This is my favorite feature. Teaching Tip: Variable Declarations Numeric Types Pluses int: Standard 32 bit integer less code 32 -3432 0 less upfront explanation eliminates "redeclaration" errors long int: Indefinitely long integers Minuses 32L 9999999999999999 typo on LHS of = creates new variable allows variables to change type floating-point: Standard double-precision float 3.14 2.57e-10 5E210 -3.64e+210 Bottom-line: I prefer dynamic types complex: Double precision real and imaginary components 2+3j 4.7J -3.5 + 4.3e-4j User-defined types (operator overloading)
no reviews yet
Please Login to review.