Unit 4 APCSP

A string is an ordered sequence of characters.

A substring is a string that is a part of an existing string.

Compound Conditionals

Use these to test that more than one condition is true at the same time.

((condition1) and (condition2)) tests if both conditions are true.

((condition1) or (condition2)) tests if at least one test is true.

Inclusive or uses “or” to mean that at least one of the two things is true.

Exclusive or uses “or” to mean that one thing is true and not the other.

We often use both in English, but Python uses inclusive or.

(not (condition)) tests if the condition is false.

Toggling is when we want a variable to switch between true and false. Use not to toggle a boolean between true and false.

Nested Conditionals

The inner conditionals are only tested if the outer conditional is true.

Nested if statements act like a compound conditional with and.

(condition1 and condition2) where condition1 is the outer conditional.

Nested else statements act like compound conditionals with not.

(condition1 and not condition2)

Step Events

onStep is a built-in CMU Graphics function often used for animation.

onStep is automatically called repeatedly just by running the program.

Like onMousePress, whatever code you write in the body of the function will be run repeatedly. onStep takes no inputs.

app.stepsPerSecond changes the number of times that onStep is called every second.

The default is 30 steps per second.

Increase this number to speed up your animation.

Loop through groups inside of onStep to animate many shapes at once.

Types and Strings

Types

All values have a type in Python. These are the types we use in this course:

Integers (ints) are numbers without a decimal point. (Ex. 1, -2, 382)

Floats are numbers with a decimal point. (Ex. 1.0, -2.0, 5.0, 8.76)

Booleans are either True or False.

Strings are any characters between single or double quotes. (Ex. ‘hi’, “54!”, ‘CS Academy’)

Lists are any ordered sequences of stored values. (Ex. [1, ‘hi’, True], [3, 4, 5])

Shapes include all the shapes you learned before! (Rect, Oval, Circle, Label, etc.)

Using a value of the wrong type can cause a Type Error.

Rect(‘hi’, 200, 100, 100) causes an error because the x value should be an int.

Input

Use app.getTextInput(“some prompt in quotes”) to ask the user for input in a text box.

app.getTextInput returns a string, so store it in a variable to save their input.

The AP Exam uses INPUT() to do a similar thing.

Use int() to turn the user’s input into an int (since it’s a string). (Ex. int(‘5’) returns 5)

Strings

A string is an ordered sequence of characters.

A substring is a string that is part of an existing string.

String concatenation: you can concatenate (combine) strings with the + operator.

‘CS’ + ‘Academy’ returns ‘CSAcademy’

Looping over Strings: If you want to do something to each individual character in a string.

for c in ‘some string’: works like with lists and groups

With each pass, the looping variable is set to the next character in the string.

String Indexing: Like lists, you can index into a string to get a specific character.

If s = ‘amazing’ then s[0] is ‘a’ and s[3] is ‘z’

for i in range(len(s)): will loop over just the indices of a string s.

Libraries

A library is a collection of functions which we can add to our code in addition to standard Python.

Functions that Return Values

.hits and .contains are functions that return values (they return booleans)

Use return value to make your function return whatever value you want.

Use RETURN(expression) on the AP Exam.

The function stops running once you return a value. (no code after return will run)

color = getColor() will assign the return value of getColor() to the color variable.

Random Numbers

You must write import random at the top of your code to use random functions.

random.randint(low, high) gives you a number between and including low and high.

Use RANDOM(a, b) on the AP Exam (works exactly like randint).

random.choice(list) chooses a random value from the input list.

Images

Using Images

First find a url for an image you want to use (url must end in .png, .jpg, etc.)

Use image = Image(url, left, top) to load your image onto the canvas.

Resize your image by changing image.width and image.height.

Include Citations for any material you use that is not your own, including images and code!

Image Properties

width and height

centerX and centerY

opacity can be used to make an image slowly visible!