What is a string in programming?
A sequence of characters representing textual data.
How is a string literal defined in source code?
By surrounding text with single or double quotes.
What index does the first character of a string have?
Index 0.
How do you find the length of a string in Python?
Using the built-in function .
What does the length of a string represent?
The number of characters in the string.
How is an empty string created in Python?
With two quotes, like "".
What does an empty string contain?
Zero elements.
What does the negative index -1 refer to in a string?
The rightmost character of the string.
What does string immutability mean?
A string object cannot have its characters changed in place.
Why does name[2] = "B" raise a TypeError?
Because strings do not support item assignment.
How do you replace a string character if strings are immutable?
Assign a new string object to the variable instead of modifying a character.
What is string concatenation?
Combining strings with the + operator to create a new string.
What does "New" + "York" produce?
The string "NewYork".
What types does string concatenation accept?
Only string objects.
How do you concatenate a number and a string?
Convert the number to a string with str() first.
Give an example of concatenating a number and a string.
str(332) + " Main Street".
What defines a formatted string literal (f-string) in Python?
It begins with f before its quote and uses curly braces for expressions.
What does the f-string f"{num_items} tacos cost {cost_taco * num_items}" produce if num_items=3 and cost_taco=1.25?
It produces "3 tacos cost 3.75".
What happens when you add an equals sign after an f-string expression?
It prints both the expression and its result.
What do double braces {{ and }} produce in an f-string?
They produce literal curly braces in the output.
Where is a format specification placed inside an f-string replacement field?
It follows a colon inside the replacement field.
What does the format specification {4:.2f} produce in an f-string?
It produces 4.00 with two decimal places.
Name three common f-string presentation types for formatting values.
s for strings, d for decimal integers, and f for fixed-point notation.
Which f-string presentation types represent numbers in binary and hexadecimal?
b for binary and x or X for hexadecimal.
What is a list in programming?
A mutable, ordered collection created with brackets.
At what index do list elements start?
Indexing starts at 0.
How do you create an empty list?
With empty brackets: [].
What type must a list index be?
An integer.
What happens if a list index is a float like 0.0?
It causes a runtime error.
How do you access a list element?
Using bracket notation with an integer index, like my_list[i].
Can a list contain different data types?
Yes, it can contain integers, floats, and strings together.
What happens when you assign prices[0] = 2.50 in a list?
The list element at position 0 is updated.
Which list method adds an element to the end?
The append() method.
Which list method removes an element by index?
The pop() method.
Which list method removes the first matching value?
The remove() method.
What is a sequence-type function?
A built-in function accepting a sequence without object-dot notation.
Name one list operation that returns the length of a list.
len(list) returns the length.
What type must all elements have for min() and max() to work on a list?
All elements must have the same type.
On what type of elements does sum() operate?
sum() operates only on numbers.
What is a tuple in Python?
An immutable sequence storing data elements that cannot be changed after creation.
How is a tuple typically written in Python?
As comma-separated values surrounded by parentheses.
Which functions does a tuple support?
len(), indexing, and other sequence functions.
What does the tuple white_house_coordinates = (38.8977, 77.0366) store at index 0?
Latitude.
What does the tuple white_house_coordinates store at index 1?
Longitude.
What is a named tuple?
A simple immutable data type with named fields accessible by dot notation.
What is required to create a named tuple in Python?
Import namedtuple, define the type with a name and fields, then call it with values.
What is a set in Python?
A mutable unordered collection of unique elements.
How can you create an empty set in Python?
By calling set() with no arguments.
What happens when you pass a list with duplicates to set()?
Duplicates are omitted in the resulting set.
Which method adds one value to a set?
add()
Which method adds elements from another set to a set?
update()
Which method removes a specified value from a set?
remove()
Which method removes a random element from a set?
pop()
What does the intersection of sets contain?
Elements common to all specified sets.
What is a dictionary in programming?
A mutable mapping that associates keys with values.
What types must dictionary keys be?
Immutable types such as numbers, strings, or tuples.
Can dictionary values be of any type?
Yes, dictionary values can be any type.
How do you access a dictionary entry?
By using its key in brackets.
What error occurs when accessing a non-existent dictionary key?
KeyError.
What happens when assigning to a new dictionary key?
A new key-value pair is added.
What happens when assigning to an existing dictionary key?
The existing value is replaced.
How do you remove an entry from a dictionary?
Using del dict[key].
Which Python container type represents mutable ordered data?
List
Which Python container type represents immutable ordered data?
Tuple
Which Python container type represents unordered unique data?
Set
Which Python container type represents key-value associations?
Dictionary
What is the difference between a list and a tuple in Python?
A list is mutable and ordered; a tuple is immutable and ordered.
Which Python sequence types support indexing with brackets?
Strings, lists, and tuples
Which Python sequence types support the len() function?
Strings, lists, and tuples
When should a programmer use a tuple instead of a list?
When ordered data should not change
What is type conversion in programming?
Conversion of a value from one data type to another.
When does an arithmetic operation produce a float?
If either operand is a float.
What type results from adding or multiplying two integers?
An integer.
What happens when int() converts a float like 4.9?
It truncates the fractional part, producing 4.
What do int(), float(), and str() functions create?
Integers, floating-point values, and strings respectively.
What type does input() initially return?
A string.
What does int("500") produce?
The integer 500.
What does float("1.75") produce?
The floating-point value 1.75.
What base does a binary number use and which digits does it include?
Base 2 with digits 0 and 1.
How is each position weighted in a binary number compared to a decimal number?
Binary positions use increasing powers of 2; decimal positions use increasing powers of 10.
What limits the range of values a computer variable can represent?
The finite number of bits allocated to the variable.
What base and digit weighting does a binary number use?
Base 2 with digits weighted by increasing powers of 2.
What base and digit weighting does a decimal number use?
Base 10 with digits weighted by increasing powers of 10.
What is the largest integer representable with 8-bit unsigned binary numbers?
255.
How do you convert a binary number to decimal?
Multiply each binary digit by its power of 2 and add the results.
How do you convert a decimal number to binary?
Select powers of 2 summing to the decimal and write 1s there, 0s elsewhere.
How does the program estimate age in days from years?
By multiplying age in years by 365.
What health data calculations does the practice ask the programmer to perform?
Calculate age in minutes and seconds, estimate lifetime heartbeats, sneezes, calories, and other statistics.
What is the age in days for a 3-year-old using the program's calculation?
1,095 days.
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?
Review the complete course in the study sheet for Python Strings and Lists.
See study sheet →Import your course and AI generates flashcards in 30 seconds.
Flashcard generator