Dot & Flip: Funny Python ๐Ÿ˜Š
1 subscriber
37 photos
16 links
For parents and teachers of kids who learn Python with our cartoons. New films, puzzle answers, tips, and answers to your questions: comment under a post or message the channel. Films: @dotandflip on YouTube, Instagram, TikTok and Facebook.
Download Telegram
Lesson 16: a loop inside a loop.

The picture show needs ten rows of ten seats, and Gulp wants the middle of the front row. The poster says one hundred seats, so Dot puts 100 in the inner loop. The rows go through the wall and into the next town. So does Gulp's seat.

New idea: the inner loop runs again for every pass of the outer loop, so ten rows of ten seats is one hundred.

Puzzle: what will this print?
for a in range(2):
for b in range(3):
print("x")

Answer: x six times. 3 for each of 2 passes.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson16 #python
๐Ÿ‘1
Lesson 17: while loops.

Gulp's hunger is five, and Dot means: keep flipping until hunger is zero. Python has no until, so she writes while hunger == 0. The answer is False straight away, the loop never starts, and Gulp eats the menu.

New idea: while asks its question before every pass, a line inside must change the answer, and while means as long as, not until.

Puzzle: what will this print?
n = 10
while n > 3:
n = n - 4
print(n)

Answer: 2. 10, 6, 2, and 2 > 3 is False.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson17 #python
๐Ÿ‘1
Lesson 18: the loop that never stops, and break.

Dot writes two lines of Python. The town goes under forty feet of pancakes.

New idea: while True never stops by itself. break stops the loop from inside, and Ctrl+C stops a program that runs away.

Puzzle: what will this print?
flips = 0
while True:
flips = flips + 1
print("flip!")
if flips == 2:
break

Answer: flip! twice. break stops it at 2.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson18 #python
๐Ÿ‘1
Lesson 19: slices and tuples.

The festival runs Monday to Friday, and Monday is giant pancake day. Dot writes days[1:5] for days one to five. But places start at zero, so the festival starts on Tuesday. Gulp sits by a cold pan.

New idea: a slice takes places from the start up to, but not including, the stop. A tuple never changes, and a tuple of one needs a comma.

Puzzle: what will this print?
days = [1, 2, 3, 4, 5, 6, 7]
print(days[2:4])

Answer: [3, 4]. Places 2 and 3.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson19 #python
๐Ÿ‘1
Lesson 20: dictionaries.

Flip runs the town switchboard from a phone book. The town has three people called Sam, and Dot adds them all under the key Sam. Each new Sam replaces the last one, and every call for Sam goes to the doctor.

New idea: a dictionary keeps keys and values. You look up by key, a missing key is a KeyError, and each key appears only once.

Puzzle: what will this print?
book = {"a": 1, "b": 2}
book["a"] = 5
print(book["a"] + book["b"])

Answer: 7. "a" is now 5, and 5 + 2 is 7.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson20 #python
๐Ÿ‘1
Lesson 21: functions.

It is Gulp's birthday. Dot writes the birthday recipe perfectly, as a function, and prints ready at the end. But nobody calls it. Nothing is cooked, and by evening Gulp has grown a little moss.

New idea: def gives a block of steps a name but does not run it. A call runs it, and a value in the brackets changes what the steps do.

Puzzle: what will this print?
def a():
print("a")
def b():
print("b")
b()

Answer: b. Only b() is called.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson21 #python
๐Ÿ‘1
Lesson 22: return.

Gulp orders a banquet: three plates, four pancakes on each. Dot's function prints cooking, cooking, cooking, and Flip serves what lands in the list of plates: three cards that say None. Gulp tries one. It tastes of nothing.

New idea: return hands a value back, print only shows it on the screen, and a function with no return gives back None.

Puzzle: what will this print?
def triple(n):
print(n * 3)

x = triple(2)
print(x)

Answer: 6, then None.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments
๐Ÿ‘1
Lesson 23: where variables live.

Flip cooks three pancakes inside a function, then walks out to serve them. But the stack was made inside the kitchen, and when the function ended, it was gone. Outside, Python has never heard of it.

New idea: a name made inside a function lives only inside it. Setting a name inside makes a new one; to get something out, return it.

Puzzle: what will this print?
n = 1

def change():
n = 2

change()
print(n)

Answer: 1. The 2 stayed inside.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson23 #python
๐Ÿ‘1
Lesson 24: modules and import.

Gulp and Flip play dice for pancakes, and Python keeps the dice in a toolbox called random. Dot keeps a throw in a variable, and she calls it random too. Now the name means a number, not the toolbox, and Flip never gets a turn.

New idea: import opens a toolbox, and its tools come after a period: random.randint. from random import randint takes one tool out.

Puzzle: what will this print?
from random import randint

print(random.randint(1, 6))

Answer: NameError. Only randint came out.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson24 #python
๐Ÿ‘1
Lesson 25: angles, shapes and stars.

Dot draws Gulp a star to wish on: eight rays share a full turn of 360 degrees. Then she writes rays = 45. Forty-five rays share the turn, eight degrees each, and midnight is as bright as noon.

New idea: a shape turns 360 / sides at each corner. The % sign is not percent: it gives what is left over, so 7 % 2 is 1.

