Search Header Logo

Python Basics: Dictionaries and Strings

Authored by Jeff Thuong

Computers

University - Professional Development

Dictionaries covered

Used 4+ times

Python Basics: Dictionaries and Strings
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

10 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How to define an empty dictionary?

my_dict = {}

my_dict = empty_dict()

my_dict = []

my_dict = ()

Answer explanation

There are two ways to define an empty dictionary:

* my_dict = {}

* my_dict = dict()

Tags

Dictionaries

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How to define a dictionary with one person (name) and its age?

ages = {"Abraham"=1000}

ages = ["Abraham": 1000]

ages = {"Abraham": 1000}

ages = ["Abraham"=1000]

Answer explanation

Curly brackets {...} for a dictionary and : to match a key to its value

Tags

Dictionaries

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How to access the value matching a key "China" in a dictionary countries?

countries{"China"}

countries["China"]

countries("China")

countries.China

Answer explanation

To access a value, we always use [...], shall it be in lists, strings (character or slice), tuples, or dictionaries

Tags

Dictionaries

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What does the following do:

my_dictionary = {"one":1, "two:2}

my_dictionary["one"] = 111

Replace the value of "one" with 111

Nothing because we already have a value "one"

Store a second value "one" with value 111

Raise an Error

Answer explanation

In a dictionary, the keys are unique, and we have a matching value for each.

Setting a new value will replace the previous one (because there is a simple way to set a value only if it does not exist).

Tags

Dictionaries

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How to check a dictionary my_dict has a certain key my_key?

my_key in my_dict

my_dict.has(my_key)

my_dict has my_key

my_dict in my_key

Answer explanation

The keyword in is used:

* To loop in "for loops"

* To check if an element is part of a list, string, or dictionary

Tags

Dictionaries

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What does the split method do (e.g. my_string.strip(...))?

Return a string with all spaces removed, even in the middle

Return a string with all spaces at the beginning and end removed

Return a list of strings (my_string being cut into slices)

Modify my_string by removing spaces and return nothing

Answer explanation

The methods on the string (like .strip() or .split(), etc.) does not change the initial string but return a modified one.

Tags

Strings

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What does the following do?

"1-2-3-4-5-6".split("-")

Return a list of numbers

[1, 2, 3, 4, 5]

Return a list of strings

["1", "2", "3", "4", "5"]

Return a string

"123456"

Raise an Error

Answer explanation

Split is cutting into a list of the strings (even if it's numbers) based on a keyword passed as argument.

Note that we can apply method directly on strings (or lists, dictionaries) without going through a variable.

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?

Discover more resources for Computers