AP COMP SCI

AP Exam Vocab

  • A procedure is a named group of programming instructions that may have parameters and return values.

  • Parameters are input variables of a procedure.

  • Arguments specify the values of the parameters when a procedure is called.

  • Testing uses defined inputs to ensure that an algorithm or program is producing the expected outcomes. Programmers use the results from testing to revise their algorithms or programs.

  • A code statement is a part of program code that expresses an action to be carried out.

  • An expression can consist of a value, a variable, an operator, or a procedure call that returns a value.

  • Program input is data sent to a computer for processing by a program. Input can come in a variety of forms, such as tactile, audio, visual, or text.

  • Events are associated with an action and supplies input data to a program.


Functions

  • A function takes inputs and runs code using those inputs.

  • To define a function, we write: def functionName(parameter1, parameter2, ...):

    • All code inside your function must be indented below your function definition.

    • Parameters are input variables of a function.

  • You can call a function with arguments after it is defined.

    • Arguments specify the values of the parameters when a function is called.

    • You must have exactly as many arguments as parameters, and in the correct order.


Debugging

Errors
  • The console is where python informs you of any errors in your code.

  • A syntax error is a mistake in the program where the rules of the programming language are not followed.

    • For example, using things like commas, parentheses, or indentation incorrectly.

  • A runtime error is a mistake in the program that occurs during the execution of a program.

    • For example, inputting a negative number as the radius of a circle.

  • A logical error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly.

Debugging Tips and Tricks
  • Look at the line numbers provided by the console. Check the surrounding lines as well!

  • Use print statements to see what values your variables are storing, and see what parts of your code are run before or after the error.

  • Use the test cases to run code and analyze the difference between your canvas and the solution.

  • Finally, use code tracing by hand (on paper) to simulate your code in slow motion.


Mouse Events

  • We provide some built-in functions that you can use to create interactive programs.

    • onMousePress(mouseX, mouseY) is called every time the mouse is clicked.

    • onMouseRelease(mouseX, mouseY) is called when the mouse is released.

    • (mouseX, mouseY) is the point where the mouse is clicked/released.


Variables and Properties

Variables
  • Use variables to store shapes so you can change their properties later without drawing entirely new shapes.

  • c = Circle(200, 200, 100, fill=’red’) stores a red circle in the variable c.

  • c.fill=’blue’ changes that circle’s color to blue.

  • c.centerX += 100 adds 100 to the circle's centerX. Subtract with -=, multiply *=, or divide /=.

General Shape Properties
  • All Shapes have these properties.

  • Position: left, right, top, bottom, centerX, and centerY (numbers only)

  • Size: width and height (positive numbers only)

  • Fill and Border:

    • fill and border (colors only)

    • borderWidth (positive numbers only)

    • opacity (numbers between 0 and 100)

    • dashes (True or False)

  • Other:

    • rotateAngle (numbers only)

    • visible (True or False)

Custom Properties
  • Custom properties are like variables that belong to a specific shape.

  • You can name them whatever you want, and store whatever values you want.

    • myPerson.hunger = 50 creates a custom property for your myPerson shape. You can then use this custom property to keep track of its hunger and make your program do something when it reaches 0.


Conditionals

If - Else Conditionals
  • Use if (condition): and indent all code you want to be in the conditional body.

  • An if statement will test a condition and only run the conditional body if that condition is true.

  • Use else: and an indented body to define a chunk of code that should run when the if condition is not true. You must have an if statement before any else’s

  • Here are some common conditions you can write:

Python syntax

AP Exam Syntax

Meaning

x == y

x = y

Is x equal to y?

x != y

x ≠ y

Is x not equal to y?

x < y

x < y

Is x less than y?

x > y

x > y

Is x greater than y?

x <= y

x ≤ y

Is x less than or equal to y?

x >= y

x ≥ y

Is x greater than or equal to y?

If - Elif - Else Conditionals
  • Use consecutive if statements if you want one or more conditions to be true at a time.

  • Use if - elif conditionals if you have multiple conditions and you want only exactly one condition to be true at a time.

  • elif (condition): will only run the conditional body if the first if statement was not true.

  • You must have an if statement before any elif statements.


Shape Methods

  • In Python, only objects can have properties and methods.

    • In CS Academy, shapes are a type of object.

    • The app is also an object because it has properties and methods such as app.background and app.stop()

  • A method is like function, but it only works with shapes.

  • shape.toFront() moves that shape to the top layer of the canvas.

  • shape.hits(mouseX, mouseY) tests whether a point is hitting that shape.

    • This method does not change anything, it simply tells us True or False if the point is hitting that shape.

    • Hits only works on parts of the shape that are drawn (non-empty fills).

    • You may often use if (shape.hits(mouseX, mouseY) == True):

  • shape.hitsShape(otherShape) tests whether that shape is hitting another shape. It is used in the same way as hits above.