0% found this document useful (0 votes)
25 views

Python 01-Basics-Functions

This document provides an introduction and overview of the Python programming language. It discusses that Python was created in 1991 and is an interpreted scripting language used for small to medium projects. The document then covers installing Python on different operating systems, how Python code is interpreted rather than compiled, the Python interpreter, writing basic "Hello World" programs using print statements and comments, defining functions, the significance of whitespace in Python, and provides an exercise to rewrite a sample Java program in Python.

Uploaded by

Tse Chris
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python 01-Basics-Functions

This document provides an introduction and overview of the Python programming language. It discusses that Python was created in 1991 and is an interpreted scripting language used for small to medium projects. The document then covers installing Python on different operating systems, how Python code is interpreted rather than compiled, the Python interpreter, writing basic "Hello World" programs using print statements and comments, defining functions, the significance of whitespace in Python, and provides an exercise to rewrite a sample Java program in Python.

Uploaded by

Tse Chris
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Unit 1

Basic Python programs, functions


By Alex Cheung
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)

2
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.

3
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

4
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

5
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!")

6
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?')

7
Comments
• Syntax:
# comment text (one line)

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?')

8
Functions
• Function: Equivalent to a static method in Java.

hello2.py
• Syntax:
1 # Prints a helpful message.
def name(): 2 def hello():
3 print("Hello, world!")
statement 4
statement 5 # main (calls hello twice)
6 hello()
... 7 hello()
statement

 Must be declared above the 'main' code


 Statements inside the function must be indented

9
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.

hello3.py
1 # Prints a helpful message.
2 def hello():
3 print("Hello, world!")
4 print("How are you?")
5
6 # main (calls hello twice)
7 hello()
8 hello()

10
Exercise
• Rewrite the Figures lecture program in Python. Its output:
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+

11
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("| STOP |") # main
bottom() egg()
print() cup()
stop()
def hat(): hat()
top()
line()
print()

12

You might also like