SlideShare a Scribd company logo
Unit 3
parameters and graphics
By Alex Cheung
2
Constants
• Python doesn't really have constants.
– Instead, declare a variable at the top of your code.
– All methods will be able to use this "constant" value.
constant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
MAX_VALUE = 3
def print_top():
for i in range(MAX_VALUE):
for j in range(i):
print(j)
print()
def print_bottom():
for i in range(MAX_VALUE, 0, -1):
for j in range(i, 0, -1):
print(MAX_VALUE)
print()
3
Exercise
• Rewrite the Mirror lecture program in Python. Its output:
#================#
| <><> |
| <>....<> |
| <>........<> |
|<>............<>|
|<>............<>|
| <>........<> |
| <>....<> |
| <><> |
#================#
– Make the mirror resizable by using a "constant."
4
Exercise Solution
SIZE = 4
def bar():
print("#" + 4 * SIZE * "=" + "#")
def top():
for line in range(1, SIZE + 1):
# split a long line by ending it with 
print("|" + (-2 * line + 2 * SIZE) * " " + 
"<>" + (4 * line - 4) * "." + "<>" + 
(-2 * line + 2 * SIZE) * " " + "|")
def bottom():
for line in range(SIZE, 0, -1):
print("|" + (-2 * line + 2 * SIZE) * " " + 
"<>" + (4 * line - 4) * "." + "<>" + 
(-2 * line + 2 * SIZE) * " " + "|")
# main
bar()
top()
bottom()
bar()
5
Parameters
def name(parameter, parameter, ..., parameter):
statements
– Parameters are declared by writing their names (no types)
>>> def print_many(message, n):
... for i in range(n):
... print(message)
>>> print_many("hello", 4)
hello
hello
hello
hello
6
Exercise
• Recreate the lines/boxes of stars example from lecture:
*************
*******
***********************************
**********
* *
**********
*****
* *
* *
*****
7
Exercise Solution
stars.py
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
# Draws a box of stars with the given width and
height.
def box(width, height):
print(width * "*")
for i in range(height - 2):
print("*" + (width - 2) * " " + "*")
print(width * "*")
# main
print(13 * "*")
print( 7 * "*")
print(35 * "*")
box(10, 3)
box(5, 4)
8
Default Parameter Values
def name(parameter=value, ..., parameter=value):
statements
– Can make parameter(s) optional by specifying a default value
– Exercise: Modify stars.py to add an optional parameter for
the character to use for the outline of the box (default "*").
>>> def print_many(message, n=1):
... for i in range(n):
... print(message)
>>> print_many("shrubbery")
shrubbery
>>> print_many("shrubbery", 3)
shrubbery
shrubbery
shrubbery
9
Parameter Keywords
name(parameter=value, ..., parameter=value)
– Can specify name of each parameter as you call a function
– This allows you to pass the parameters in any order
>>> def print_many(message, n):
... for i in range(n):
... print(message)
>>> print_many(str="shrubbery", n=4)
shrubbery
shrubbery
shrubbery
shrubbery
>>> print_many(n=3, str="Ni!")
Ni!
Ni!
Ni!
10
DrawingPanel
• Instructor-provided drawingpanel.py file must be in the
same folder as your Python program
• At the top of your program, write:
– from drawingpanel import *
• Panel's canvas field behaves like Graphics g in Java
11
DrawingPanel Example
draw1.py
1
2
3
4
5
from drawingpanel import *
panel = DrawingPanel(400, 300)
panel.set_background("yellow")
panel.canvas.create_rectangle(100, 50, 200, 300)
12
– Notice, methods take x2/y2 parameters, not width/height
Drawing Methods
Java Python
drawLine panel.canvas.create_line(x1, y1, x2, y2)
drawRect,
fillRect
panel.canvas.create_rect(x1, y1, x2, y2)
drawOval,
fillOval
panel.canvas.create_oval(x1, y1, x2, y2)
drawString panel.canvas.create_text(x, y, text="text")
setColor (see next slide)
setBackground panel.set_background(color)
13
Colors and Fill
• Python doesn't have fillRect, fillOval, or setColor.
– Instead, pass outline and fill colors when drawing a shape.
– List of all color names: http://wiki.tcl.tk/16166
– See class web site for visual index of colors!
drawcolors.py
1
2
3
4
5
from drawingpanel import *
panel = DrawingPanel(400, 300)
panel.canvas.create_rectangle(100, 50, 200, 200,
outline="red", fill="yellow")
panel.canvas.create_oval(20, 10, 180, 70, fill="blue")
14
Polygons
• Draw arbitrary polygons with create_polygon
• Draw line groups by passing more params to create_line
drawpoly.py
1
2
3
4
5
from drawingpanel import *
panel = DrawingPanel(200, 200)
panel.canvas.create_polygon(100, 50, 150, 0,
150, 100, fill="green")
panel.canvas.create_line(10, 120, 20, 160,
30, 120, 40, 175)
15
Exercise
• Write a Python version of the Car program.
– Convert this Java code to Python:
DrawingPanel panel = new DrawingPanel(200, 200);
panel.setBackground(Color.LIGHT_GRAY);
Graphics g = panel.getGraphics();
g.setColor(Color.BLACK); // body
g.fillRect(10, 30, 100, 50);
g.setColor(Color.RED); // wheels
g.fillOval(20, 70, 20, 20);
g.fillOval(80, 70, 20, 20);
g.setColor(Color.CYAN); // windshield
g.fillRect(80, 40, 30, 20);
16
Exercise
• Modify your car program to use parameters so that cars can
be drawn in many different locations.
17
Exercise
• Write a variation of the Car program
where the car body is octagonal
and there is a stop sign.
– Stop sign at (150, 10), size 40
• post at (165, 50), size 10x30, brown fill
– Write an octagon function to draw the car body / stop sign.
• Points of car body, located at (10, 10):
1. (10, 20), 2. (20, 10), 3. (100, 10), 4. (110, 20),
5. (110, 50), 6. (100, 60), 7. (20, 60), 8. (10, 50)
• Points of stop sign, located at (150, 10):
1. (150, 20), 2. (160, 10), 3. (180, 10), 4. (190, 20),
5. (190, 40), 6. (180, 50), 7. (160, 50), 8. (150, 40)
(An octagon has 10x10 triangular cuts in each corner.)
1
2 3
4
5
6
7
8
18
Animation
• Pause the panel by calling sleep
animation.py
1
2
3
4
5
6
7
8
9
from drawingpanel import *
panel = DrawingPanel(350, 300)
for i in range(20):
# clear any previous image
panel.canvas.create_rectangle(0, 0, 400, 400,
outline="white", fill="white")
panel.canvas.create_polygon(20 * i, 50, 20 * i,
100, 20 * i + 50, 75)
panel.sleep(100)
19
Exercise
• Animate the car to make it drive across the panel using the
sleep function.

