Python 7 Flashcards
How will Bubble sort these lists in ascending order?
[ 1, 2, 4, 5, 8]
[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
What is the bubble sort pseudocode?
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]
Explain the code?
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)
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]:
-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 to start drawing with turtle?
> > > import turtle
How to see the turtle?
> > > turtle.shape (‘turtle’)
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
-turtle.forward(distance)
-turtle.left(angle)
-turtle.right(angle)
-turtle.pendown()
-turtle.penup()
-turtle.clear()
-turtle.reset()
turtle.xcor()
turtle.ycor()
What should the last line be in turtle?
turtle.exitonclick()
What code to find turtle coordinates?
print(“x-coordinate=”, turtle.xcor() , “y-coordinate=” , turtlemycor())
Angle for
-Triangle
-Quadrilateral
-Pentagon
-Hexagon
-Hepatgon
-Octagon
-60
-90
-108
-120
-128.57
-135
How to draw with turtle on loop? Draw a square
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()
What not to name your turtle file and why?
Dont name it turtle.py as it will shadow turtle module in standard libary