Python 7 Flashcards

1
Q

How will Bubble sort these lists in ascending order?
[ 1, 2, 4, 5, 8]

A

[1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8]
Do nothing, since 1 < 2
[1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8]
Do nothing, since 2 < 4
[1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8]
Do nothing, since 4 < 5
[1, 2, 4, 5, 8] -> [1, 2, 4, 5, 8]
Do nothing, since 5 < 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the bubble sort pseudocode?

A

While swapped is True
swapped = False
for i from 1 to the length of list
if list[i – 1] > list[i]
swapped = True
swap list[i – 1] and list[i]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the code?

A

1️⃣ Start with swapped = True so that the loop runs at least once.
2️⃣ Enter the while loop (only runs if swaps happened in the previous pass).
3️⃣ Set swapped = False at the start of each pass.
4️⃣ Go through the list (for i from 1 to length of list):

Compare list[i - 1] and list[i].
If the left number is bigger than the right, swap them.
Set swapped = True (this means a swap happened).
5️⃣ If no swaps happened (swapped = False), exit the loop early (list is already sorted)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the following do: Use S=python as an example
-S[1:4]:
-S[1:4:1] or S[start, stop, step]
-S[::2]:
S[::-1]:

A

-S[1:4]: ‘yth’
-S[1:4:1]: ‘yth’, first number is start 2nd where you stop third how many you skip
-s[::2]: ‘pto’ Takes every 2nd element from the beginning
-S[::-1]: ‘nohtyp’ It starts at the last character then moves backwards every 1 step

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to start drawing with turtle?

A

> > > import turtle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to see the turtle?

A

> > > turtle.shape (‘turtle’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the methods to do these
-Move forward by distance pixels
-Rotate anti-clockwise angle degrees
-Rotate clockwise angle degrees
-Lowers pen so it can draw
-Lift pen to it doesnt draw
-Clear the screen and leaves turtle where it is
-Clear screen and put turtle back in starting position
-Return the turtle x coordinate
-Return the turtle y coordinate

A

-turtle.forward(distance)
-turtle.left(angle)
-turtle.right(angle)
-turtle.pendown()
-turtle.penup()
-turtle.clear()
-turtle.reset()
turtle.xcor()
turtle.ycor()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What should the last line be in turtle?

A

turtle.exitonclick()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What code to find turtle coordinates?

A

print(“x-coordinate=”, turtle.xcor() , “y-coordinate=” , turtlemycor())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Angle for
-Triangle
-Quadrilateral
-Pentagon
-Hexagon
-Hepatgon
-Octagon

A

-60
-90
-108
-120
-128.57
-135

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to draw with turtle on loop? Draw a square

A

Use i in range(amount of sides shape has)
To draw a square:
import turtle
for i in range(4) :
turtle,forward(50)
turtle.right(90)
turtle.exitonclick()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What not to name your turtle file and why?

A

Dont name it turtle.py as it will shadow turtle module in standard libary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly