• Boolean Logic Advanced Math Operations Advanced String Operations Review and Quiz Unit Project

Welcome to Python 2



Next
© Tynker, 2020

Introduction to Python 2

In this level of the course, you'll dive even deeper into computer science. You will work with advanced concepts like functions to reuse and organize your code. You'll explore new data structures like lists and dictionaries to store data. You'll explore intriguing questions like: Can a function call it self? (Answer: But of course! This can lead to really cool art like fractals.) Do you want to learn about data science and charting? Pygal has you covered. Read on for a quick summary of everything that lies ahead.

APCSP
Previous Next
© Tynker, 2020

About The Course

The course is organized into a series of Units. Each unit is broken into chapters. You can skip around if you like, but it is recommended that you follow this course sequentially: Each challenge builds on the last, and they grow in complexity as the course progresses.

Unit 1. Advanced Expression One powerful aspect of computing is the ability to perform many calculations at once. An expression is a bit of code that represents a value (or that can be computed to a single value). In this lesson, you'll learn how to create useful expressions of your own.

Unit 2. Functions Learn how functions can be used to generalize particular solutions into broader, more generally applicable ones. In addition, read how functions can help keep your code organized and repeatable.

Unit 3. Lists A list is a special kind of variable that holds a series of items called elements in a particular order. Lists are handy for representing lots of data, all at once, in a sequence. Learn how to manipulate and process lists in this Unit.

Unit 4. Dictionaries The dictionary is another useful data structure, which represents key-value pairs. Create a working version of Tetris using this new data structure!

Unit 5. PyGal and Charting Plot energy data from the US federal government and analyze trends in different energy sources. Bar Charts, Pie Charts, Line Charts are demonstrated using Pygal.

Unit 6. Classes Learn about object-oriented programming, including the relationship between a class and an object, how to create their own custom types. In this way, you will learn how object oriented programming can make for more generalizable solutions to problems.

Unit 7. Recursion Kick back with recursion, the most mind-bending programming idea around. The course keeps things fun with examples like fractals, “coin counters” and more, after covering the classics like Fibonacci, Merge Sort, and Factorial.

How to complete each lesson

  • Read each module carefully and experiment with the programs we give you
  • Solve all programming problems and answer all questions in the module before moving on
  • Take the quiz at the end of the lesson

New concept: Introduces a new term or idea.

General idea: Shows a diagram of a short Python snippet.

Puzzles: Programming challenges that test your program and give immediate feedback on whether your solution works.

Project: Be creative with these programs - there is more than one way to solve a problem!

Previous Next
© Tynker, 2020

Boolean Logic

Along with performing mathematical computations, computers can also perform logical computations. Logic expressions compute boolean values, represented by the special values True and False.

Python equates True to the number 1, and False to the number 0. Therefore, you can use booleans in mathematical expressions just as you would use the numbers 1 and 0.

Calculating Your Score

Multiplying a number by True produces the same number, and multiplying by False produces 0. Try changing on_time to False to see your score if you turn in your homework late.

Gymnastics Score

In a gymnastics routine, a gymnast gets a certain number of points if they performed a certain move.

  • If they did a frontflip, they get 1 point.
  • If they did a handspring, they get 2 points.
  • If they did a cartwheel, they get 3 points.
  • If they did a backflip, they get 4 points.

Using the given boolean variables, print the gymnast's final score.

Previous Next
© Tynker, 2020

Equality Operators

Certain operators produce boolean values that describe some property of the values they are combining.

Equal Operator

The equal operator == produces a boolean value based on whether the left side is equal to the right side. This operator is read as, "equal to". For example x == 2 is read as x is equal to 2?.

The equal to operator can be applied to strings and numbers.

Pythagorean Theorem

The Pythagorean theorem states that a2 + b2 = c2 holds true for any right triangle with side lengths a, b, and c, where c is the length of the hypotenuse.

Given the lengths of the three sides of a triangle as a, b, and c, print True if the triangle is a right triangle, and False otherwise. Assume c is the hypotenuse of the triangle.

Not Equal Operator

The not equal operator != produces a boolean based on whether the left value is not equivalent to the right value. This operator is read as, "not equal to". For example x != 2 is read as x is not equal to 2?.

The not equal to operator can be applied to strings and numbers.

Not divisible

Given two numbers a and b, print True if a is not divisible by b, and False otherwise.

is and is not

The is operator produces True if the values on either side of the operator refer to the same value. The is operator is particularly useful in determining if two variables refer to the same location in the computer's memory.

The is not operator returns True if the values on either side of the operator are not in the same location in the computer's memory.

When comparing values, it is conventional to use the equal operator ==. The is and is not operators are primarily used for checking if two variables refer to the same location in the computer's memory.

Previous Next
© Tynker, 2020

Inequalities

