Flashcards: Python Strings and Lists — 89 cards

All cards

1Question

What is a string in programming?

Answer

A sequence of characters representing textual data.

2Question

How is a string literal defined in source code?

Answer

By surrounding text with single or double quotes.

3Question

What index does the first character of a string have?

Answer

Index 0.

4Question

How do you find the length of a string in Python?

Answer

Using the built-in function len⁡(string)\operatorname{len}(string).

5Question

What does the length of a string represent?

Answer

The number of characters in the string.

6Question

How is an empty string created in Python?

Answer

With two quotes, like "".

7Question

What does an empty string contain?

Answer

Zero elements.

8Question

What does the negative index -1 refer to in a string?

Answer

The rightmost character of the string.

9Question

What does string immutability mean?

Answer

A string object cannot have its characters changed in place.

10Question

Why does name[2] = "B" raise a TypeError?

Answer

Because strings do not support item assignment.

11Question

How do you replace a string character if strings are immutable?

Answer

Assign a new string object to the variable instead of modifying a character.

12Question

What is string concatenation?

Answer

Combining strings with the + operator to create a new string.

13Question

What does "New" + "York" produce?

Answer

The string "NewYork".

14Question

What types does string concatenation accept?

Answer

Only string objects.

15Question

How do you concatenate a number and a string?

Answer

Convert the number to a string with str() first.

16Question

Give an example of concatenating a number and a string.

Answer

str(332) + " Main Street".

17Question

What defines a formatted string literal (f-string) in Python?

Answer

It begins with f before its quote and uses curly braces for expressions.

18Question

What does the f-string f"{num_items} tacos cost {cost_taco * num_items}" produce if num_items=3 and cost_taco=1.25?

Answer

It produces "3 tacos cost 3.75".

19Question

What happens when you add an equals sign after an f-string expression?

Answer

It prints both the expression and its result.

20Question

What do double braces {{ and }} produce in an f-string?

Answer

They produce literal curly braces in the output.

21Question

Where is a format specification placed inside an f-string replacement field?

Answer

It follows a colon inside the replacement field.

22Question

What does the format specification {4:.2f} produce in an f-string?

Answer

It produces 4.00 with two decimal places.

23Question

Name three common f-string presentation types for formatting values.

Answer

s for strings, d for decimal integers, and f for fixed-point notation.

24Question

Which f-string presentation types represent numbers in binary and hexadecimal?

Answer

b for binary and x or X for hexadecimal.

25Question

What is a list in programming?

Answer

A mutable, ordered collection created with brackets.

26Question

At what index do list elements start?

Answer

Indexing starts at 0.

27Question

How do you create an empty list?

Answer

With empty brackets: [].

28Question

What type must a list index be?

Answer

An integer.

29Question

What happens if a list index is a float like 0.0?

Answer

It causes a runtime error.

30Question

How do you access a list element?

Answer

Using bracket notation with an integer index, like my_list[i].

31Question

Can a list contain different data types?

Answer

Yes, it can contain integers, floats, and strings together.

32Question

What happens when you assign prices[0] = 2.50 in a list?

Answer

The list element at position 0 is updated.

33Question

Which list method adds an element to the end?

Answer

The append() method.

34Question

Which list method removes an element by index?

Answer

The pop() method.

35Question

Which list method removes the first matching value?

Answer

The remove() method.

36Question

What is a sequence-type function?

Answer

A built-in function accepting a sequence without object-dot notation.

37Question

Name one list operation that returns the length of a list.

Answer

len(list) returns the length.

38Question

What type must all elements have for min() and max() to work on a list?

Answer

All elements must have the same type.

39Question

On what type of elements does sum() operate?

Answer

sum() operates only on numbers.

40Question

What is a tuple in Python?

Answer

An immutable sequence storing data elements that cannot be changed after creation.

41Question

How is a tuple typically written in Python?

Answer

As comma-separated values surrounded by parentheses.

42Question

Which functions does a tuple support?

Answer

len(), indexing, and other sequence functions.

43Question

What does the tuple white_house_coordinates = (38.8977, 77.0366) store at index 0?

Answer

Latitude.

44Question

What does the tuple white_house_coordinates store at index 1?

Answer

Longitude.

45Question

What is a named tuple?

Answer

A simple immutable data type with named fields accessible by dot notation.

46Question

What is required to create a named tuple in Python?

Answer

Import namedtuple, define the type with a name and fields, then call it with values.

