0% found this document useful (0 votes)
155 views22 pages

Week 1

The document provides an overview of Week 1 topics in a Python course, including: - Installing Python and running it on different platforms - Using the Python interpreter to run commands and see results - Writing Python programs with print statements, comments, functions, and proper indentation - An exercise to rewrite a figures drawing program from a lecture in Python

Uploaded by

Wesley Falcão
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views22 pages

Week 1

The document provides an overview of Week 1 topics in a Python course, including: - Installing Python and running it on different platforms - Using the Python interpreter to run commands and see results - Writing Python programs with print statements, comments, functions, and proper indentation - An exercise to rewrite a figures drawing program from a lecture in Python

Uploaded by

Wesley Falcão
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Week 1

basic Python programs,


defining functions
Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides.
Except where otherwise noted, this work is licensed under:
http://creativecommons.org/licenses/by-nc-sa/3.0
About Us
• Stefanie Hatcher • Will Beebe
– Informatics – Informatics
– Grandmistress Leet Queen – IPL Hobo
[email protected][email protected]

• 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

• 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)

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

Python: Code Interpreter Computer

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

1 public class Hello2 {


2 public static void main(String[] args) {
3 hello();
4 }
5
6 public static void hello() {
7 System.out.println("Hello, world!");
8 }
9 }

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

How to Python 2.x Python 3.x


print text print “text” print(“text”)
print a blank print print()
line

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

1 print("Hello, world! ")


2 print()
3 print("Suppose two swallows \"carry\" it together.")
4 print('African or "European" swallows?')

15
Comments
# comment text (one line)
# must start each line of comments with the pound sign

swallows2.py

1 # Suzy Student, CSE 142, Fall 2097


2 # This program prints important messages.
3 print("Hello, world!")
4 Print() # blank line
5 print("Suppose two swallows \"carry\" it together.")
6 Print('African or "European" swallows?')

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()

– Must be declared above the 'main' code


– Statements inside the function must be indented

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

1 # Prints a welcoming message.


2 def hello():
3 print("Hello, world!")
4 print("How are you?")
5
6 # main (calls hello twice)
7 hello()
8 hello()

18
Tabs or spaces
shell

1 # Prints a helpful message.


2 >>> def indent_reminder():
3 ... print("Remember, you must indent!")
4 File "<stdin>", line 2
5 print("Remember, you must indent!")
6 ^
7 IndentationError: expected an indented block
8 >>> def indent_reminder():
9 ... print("Remember, you must indent!")
10 ...
11 >>> indent_reminder()
12 Remember, you must indent!
>>>

19
Tabs or spaces
shell

1 # Prints a helpful message.


2 >>> def indententation_errors():
3 ... print("this was indented using a tab")
4 ... print("this was indented using four spaces")
5 File "<stdin>", line 3
6 print("this was indented using four spaces")
7 ^
8 IndentationError: unindent does not match any outer
9 indentation level
10 >>> def indententation_errors():
11 ... print("this was indented using a tab")
12 ... print("so this must also use a tab")
...
>>> def more_indentation_tricks():
... print("If I use spaces to indent here.")
... print("then I must use spaces to indent here.”)
>>>

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("/ \\”)

def cup(): def bottom():


bottom() print("\\ /”)
line() print(" \\______/”)
print()
def line():
def stop(): print("+--------+”)
top()
print("| SPAM |”) # main
bottom() egg()
print() cup()
stop()
def hat(): hat()
top()
line()
print()

22

You might also like