The order of values can be compared with the < (less than) and > (greater than) operators. By adding an equal sign = after the operator (i.e. <= and >=), the comparison will also be True if the values are equal.

These operators are read as:

  • <: "less than"
  • >: "greater than"
  • <=: "less than or equal to"
  • >=: "greater than or equal to"

Storing comparisons

You can make your code easier to read by storing boolean results of comparisons in well-named variables for further computations.

Change the value of age to see how the stored boolean values change.

Valid triangle

Given three numbers a, b, and c, print True if a triangle can be drawn with those side lengths, and False otherwise.

Checking if a triangle can be drawn:

  • a + b is greater than c
  • a + c is greater than b
  • b + c is greater than a

Chaining comparisons

Comparison operators can be chained such that each pair of comparisons must be True for the entire expression to be True.

Is the Number a Teen?

Given a number, print True if the number is a teen number (i.e. between thirteen and nineteen), and False otherwise.

Previous Next
© Tynker, 2020

Comparing strings

The equality and inequality operators can be used on strings as well. For example, the greater than and less than operators compare alphabetical order.

Pizza Delivery

A New York pizza restaurant will only deliver to addresses between 01st street and 46th street. (There are no single digits in the street name.) Given a customer's address as a string, print True if the restaurant will deliver to the address, and False otherwise.

ASCII

Every character corresponds to a standard unique number given by the American Standard Code for Information Interexchange, abbreviated as ASCII.

You can use the ord() function to find this unique number.

When two strings are compared, the underlying ASCII values of the characters are being compared. Notice that the ASCII value of number characters are not equivalent to the numbers themselves.

This is why comparing numbers as strings can lead to unexpected results.

In the first comparison, the ASCII value of the first character "8" is greater than the ASCII value of "4", so the string is considered greater than the other string.

Alphabet position

Given a lowercase letter of the alphabet, print its position in the alphabet between 1 and 26.

The chr Function

The chr function performs the reverse operation of the ord function. Whereas the ord function returns the unique integer associated with each ASCII character, the chr function will return the unique character associated with an integer.

All strings are just sequences of numbers. The chr function lets you construct strings from their underlying ASCII values.

Print the Letter

Given an integer position between 1 and 26, print the corresponding uppercase letter of the alphabet.

Previous Next
© Tynker, 2020

The and operator

The and operator produces True if the values on the left and right are both True.

A common use of the and operator is to combine mathematical comparisons.

Test It Out

Given a number n, print True if n is an even two-digit number, and False otherwise.

The number in n must satisfy all of the following three conditions:

  • n is greater than or equal to 10
  • n is less than 100
  • n is an even number (you can find this with the modulus operator %)

and with numbers

Since nonzero numbers are equal to True, the and operator can also be used with numbers.

When two values are combined with the and operator, the right value is produced. This is why 1 and True produces True, while True and 1 produces 1.

and with True

In general, an expression of true values combined with and produces the last value.

Previous Next
© Tynker, 2020

The or operator

The or operator produces True if one or both boolean values is True.

How Many Digits?

Given a number n, print True if it is an even two-digit number or an odd three-digit number. Print False otherwise.

or with numbers

The or operator can be used with numbers like the and operator.

An expression of values combined with or produces the first true value. Remember: An expression of values combined with and produces the last true value.

and and or

In expressions that use both the and and or operator, the and operator is evaluated first.

If you need the or operator to be evaluated first, use parentheses, just as you would in any mathematical expression.

Previous Next
© Tynker, 2020

The not operator

The not operator produces True if the value on the right is equivalent to False, and True otherwise.

Programmer Happiness

A programmer is happy if they are smiling, they are not sick, and they are coding. Given these three characteristics in the boolean variables smiling, sick, and coding, print True if the programmer is happy, and False otherwise.

not with numbers

You can use the not operator to produce True if a number is zero, and False otherwise.

Exact Change

If you know the amount something costs and the amount of money given by a customer, print True if the customer paid exactly the right amount, and False otherwise.

Use the not operator to invert whether or not the difference between amount and given is 0.

not with strings

You can also use the not operator to check if a string is empty.

Change example to a non-empty string.

Coupons

A customer is about to purchase an item online that costs a certain price in dollars. Shipping costs 10 dollars unless the customer enters a coupon code. Use an if statement to print the total amount that the customer has to pay. The coupon value will be the empty string "" unless the user enters a coupon. Your code should work correctly for all values of price and coupon.

DeMorgan's Law

DeMorgan's laws state that given two true/false values a and b,

  • the negation of a and b is not a or not b
  • the negation of a or b is not a and not b

We can test DeMorgan's laws with the program below.

Change a and b to every combination of True and False, and notice the laws are always true.

DeMorgan's laws are useful for intuitively understanding the value produced by the not operator.

Previous Done
© Tynker, 2020
Sign in
Don't have an account?