Presentation is loading. Please wait.

Presentation is loading. Please wait.

1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer.

Similar presentations


Presentation on theme: "1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer."— Presentation transcript:

1 1/10/2008

2 >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Email: rctucker@u.washington.edu Ryan Tucker * Second quarter TA * Computer Engineering & Electrical Engineering * Email: paulbeck@u.washington.edu

3 >>> Python Basics Interpreted Not compiled like Java This means: type commands and see results Targeted towards short to medium sized projects Useful as a scripting language A whaa? script – short program meant for one-time use. Compute r Runtime Env CompilerCode InterpreterCode

4 >>> Getting it Going Windows Mac OSX Linux 1) Download Python from python.org. 2) Run 'python' using the run command. -or- Run Idle from the Start Menu. 1) Python is already installed. 2) Open a terminal and run python or run Idle from finder. 1) Chances are you already have Pythoninstalled. To check run python from the terminal. 2) If not, install python through your distribution's package system. Note: For step by step installation instructions, follow those provided on the course web site. Click on Python on the right hand side and follow the “Python Installation Instructions”

5 >>> Topic Review * printing - System.out.println(); * methods – public static void ()... public class Hello { public static void main(String[] args){ System.out.println("Hello world!"); } Hello.java 1234512345 public class Hello { public static void main(String[] args){ hello(); } public static void hello(){ System.out.println("Hello world!"); } Hello.java 1234567812345678

6 >>> Missing Main print "Hello world!" hello.py 1234512345 public class Hello { public static void main(String[] args){ System.out.println("Hello world!"); } Hello.java 1234512345 The entire file is interpreted as if it was typed into the interpreter. This makes it easy to write scripts with Python.

7 >>> Printing public class Hello { public static void main(String[] args){ System.out.println("Hello world!"); System.out.println(); System.out.println("Suppose two swallows carry it together."); } Hello.java 1234567812345678 print "Hello world!" print print "Suppose two swallows carry it together." hello.py 12341234 Escape sequences: * \t – tab * \n – new line * \" - " * \\ - \

8 >>> Methods public class Hello { public static void main(String[] args){ hello(); } public static void hello(){ System.out.println("Hello \"world\"!"); } Hello.java 1234567812345678 def hello(): print "Hello \"world\"!" #main hello()‏ hello.py 1234512345 Note: Python does not have a main method. However, methods must be defined first. Calls to methods can then be made. This is similar to having a “main” section of code. It is good practice to label this section of code with a comment. Structure: def ():...

9 >>> Whitespace Unlike in Java, in Python whitespace (think tabs and spaces) matters. Instead of using braces ({}) to designate code blocks, Python uses the indentation. This was done to promote readable code. In Java you may or may not indent and it will work. In Python you must indent. public class Hello { public static void main(String[] args){ System.out.println(“Hello \"world\"!”); } Hello.java 1234512345 def hello(): print "Hello \"world\"!" hello()‏ hello.py 12341234

10 >>> Example 1 Example: Write a method that produces the following output. We are the Knights Who Say... "Nee!" (Nee! Nee! Nee!) NO! Not the Knights Who Say "Nee"... We are the Knights Who Say... "Nee!" (Nee! Nee! Nee!)

11 >>> Example 1 Solution: def knights(): print "We are the Knights Who Say... \"Nee!\"" print "(Nee! Nee! Nee!)" print def poorSouls(): print "NO!\t Not the Knights Who Say \"Nee\"..." print knights() poorSouls() knights()

12 >>> Example 2 Example: Write a method that produces the following output. ______ / \ \ / \______/ \ / \______/ +--------+ ______ / \ | STOP | \ / \______/ ______ / \ +--------+

13 >>> Example 2 Example: Write a method that produces the following output. ______ / \ \ / \______/ \ / \______/ +--------+ ______ / \ | STOP | \ / \______/ ______ / \ +--------+ def egg(): top() bottom() print def cup(): bottom() line() print def stop(): top() print "| STOP |" bottom() print def hat(): top() line() print def top(): print " ______" print " / \\" def bottom(): print "\\ /" print " \\______/" def line(): print "+--------+" egg() cup() stop() hat()

14 Except where otherwise noted, this work is licensed under http://creativecommons.org/licenses/by-nc-sa/3.0 © 2007 Scott Shawcroft, Some Rights Reserved Python® and the Python logo are either a registered trademark or trademark of the Python Software Foundation. Java™ is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.


Download ppt "1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer."

Similar presentations


Ads by Google