DOC PREVIEW
UW CSE 142 - Basic Python programs Functions

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Unit 1Basic Python programs, functionsSpecial thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.Except where otherwise noted, this work is licensed under:http://creativecommons.org/licenses/by-nc-sa/3.02Python!• Created in 1991 by Guido van Rossum (now at Google) Named for Monty Python• Useful as a scripting language script: A small program meant for one-time use Targeted towards small to medium sized projects• Used by: Google, Yahoo!, Youtube Many Linux distributions Games and apps (e.g. Eve Online)3Installing PythonWindows:• Download Python from http://www.python.org• Install Python.• Run Idle from the Start Menu.Mac OS X:• Python is already installed.• Open a terminal and run pythonor run Idle from Finder.Linux:• Chances are you already have Python installed. To check, run python from the terminal.• If not, install from your distribution's package system.Note: For step by step installation instructions, see the course web site.4Interpreted Languages• interpreted Not compiled like Java Code is written and then directly executed by an interpreter Type commands into interpreter and see immediate resultsComputerRuntimeEnvironmentCompilerCodeJava:ComputerInterpreterCodePython:5The Python Interpreter• Allows you to type commands one-at-a-time and see results• A great way to explore Python's syntax Repeat previous command: Alt+P6Our First Python Program• Python does not have a main method like Java The program's main code is just written directly in the file• Python statements do not end with semicolonshello.py1 print("Hello, world!")7The print Statementprint("text")print() (a blank line) Escape sequences such as \" are the same as in Java Strings can also start/end with 'swallows.py1234print("Hello, world!")print()print("Suppose two swallows \"carry\" it together.")print('African or "European" swallows?')8Comments• Syntax:# comment text (one line)swallows2.py123456# Suzy Student, CSE 142, Fall 2097# This program prints important messages.print("Hello, world!")print() # blank lineprint("Suppose two swallows \"carry\" it together.")print('African or "European" swallows?')9Functions• Function: Equivalent to a static method in Java.• Syntax:def name():statementstatement...statement Must be declared above the 'main' code Statements inside the function must be indentedhello2.py1234567# Prints a helpful message.def hello():print("Hello, world!")# main (calls hello twice)hello()hello()10Whitespace Significance• Python uses indentation to indicate blocks, instead of {} Makes the code simpler and more readable In Java, indenting is optional. In Python, you must indent.hello3.py12345678# Prints a helpful message.def hello():print("Hello, world!")print("How are you?")# main (calls hello twice)hello()hello()11Exercise• Rewrite the Figures lecture program in Python. Its output:______/ \/ \\ /\______/\ /\______/+--------+______/ \/ \| STOP |\ /\______/______/ \/ \+--------+12Exercise Solutiondef 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(" / \\")print("/ \\")def bottom():print("\\ /")print(" \\______/")def line():print("+--------+")#


View Full Document

UW CSE 142 - Basic Python programs Functions

Download Basic Python programs Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Basic Python programs Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Basic Python programs Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?