CNM - C3

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/22

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

23 Terms

1
New cards

my_list = [1, 7, “hi”, 3, 5]

write a for loop to list this list

remember colon and 4 spaces to be included in loop

<p>remember colon and 4 spaces to be included in loop</p>
2
New cards

write a while loop to produce 0,1,2,3,4,5

knowt flashcard image
3
New cards
<p>symbols for </p>

symbols for

knowt flashcard image
4
New cards
<p>whats will be printed if at the beginning</p><ul><li><p>x=1</p></li><li><p>x=2</p></li><li><p>x=3</p></li></ul><p></p>

whats will be printed if at the beginning

  • x=1

  • x=2

  • x=3

if x=1 printed: 1

if x=2 printed: -8888

if x=3 printed: -9999

5
New cards

what code can you use to avoid error and move forward with code

try and except

it will try the try code and if this won’t work it will run the except code

<p>try and except</p><p></p><p>it will try the try code and if this won’t work it will run the except code </p>
6
New cards

what can you use to test for potential issues and stop the code

if —→ raise Exception()

<p>if —→ raise Exception()</p><p></p>
7
New cards

words used to halt or proceed with a loop

break - terminates the nearest loop it is in

continue - continues to the next iteration of the loop

8
New cards
<p>what does this code do and what should the spacing be</p>

what does this code do and what should the spacing be

figure out the code

<p>figure out the code</p>
9
New cards

text = input("enter a string:")

if ___________

print("even")

else:

print("odd")

  • fill the gap after even to create a programme which prints even if the number of letters in the string is even and odd if it isn’t

text = input("enter a string:")

if len(text) %2 == 0:

print("even")

else:

print("odd")

this divides by 2 and gives the remainder - if the remainder is 0 it is even

10
New cards

how do you denote a dictionary in python and give an example dictionary with some personal info

it should have curly brackets, “speech marks” colons in between key and variables and commas in between information:

information = {"Name" : "Daisy", "Location": "Sheffield"}

print(information[“Name”]) gives Daisy

you can change and add info

11
New cards

What is the output from the following section of code?

try:

x = int("100")

print(x)

except:

print("Error")

is it '“100”, 100, or Error

100

12
New cards

what will this code return

d = {"a": 1, "b": 2}

for key in d:

print(d[key])

1

2

13
New cards

Which Python feature can combine two lists into a dictionary without using a manual loop?

zip() is used followed by dict()

eg:

keys = ['a', 'b', 'c']

values = [1, 2, 3]

d = dict(zip(keys, values))

14
New cards

what does this code print

my_dict = {"a": 1, "b": 2}

for key in my_dict:

print(key)

a

b

15
New cards

What is the output of this code?

for i in range(2):

for j in range(2):

print(i, j)

(0,0) (0,1) (1,0) (1,1)

16
New cards

What is the output for the following section of code?

keys = ["a", "b", "c"]

values = [1, 2, 3]

result = {}

for i in range(len(keys)):

result[keys[i]] = values[i]

{"a": 1, "b": 2, "c": 3}

17
New cards

What is the output of this code?

varA = True

varB = False

if varA:

print("TRUE")

if not varB:

print("TRUE")

TRUE

TRUE

18
New cards

What will happen?

if 0:

print("Hello")

else:

print("Goodbye")

prints goodbye

19
New cards

What's the output for the following section of code?

nums = {"x": 10, "y": 20}

nums["x"] += 5

print(nums["x"])

15

20
New cards

What's the output for the following section of code?

d = {"x": 1, "y": 2, "z": 3}

print("y" in d)

True

21
New cards

change this so it prints 2

d = {"x": 1, "y": 2, "z": 3}

print("y" in d)

d = {"x": 1, "y": 2, "z": 3}

print(d[“y”])

22
New cards

What type of data is always returned by input()?

String - always.

23
New cards

What is an exception in Python?


An error detected during program execution