Week 1
Week 1
• Steve Geluso
– Computer Science
– Self-proclaimed Moon Mayor
– [email protected]
2
Words you will learn today
python
print()
#
def
3
Python!
• Created in 1991 by Guido van Rossum (now at Google)
– Named for Monty Python
• Used by:
– Google, Yahoo!, Youtube
– Many Linux distributions
– Games and apps (e.g. Eve Online)
4
Python is used everywhere!
5
Interpreted Languages
• Interpreted
– Not compiled like Java
– Code is written and then directly executed by an interpreter
– Type commands into interpreter and see immediate results
Java: Runtime
Code Compiler Computer
Environment
6
Installing Python
Windows: Mac OS X:
• Download Python from • Python is already installed.
http://www.python.org • Open a terminal and run python
• Install Python. or run Idle from Finder.
• Run Idle from the Start Menu.
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.
7
The 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+P
8
How to run Python Windows
• Run IDLE to use the interpret
• Open a new window in IDLE to write and save programs
9
How to run Python Unix
• Start the interactive shell with python
• Run a program with python /path/to/program.py
10
Chapter 1 Review
• Console output: System.out.println();
• Methods: public static void name() { ... }
Hello2.java
11
Our 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 semicolons
hello.py
1 print("Hello, world!")
12
A Brief Review
13
Python 2.x vs Python 3.x
– We will be using Python 3 for this course
– Sometimes we may refer to Python 2
– The differences are minimal
14
The print Statement
print(”text")
print() (a blank line)
– Escape sequences such as \" are the same as in Java
– Strings can also start/end with '
swallows.py
15
Comments
# comment text (one line)
# must start each line of comments with the pound sign
swallows2.py
16
Functions
• Function: Equivalent to a static method in Java.
• Syntax:
hello2.py
def name():
statement 1 # Prints a helpful message.
2 def hello():
statement 3 print("Hello, world!")
... 4
5 # main (calls hello twice)
statement 6 hello()
7 hello()
17
Whitespace 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.
– You may use either tabs or spaces, but you must be
consistent
hello3.py
18
Tabs or spaces
shell
19
Tabs or spaces
shell
20
Exercise
• Rewrite the Figures lecture program in Python. Its output:
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| SPAM |
\ /
\______/
______
/ \
/ \
+--------+
21
Exercise Solution
def egg(): def top():
top() print(" ______”)
bottom() print(" / \\”)
print() print("/ \\”)
22