Search Header Logo

Python comprehensions, generators, and exceptions

Authored by Erik Nielsen

Computers

9th - 12th Grade

28 Questions

Used 6+ times

Python comprehensions, generators, and exceptions
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Media Image

The code to the left can be written more compactly as

a = 'hi' if x = 3 else 'there'

a = if x == 3: 'hi' else: 'there'

a = 'hi' if x == 3 else 'there'

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Media Image

The code to the left can be written more compactly as

b = line if ('H' in line) else ''

b = line if 'H' in line

b = if 'H' in line: line else: ''

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the result of the following statement?

print([i.upper() for i in "hello"])

[‘H’, ‘E’, ‘L’, ‘L’, ‘O’]

‘HELLO’

[‘HELLO’]

HELLO

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What datatype is x after the following statements?

a = [1, 2, 3]

x = [2*i for i in a]

list

tuple

dictionary

generator

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What datatype is x after the following statements?

a = ['hello', 3.14, 7]

x = {i: 10 for i in a}

list

tuple

dictionary

generator

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the result of the following statement?

print([i if i%2==0 else i+1 for i in range(4)])

[0, 2, 2, 4]

[1, 1, 3, 3]

error

none of the mentioned

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which list comprehension will pick out only negative integers from a given list ‘a’?

[x<0 in a]

[x for x<0 in a]

[x in a for x<0]

[x for x in a if x<0]

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?