47Question

What is a set in Python?

Answer

A mutable unordered collection of unique elements.

48Question

How can you create an empty set in Python?

Answer

By calling set() with no arguments.

49Question

What happens when you pass a list with duplicates to set()?

Answer

Duplicates are omitted in the resulting set.

50Question

Which method adds one value to a set?

Answer

add()

51Question

Which method adds elements from another set to a set?

Answer

update()

52Question

Which method removes a specified value from a set?

Answer

remove()

53Question

Which method removes a random element from a set?

Answer

pop()

54Question

What does the intersection of sets contain?

Answer

Elements common to all specified sets.

55Question

What is a dictionary in programming?

Answer

A mutable mapping that associates keys with values.

56Question

What types must dictionary keys be?

Answer

Immutable types such as numbers, strings, or tuples.

57Question

Can dictionary values be of any type?

Answer

Yes, dictionary values can be any type.

58Question

How do you access a dictionary entry?

Answer

By using its key in brackets.

59Question

What error occurs when accessing a non-existent dictionary key?

Answer

KeyError.

60Question

What happens when assigning to a new dictionary key?

Answer

A new key-value pair is added.

61Question

What happens when assigning to an existing dictionary key?

Answer

The existing value is replaced.

62Question

How do you remove an entry from a dictionary?

Answer

Using del dict[key].

63Question

Which Python container type represents mutable ordered data?

Answer

List

64Question

Which Python container type represents immutable ordered data?

Answer

Tuple

65Question

Which Python container type represents unordered unique data?

Answer

Set

66Question

Which Python container type represents key-value associations?

Answer

Dictionary

67Question

What is the difference between a list and a tuple in Python?

Answer

A list is mutable and ordered; a tuple is immutable and ordered.

68Question

Which Python sequence types support indexing with brackets?

Answer

Strings, lists, and tuples

69Question

Which Python sequence types support the len() function?

Answer

Strings, lists, and tuples

70Question

When should a programmer use a tuple instead of a list?

Answer

When ordered data should not change

71Question

What is type conversion in programming?

Answer

Conversion of a value from one data type to another.

72Question

When does an arithmetic operation produce a float?

Answer

If either operand is a float.

73Question

What type results from adding or multiplying two integers?

Answer

An integer.

74Question

What happens when int() converts a float like 4.9?

Answer

It truncates the fractional part, producing 4.

75Question

What do int(), float(), and str() functions create?

Answer

Integers, floating-point values, and strings respectively.

76Question

What type does input() initially return?

Answer

A string.

77Question

What does int("500") produce?

Answer

The integer 500.

78Question

What does float("1.75") produce?

Answer

The floating-point value 1.75.

79Question

What base does a binary number use and which digits does it include?

Answer

Base 2 with digits 0 and 1.

80Question

How is each position weighted in a binary number compared to a decimal number?

Answer

Binary positions use increasing powers of 2; decimal positions use increasing powers of 10.

81Question

What limits the range of values a computer variable can represent?

Answer

The finite number of bits allocated to the variable.

82Question

What base and digit weighting does a binary number use?

Answer

Base 2 with digits weighted by increasing powers of 2.

83Question

What base and digit weighting does a decimal number use?

Answer

Base 10 with digits weighted by increasing powers of 10.

84Question

What is the largest integer representable with 8-bit unsigned binary numbers?

Answer

255.

85Question

How do you convert a binary number to decimal?

Answer

Multiply each binary digit by its power of 2 and add the results.

86Question

How do you convert a decimal number to binary?

Answer

Select powers of 2 summing to the decimal and write 1s there, 0s elsewhere.

87Question

How does the program estimate age in days from years?

Answer

By multiplying age in years by 365.

88Question

What health data calculations does the practice ask the programmer to perform?

Answer

Calculate age in minutes and seconds, estimate lifetime heartbeats, sneezes, calories, and other statistics.

89Question

What is the age in days for a 3-year-old using the program's calculation?

Answer

1,095 days.

Test yourself with the quiz

Test your knowledge with 44 questions on Python Strings and Lists.

1. Which kind of data does a string represent in Python?

2. Which expression is a string literal written directly in source code?

Take the quiz →

Read the study sheet

Review the complete course in the study sheet for Python Strings and Lists.

See study sheet →

Similar courses

Create your own flashcards

Import your course and AI generates flashcards in 30 seconds.

Flashcard generator