Example Code Flashcards

1
Q

How do you do whole number division?

A

answer = num1 // num2

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

How do you do a line break?

A

\n

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

How to find the length of a variable

A

len(word)

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

Removes extra characters (in this case spaces) from the start and end of a string

A

text = “ This is some text. ”
print(text.strip(“ ”))

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

How to display certain letters in a word?

A

print (“Hello world”[7:10])

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

Changes the string into upper case.

A

word.upper()

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

Changes the string into lower case.

A

word.lower()

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

Changes a phrase so that every word has a capital letter at the beginning with the rest of the letters in the word in lower case

A

word.title()

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

To the power of

A

**

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

When using some math code what do we need to have at the start of the program?

A

import math

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

The square root of a number

A

math.sqrt(num)
but you must have the line import math

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

Gives you pi (π) to 15 decimal places

A

math.pi
you must have the line import math

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

Finds the remainder

A

x % y

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

Count up from 1 to 9

A

for i in range(1,10):
print(i)

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

counts up from 1 to 9 in 2s

A

for i in range(1,10,2):
print(i)

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

Counts down from 10 to 1 in 3s

A

for i in range(10,1,-3):
print(i)

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

Prints every letter of a variable on each line

A

for i in word:
print(i)

18
Q

Not equal to

A

!=

19
Q

What should you write at the top when starting to write code that uses random

A

import random

20
Q

Selects a random floating-point number between 0 and 1 and stores it in a variable called “num”

A

num = random.random()

21
Q

Selects a random whole number between 0 and 9 (inclusive)

A

num = random.randint(0,9)

22
Q

Picks a random value from the options “red”, “black” or “green” and stores it as the variable “colour”

A

colour = random.choice([“red”,“black”,“green”])

23
Q

Picks a random number between the numbers 0 and 100 (inclusive) in steps of five

A

num = random.randrange(0,100,5)

24
Q

Creates a variable name called “fruit_tuple” which stores four pieces of fruit within it

A

fruit_tuple = (“apple”,“banana”,“strawberry”,“orange”)

25
Q

Displays the index (i.e. the numeric key) of the item “strawberry”

A

print(fruit_tuple.index(“strawberry”))

26
Q

Displays item 2 from “fruit_tuple”, in this case “strawberry”.

A

print(fruit_tuple[2])

27
Q

Creates a list of the names and stores them in the variable “names_list”

A

names_list = [“John”,“Tim”,“Sam”]

28
Q

Deletes item 1 from “names_list”

A

del names_list[1]

29
Q

Asks the user to enter a name and will add that to the end of “names_list”.

A

names_list.append(input(“Add a name: “))

30
Q

Sorts name_list into alphabetical order and saves the list in the new order.

A

names_list.sort()

31
Q

Displays names_list in alphabetical order but does not change the order of the original list, which is still saved in the original order.

A

print(sorted(names_list))

32
Q

Creates a dictionary called “colours”, where each item is assigned an index of your choosing.

A

colours = {1:“red”,2:“blue”,3:“green”}

33
Q

Makes a change to the data stored in position 2 of the colours dictionary. In this case it will change “blue” to “yellow”.

A

colours[2] = “yellow”

34
Q

Displays the length of the list (i.e. how many items are in the list).

A

print(len(x))

35
Q

This will display data in positions 1, 2 and 3.

A

print(x[1:4])

36
Q

print the items in a list on separate lines.

A

for i in x:
print(i)

37
Q

Inserts the number 420 into position 2 and pushes everything else along to make space

A

x.insert(2,420)

38
Q

Deletes an item from the list.

A

x.remove(892)

39
Q

Adds the number 993 to the end of the list.

A

x.append(993)

40
Q

If their input is fully CAPS

A

if msg.isupper()

41
Q

If their input is fully lower

A

if msg.islower()