More Related Content

Similar to Python 03-parameters-graphics.pptx

03-fortran.ppt
03-fortran.ppt03-fortran.ppt
03-fortran.ppt
thomashughes837337
 
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
FarhanAhmade
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Yashpatel821746
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Yashpatel821746
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
Yashpatel821746
 
Astronomical data analysis by python.pdf
Astronomical data analysis by python.pdfAstronomical data analysis by python.pdf
Astronomical data analysis by python.pdf
ZainRahim3
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
rohassanie
 
Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make
YASHU40
 
Pres_python_talakhoury_26_09_2023.pdf
Pres_python_talakhoury_26_09_2023.pdfPres_python_talakhoury_26_09_2023.pdf
Pres_python_talakhoury_26_09_2023.pdf
RamziFeghali
 
CDMA simulation code for wireless Network.docx
CDMA simulation code for wireless Network.docxCDMA simulation code for wireless Network.docx
CDMA simulation code for wireless Network.docx
MaryamAziz47
 
Array Cont
Array ContArray Cont
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
hhliu
 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
actexerode
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
Mahmud Hasan Tanvir
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPython
Ross McDonald
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to Coding
Fabio506452
 

Similar to Python 03-parameters-graphics.pptx (20)

03-fortran.ppt
03-fortran.ppt03-fortran.ppt
03-fortran.ppt
 
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
 
