Python Functions Basics

AccommodativeYew avatar
AccommodativeYew
·
·
Download

Start Quiz

Study Flashcards

12 Questions

What is a common characteristic of good function names in Python?

They are short and clear

What happens when you call a function with no parameters in Python?

You still need to use parentheses

What is the purpose of the pass statement in a function?

To tell Python to do nothing

What is the rule for function names in Python regarding spaces?

They cannot contain spaces, newlines, or punctuation

What is the purpose of the onMousePress function in Python?

To handle mouse click events

What is a requirement for function parameters and arguments in Python?

They must match in number and order

What is the purpose of using the onMouseRelease(mouseX, mouseY) event?

To modify existing shapes when the mouse is released

How do you add 10 to a shape's x-coordinate?

x += 10

What is the default value of the border property for a shape?

None

Which property determines the text of a Label shape?

value

What is the purpose of top-level code?

To run code once before any event is called

How do you set the background color of the app?

app.background = 'black'

Study Notes

Functions Basics

  • A function's body should be indented by 4 spaces
  • Function parameters can be of different types: numbers, strings, booleans
  • Functions can be called multiple times
  • Python has rules for legal function names: must start with letters, can contain digits, cannot contain spaces, newlines, or punctuation
  • Good function names are short, clear, and camelCased

Function Parameters and Arguments

  • Parameters and arguments must match in number and order
  • When calling a function with no parameters, use parentheses

Mouse Events

  • The onMousePress(mouseX, mouseY) function is called every time the canvas is clicked
  • The function name must match the exact name
  • Parameter names can be chosen freely, but represent the x and y coordinates of the mouse press on the canvas

Using and Debugging Events

  • Use print to understand and debug code
  • Print statements should be removed when no longer needed
  • Use the control or ctrl key to access the inspector

Top-Level Code

  • Any code not inside a function is top-level code
  • Runs exactly once before any event is called

Math Operations

  • Use + to add, - to subtract, * to multiply, / to divide

Globals

  • To modify a shape after creation, store it in a global variable
  • Get and set shape properties in any function

Mouse Release Events

  • The onMouseRelease(mouseX, mouseY) function is called every time the mouse is released
  • Often used to modify existing shapes

Shape Properties

  • General shape properties include: position, size, alignment, fill, border, and more
  • Properties can be gotten and set
  • Default values are provided for each property

Shape-Specific Properties

  • Circle: radius
  • RegularPolygon: radius, points
  • Star: radius, points, roundness
  • Line: x1, y1, x2, y2, dashes, lineWidth
  • Label: value, font, size, bold, italic
  • App background: app.background

Test your understanding of basic functions in Python, including function elements, parameter types, and naming conventions. Learn how to define and call a function in Python.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser