Python Functions Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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

  • They must contain at least one digit
  • They are short and clear (correct)
  • They must start with an uppercase letter
  • They must be at least 10 characters long

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

  • The function will not be executed
  • You must pass a default value
  • You still need to use parentheses (correct)
  • You don't need to use parentheses

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

  • To print a message to the console
  • To tell Python to do nothing (correct)
  • To raise an error
  • To exit the function early

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

<p>They cannot contain spaces, newlines, or punctuation (B)</p> Signup and view all the answers

What is the purpose of the onMousePress function in Python?

<p>To handle mouse click events (C)</p> Signup and view all the answers

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

<p>They must match in number and order (B)</p> Signup and view all the answers

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

<p>To modify existing shapes when the mouse is released (A)</p> Signup and view all the answers

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

<p>x += 10 (B)</p> Signup and view all the answers

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

<p>None (B)</p> Signup and view all the answers

Which property determines the text of a Label shape?

<p>value (B)</p> Signup and view all the answers

What is the purpose of top-level code?

<p>To run code once before any event is called (B)</p> Signup and view all the answers

How do you set the background color of the app?

<p>app.background = 'black' (A)</p> Signup and view all the answers

Flashcards are hidden until you start studying

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

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team
Use Quizgecko on...
Browser
Browser