Astronomical data analysis by python.pdf
Astronomical data analysis by python.pdfAstronomical data analysis by python.pdf
Astronomical data analysis by python.pdf
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make
 
Pres_python_talakhoury_26_09_2023.pdf
Pres_python_talakhoury_26_09_2023.pdfPres_python_talakhoury_26_09_2023.pdf
Pres_python_talakhoury_26_09_2023.pdf
 
CDMA simulation code for wireless Network.docx
CDMA simulation code for wireless Network.docxCDMA simulation code for wireless Network.docx
CDMA simulation code for wireless Network.docx
 
Array Cont
Array ContArray Cont
Array Cont
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPython
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to Coding
 

Recently uploaded

Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 

Recently uploaded (20)

Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 

Python 03-parameters-graphics.pptx

  • 1. Unit 3 parameters and graphics By Alex Cheung
  • 2. 2 Constants • Python doesn't really have constants. – Instead, declare a variable at the top of your code. – All methods will be able to use this "constant" value. constant.py 1 2 3 4 5 6 7 8 9 10 11 12 13 MAX_VALUE = 3 def print_top(): for i in range(MAX_VALUE): for j in range(i): print(j) print() def print_bottom(): for i in range(MAX_VALUE, 0, -1): for j in range(i, 0, -1): print(MAX_VALUE) print()
  • 3. 3 Exercise • Rewrite the Mirror lecture program in Python. Its output: #================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================# – Make the mirror resizable by using a "constant."
  • 4. 4 Exercise Solution SIZE = 4 def bar(): print("#" + 4 * SIZE * "=" + "#") def top(): for line in range(1, SIZE + 1): # split a long line by ending it with print("|" + (-2 * line + 2 * SIZE) * " " + "<>" + (4 * line - 4) * "." + "<>" + (-2 * line + 2 * SIZE) * " " + "|") def bottom(): for line in range(SIZE, 0, -1): print("|" + (-2 * line + 2 * SIZE) * " " + "<>" + (4 * line - 4) * "." + "<>" + (-2 * line + 2 * SIZE) * " " + "|") # main bar() top() bottom() bar()
  • 5. 5 Parameters def name(parameter, parameter, ..., parameter): statements – Parameters are declared by writing their names (no types) >>> def print_many(message, n): ... for i in range(n): ... print(message) >>> print_many("hello", 4) hello hello hello hello
  • 6. 6 Exercise • Recreate the lines/boxes of stars example from lecture: ************* ******* *********************************** ********** * * ********** ***** * * * * *****
  • 7. 7 Exercise Solution stars.py 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 # Draws a box of stars with the given width and height. def box(width, height): print(width * "*") for i in range(height - 2): print("*" + (width - 2) * " " + "*") print(width * "*") # main print(13 * "*") print( 7 * "*") print(35 * "*") box(10, 3) box(5, 4)
  • 8. 8 Default Parameter Values def name(parameter=value, ..., parameter=value): statements – Can make parameter(s) optional by specifying a default value – Exercise: Modify stars.py to add an optional parameter for the character to use for the outline of the box (default "*"). >>> def print_many(message, n=1): ... for i in range(n): ... print(message) >>> print_many("shrubbery") shrubbery >>> print_many("shrubbery", 3) shrubbery shrubbery shrubbery
  • 9. 9 Parameter Keywords name(parameter=value, ..., parameter=value) – Can specify name of each parameter as you call a function – This allows you to pass the parameters in any order >>> def print_many(message, n): ... for i in range(n): ... print(message) >>> print_many(str="shrubbery", n=4) shrubbery shrubbery shrubbery shrubbery >>> print_many(n=3, str="Ni!") Ni! Ni! Ni!
  • 10. 10 DrawingPanel • Instructor-provided drawingpanel.py file must be in the same folder as your Python program • At the top of your program, write: – from drawingpanel import * • Panel's canvas field behaves like Graphics g in Java
  • 11. 11 DrawingPanel Example draw1.py 1 2 3 4 5 from drawingpanel import * panel = DrawingPanel(400, 300) panel.set_background("yellow") panel.canvas.create_rectangle(100, 50, 200, 300)
  • 12. 12 – Notice, methods take x2/y2 parameters, not width/height Drawing Methods Java Python drawLine panel.canvas.create_line(x1, y1, x2, y2) drawRect, fillRect panel.canvas.create_rect(x1, y1, x2, y2) drawOval, fillOval panel.canvas.create_oval(x1, y1, x2, y2) drawString panel.canvas.create_text(x, y, text="text") setColor (see next slide) setBackground panel.set_background(color)
  • 13. 13 Colors and Fill • Python doesn't have fillRect, fillOval, or setColor. – Instead, pass outline and fill colors when drawing a shape. – List of all color names: http://wiki.tcl.tk/16166 – See class web site for visual index of colors! drawcolors.py 1 2 3 4 5 from drawingpanel import * panel = DrawingPanel(400, 300) panel.canvas.create_rectangle(100, 50, 200, 200, outline="red", fill="yellow") panel.canvas.create_oval(20, 10, 180, 70, fill="blue")
  • 14. 14 Polygons • Draw arbitrary polygons with create_polygon • Draw line groups by passing more params to create_line drawpoly.py 1 2 3 4 5 from drawingpanel import * panel = DrawingPanel(200, 200) panel.canvas.create_polygon(100, 50, 150, 0, 150, 100, fill="green") panel.canvas.create_line(10, 120, 20, 160, 30, 120, 40, 175)
  • 15. 15 Exercise • Write a Python version of the Car program. – Convert this Java code to Python: DrawingPanel panel = new DrawingPanel(200, 200); panel.setBackground(Color.LIGHT_GRAY); Graphics g = panel.getGraphics(); g.setColor(Color.BLACK); // body g.fillRect(10, 30, 100, 50); g.setColor(Color.RED); // wheels g.fillOval(20, 70, 20, 20); g.fillOval(80, 70, 20, 20); g.setColor(Color.CYAN); // windshield g.fillRect(80, 40, 30, 20);
  • 16. 16 Exercise • Modify your car program to use parameters so that cars can be drawn in many different locations.
  • 17. 17 Exercise • Write a variation of the Car program where the car body is octagonal and there is a stop sign. – Stop sign at (150, 10), size 40 • post at (165, 50), size 10x30, brown fill – Write an octagon function to draw the car body / stop sign. • Points of car body, located at (10, 10): 1. (10, 20), 2. (20, 10), 3. (100, 10), 4. (110, 20), 5. (110, 50), 6. (100, 60), 7. (20, 60), 8. (10, 50) • Points of stop sign, located at (150, 10): 1. (150, 20), 2. (160, 10), 3. (180, 10), 4. (190, 20), 5. (190, 40), 6. (180, 50), 7. (160, 50), 8. (150, 40) (An octagon has 10x10 triangular cuts in each corner.) 1 2 3 4 5 6 7 8
  • 18. 18 Animation • Pause the panel by calling sleep animation.py 1 2 3 4 5 6 7 8 9 from drawingpanel import * panel = DrawingPanel(350, 300) for i in range(20): # clear any previous image panel.canvas.create_rectangle(0, 0, 400, 400, outline="white", fill="white") panel.canvas.create_polygon(20 * i, 50, 20 * i, 100, 20 * i + 50, 75) panel.sleep(100)
  • 19. 19 Exercise • Animate the car to make it drive across the panel using the sleep function.