Puzzle: what will this print?
print(10 % 4)

Answer: 2. Ten on four plates: 2 each, 2 left over.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson25 #python
๐Ÿ‘1
Lesson 26: colors and filling.

The town votes for brown walls. Red and green paint make brown, so Dot turns the red and green lamps full on: color(1, 1, 0). But a screen mixes light, not paint. Every wall in town turns yellow, and Gulp starts on the bakery.

New idea: color(r, g, b) is three lamps, each from 0 (off) to 1 (full on). begin_fill() and end_fill() paint the inside.

Puzzle: what color will it be?
import turtle
turtle.color(0, 0, 0)
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
turtle.done()

Answer: black. Every lamp is off.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson26 #python
๐Ÿ‘1
Lesson 27: functions with named values.

Every good place on the town map gets a star: star(size, filled=False). The bakery gets star(5000, filled=True). The window is 330 steps wide, so Flip walks off the map with his brush and comes back from the far side.

New idea: a value can have a name and a default, like filled=False. Leave it out, and the default is used.

Puzzle: what will this draw?
import turtle
def star(size, filled=False):
if filled: turtle.begin_fill()
for n in range(5):
turtle.forward(size)
turtle.left(144)
if filled: turtle.end_fill()
star(60)
turtle.done()

Answer: an outline. filled stays False.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson27 #python
๐Ÿ‘1
Lesson 28: measuring lists.

At the pancake contest, Dot's program hands out the prizes: a trophy for the most, a crumb for the fewest. But min and max traded places, and the champion, who ate thirty pancakes, gets a crumb.

New idea: len counts, sum adds, max and min find the biggest and the smallest. Places start at 0, so the last place is len - 1.

Puzzle: what will this print?
scores = [3, 9, 4]
print(max(scores) - min(scores))

Answer: 6. Nine minus three.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson28 #python
๐Ÿ‘1
Lesson 29: asking with input.

Each customer types how many pancakes they want, and int turns the answer into a number. Bo types 2. Then Gulp types lots, int cannot read a word, and the program stops. So does Flip, and nobody after Gulp gets breakfast.

New idea: input() asks and waits, and what you type always comes back as text. int() makes a whole number; a word gives a ValueError.

Puzzle: what will this print? Type 7 when it asks.
n = input("How many? ")
print(n * 2)

Answer: 77. Text times 2: the text, twice.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson29 #python
๐Ÿ‘1
Lesson 30: what counts as true.

Ben has a cold, so Dot rubs his name off the guest list with a space. But a space is not empty: Flip lays a plate for a guest nobody can see, and serves him soup.

New idea: zero, empty text "" and an empty list [] count as no. Everything else counts as yes, even a single space " ".

Puzzle: what will this print?
print(bool("0"), bool(0))

Answer: True False. "0" is not empty.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson30 #python
๐Ÿ‘1
Lesson 31: random choices.

Dot's program mixes up the diner's menu: menu = random.shuffle(menu). But shuffle mixes the list itself and gives back None. The menu board goes blank, and Gulp, still hungry, eats it.

New idea: random.choice picks one thing from a list, random.shuffle mixes a list and gives back None, and random.seed makes the same picks every run.

Puzzle: what will this print?
import random
random.seed(1)
a = random.choice([1, 2, 3])
random.seed(1)
b = random.choice([1, 2, 3])
print(a == b)

Answer: True. Same seed, same pick.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson31 #python
๐Ÿ‘1
Lesson 32: time and waiting.

A pancake needs three seconds on the pan, and Flip naps while the program waits. Dot tells it to sleep until start plus three. But sleep wants how long, and start is almost two billion seconds since 1970. Flip sleeps for almost fifty-seven years.

New idea: time.sleep(3) waits three seconds, time.time() is the time now in seconds since 1970, and the end minus the start is how long it took.

Puzzle: what will this print?
import time
start = time.time()
time.sleep(1)
time.sleep(1)
print(int(time.time() - start))

Answer: 2. Two naps of one second.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson32 #python
๐Ÿ‘1
Lesson 33: classes and objects.

Dot gets her helper from the robot recipe inside the loop: same recipe, same robot. But a class makes a brand-new object every time you call it. A hundred passes later the town has 101 robots, and only one has a name.

New idea: a class is a recipe, Robot() makes a new object every time, and the dot keeps values inside one object.

Puzzle: what will this print?
class Robot:
pass

a = Robot()
a.made = 1
a.made = a.made + 2
print(a.made)

Answer: 3. One made, plus two.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson33 #python
โค1
Please open Telegram to view this post
VIEW IN TELEGRAM
Lesson 34: methods and self.

Dot gives her robots a new trick, cook, and writes def cook(): with nothing in the brackets. Python still hands the robot over, cook has nowhere to put him, and the program stops. So does Flip, mid-flip, from breakfast to dusk.

New idea: a function inside a class is a method. Python hands the object in under the name self, so every method needs self first in its brackets.

Puzzle: what will this print?
class Robot:
def hello(self):
return "hi " + self.name

a = Robot()
a.name = "Flop"
print(a.hello())

Answer: hi Flop. self is a, and a.name is Flop.

Watch: YouTube ยท Instagram ยท TikTok ยท Facebook
Stuck? Ask in the comments.
#lesson34 #python