SlideShare a Scribd company logo
Unit 1
Basic Python programs, functions
Special 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.0
2
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)
3
Installing Python
Windows:
• 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 python
or 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.
4
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
Computer
Runtime
Environment
Compiler
Code
Java:
Computer
Interpreter
Code
Python:
5
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
6
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!")
7
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
2
3
4
print("Hello, world!")
print()
print("Suppose two swallows "carry" it together.")
print('African or "European" swallows?')
8
Comments
• Syntax:
# comment text (one line)
swallows2.py
1
2
3
4
5
6
# Suzy Student, CSE 142, Fall 2097
# This program prints important messages.
print("Hello, world!")
print() # blank line
print("Suppose two swallows "carry" it together.")
print('African or "European" swallows?')
9
Functions
• Function: Equivalent to a static method in Java.
• Syntax:
def name():
statement
statement
...
statement
 Must be declared above the 'main' code
 Statements inside the function must be indented
hello2.py
1
2
3
4
5
6
7
# Prints a helpful message.
def hello():
print("Hello, world!")
# main (calls hello twice)
hello()
hello()
10
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
2
3
4
5
6
7
8
# Prints a helpful message.
def hello():
print("Hello, world!")
print("How are you?")
# main (calls hello twice)
hello()
hello()
11
Exercise
• Rewrite the Figures lecture program in Python. Its output:
______
/ 
/ 
 /
______/
 /
______/
+--------+
______
/ 
/ 
| STOP |
 /
______/
______
/ 
/ 
+--------+
12
Exercise Solution
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(" / ")
print("/ ")
def bottom():
print(" /")
print(" ______/")
def line():
print("+--------+")
# main
egg()
cup()
stop()
hat()

More Related Content

PPT
week1.ppt
PPT
python installation in different Operating Systems.ppt
PDF
week1.pdf
PDF
01. Introduction to Python.pdf yessir sirski
DOCX
Computer Science 111 Computer Science I with Java and Pyth.docx
PPT
Unit 1: Primitive Types - Basic Java Syntax
PPT
Java Primitive Type from Long Nguyen's lecture
PDF
java 1 new.pdf
week1.ppt
python installation in different Operating Systems.ppt
week1.pdf
01. Introduction to Python.pdf yessir sirski
Computer Science 111 Computer Science I with Java and Pyth.docx
Unit 1: Primitive Types - Basic Java Syntax
Java Primitive Type from Long Nguyen's lecture
java 1 new.pdf

Similar to DS basics functions.ppt (20)

PPT
IntroductionToJavaProgrammingxcvxcvx.ppt
PPT
Python Programming
PPTX
Python_Introduction_Good_PPT.pptx
PDF
Java language: a gentle introduction
PPT
Java introduction
PPTX
python intro and installation.pptx
PPTX
ITC 110 Week 10 Introdution to Python .pptx
PPT
python-ppt.ppt
PPT
python-ppt.ppt
PPTX
PyCourse - Self driving python course
PPTX
An Introduction To Python - Python, Print()
PPT
PythonCourse_01_Intro.ppt Python introduction turorial for beginner.
PPT
Python introduction turorial for beginner.
PDF
Fundamentals of python
PPTX
Introduction to Python Programming
PDF
05 python.pdf
PDF
Java Programming
PDF
Introduction to python 3
DOCX
Seminar report on python 3 course
PDF
How java works
IntroductionToJavaProgrammingxcvxcvx.ppt
Python Programming
Python_Introduction_Good_PPT.pptx
Java language: a gentle introduction
Java introduction
python intro and installation.pptx
ITC 110 Week 10 Introdution to Python .pptx
python-ppt.ppt
python-ppt.ppt
PyCourse - Self driving python course
An Introduction To Python - Python, Print()
PythonCourse_01_Intro.ppt Python introduction turorial for beginner.
Python introduction turorial for beginner.
Fundamentals of python
Introduction to Python Programming
05 python.pdf
Java Programming
Introduction to python 3
Seminar report on python 3 course
How java works
Ad

More from DrBashirMSaad (14)

PDF
googlechromedevelopertools-120424180007-phpapp01.pdf
PDF
Pico-R3-A4 Pinout for ARM embedded system
PDF
Arabic Fonts character foreigners for learner
PPTX
Algorithm analysis.pptx
PDF
Searching Informed Search.pdf
PPT
procress and threads.ppt
PPTX
2.02.Data_structures_and_algorithms (1).pptx
PDF
1b-150720094704-lva1-app6892.pdf
PPT
01_intro-cpp.ppt
PPT
Lecture 7-BinarySearchTrees.ppt
PPT
lec06-programming.ppt
PPT
CS351-L1.ppt
PPT
Matlab_Simulink_Tutorial.ppt
PDF
002 AWSSlides.pdf
googlechromedevelopertools-120424180007-phpapp01.pdf
Pico-R3-A4 Pinout for ARM embedded system
Arabic Fonts character foreigners for learner
Algorithm analysis.pptx
Searching Informed Search.pdf
procress and threads.ppt
2.02.Data_structures_and_algorithms (1).pptx
1b-150720094704-lva1-app6892.pdf
01_intro-cpp.ppt
Lecture 7-BinarySearchTrees.ppt
lec06-programming.ppt
CS351-L1.ppt
Matlab_Simulink_Tutorial.ppt
002 AWSSlides.pdf
Ad

Recently uploaded (20)

PDF
Lymphatic System MCQs & Practice Quiz – Functions, Organs, Nodes, Ducts
PPTX
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PDF
Placing the Near-Earth Object Impact Probability in Context
PDF
Biophysics 2.pdffffffffffffffffffffffffff
PDF
lecture 2026 of Sjogren's syndrome l .pdf
PPTX
famous lake in india and its disturibution and importance
PPTX
INTRODUCTION TO EVS | Concept of sustainability
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PDF
The scientific heritage No 166 (166) (2025)
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PDF
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
PPTX
2Systematics of Living Organisms t-.pptx
PDF
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
PPT
protein biochemistry.ppt for university classes
PDF
HPLC-PPT.docx high performance liquid chromatography
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PDF
An interstellar mission to test astrophysical black holes
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
Lymphatic System MCQs & Practice Quiz – Functions, Organs, Nodes, Ducts
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
Placing the Near-Earth Object Impact Probability in Context
Biophysics 2.pdffffffffffffffffffffffffff
lecture 2026 of Sjogren's syndrome l .pdf
famous lake in india and its disturibution and importance
INTRODUCTION TO EVS | Concept of sustainability
POSITIONING IN OPERATION THEATRE ROOM.ppt
The scientific heritage No 166 (166) (2025)
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
The KM-GBF monitoring framework – status & key messages.pptx
2Systematics of Living Organisms t-.pptx
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
protein biochemistry.ppt for university classes
HPLC-PPT.docx high performance liquid chromatography
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
An interstellar mission to test astrophysical black holes
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg

DS basics functions.ppt

  • 1. Unit 1 Basic Python programs, functions Special 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.0
  • 2. 2 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)
  • 3. 3 Installing Python Windows: • 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 python or 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.
  • 4. 4 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 Computer Runtime Environment Compiler Code Java: Computer Interpreter Code Python:
  • 5. 5 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
  • 6. 6 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!")
  • 7. 7 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 2 3 4 print("Hello, world!") print() print("Suppose two swallows "carry" it together.") print('African or "European" swallows?')
  • 8. 8 Comments • Syntax: # comment text (one line) swallows2.py 1 2 3 4 5 6 # Suzy Student, CSE 142, Fall 2097 # This program prints important messages. print("Hello, world!") print() # blank line print("Suppose two swallows "carry" it together.") print('African or "European" swallows?')
  • 9. 9 Functions • Function: Equivalent to a static method in Java. • Syntax: def name(): statement statement ... statement  Must be declared above the 'main' code  Statements inside the function must be indented hello2.py 1 2 3 4 5 6 7 # Prints a helpful message. def hello(): print("Hello, world!") # main (calls hello twice) hello() hello()
  • 10. 10 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 2 3 4 5 6 7 8 # Prints a helpful message. def hello(): print("Hello, world!") print("How are you?") # main (calls hello twice) hello() hello()
  • 11. 11 Exercise • Rewrite the Figures lecture program in Python. Its output: ______ / / / ______/ / ______/ +--------+ ______ / / | STOP | / ______/ ______ / / +--------+
  • 12. 12 Exercise Solution 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(" / ") print("/ ") def bottom(): print(" /") print(" ______/") def line(): print("+--------+") # main egg() cup() stop() hat()