β Must-know
A string is an ordered sequence whose first character has index 0, whose second character has index 1, and whose subsequent characters continue in order.
The length of a string is obtained with the built-in function and equals the number of characters in the string.
Further detail
π Negative string indices reference characters from the rightmost character, so alphabet[-1] refers to "Z" in the uppercase alphabet string.
Create, measure, index: assign strings, use len(), then access characters by position.
β Must-know
π An assignment such as name[2] = "B" raises a TypeError because strings do not support item assignment.
Further detail
π String concatenation accepts only string objects, so a number must first be converted with str(), as in str(332) + " Main Street".
Strings cannot be changed in place, whereas concatenation creates a new string.
β Must-know
π A format specification follows a colon inside a replacement field and controls how the evaluated value is represented, such as {4:.2f} producing 4.00.
Further detail
π Adding an equals sign after an f-string expression prints both the expression and its result, so f"{24=}" produces "24=8".
π Double braces in an f-string produce literal curly braces, so f"{{Jeff Bezos}}: Amazon" produces "{Jeff Bezos}: Amazon".
An f-string is a template with curly-braced expression slots filled during execution.
β Must-know
π A list index must be an integer; using a floating-point index such as 0.0 or 1.0 causes a runtime error.
Further detail
Strings are immutable sequences, whereas lists are mutable ordered collections.
π Because lists are mutable, assigning a value to a position such as prices[0] = 2.50 updates that list element.
The append(), pop(), and remove() methods modify lists by adding an element, removing an indexed element, and removing the first matching value, respectively.
Useful list operations include:
π The min() and max() functions require all list elements to have the same type, while sum() operates on numbers only.
Append adds at the end, pop removes by index, and remove deletes by value.
A tuple is typically written as comma-separated values surrounded by parentheses, such as (5, 15, 20), and supports len(), indexing, and other sequence functions.
The tuple white_house_coordinates = (38.8977, 77.0366) stores latitude at index 0 and longitude at index 1.
π Creating a named tuple involves these steps:
Lists can change; tuples cannot.
β Must-know
π Passing a list containing duplicate values to set() creates a set in which the duplicates are omitted.
π The intersection contains elements common to all specified sets, the union contains all unique elements, the difference contains elements in the first set but not the others, and symmetric_difference contains elements appearing in exactly one of two sets.
Further detail
π A set can be created with set(iterable) or with comma-separated elements inside braces, while an empty set must be created with set().
A bag of unique items with no numbered slots.
β Must-know
π A dictionary entry is accessed with its key in brackets, and using a key that does not exist raises a KeyError.
Further detail
π Dictionary keys must be immutable types such as numbers, strings, or tuples, while dictionary values can be any type.
Lists use positions; dictionaries use keys.
β Must-know
π A list is mutable and ordered, whereas a tuple is immutable and ordered.
π A programmer generally uses a list for ordered data that may change, a tuple for ordered data that should not change, and a dictionary for relationships between keys and values.
Further detail
π Sequence types such as strings, lists, and tuples support indexing with brackets and the len() function.
N-L-T-S-D: numeric, list, tuple, set, dictionary.
β Must-know
π An arithmetic operation produces a float if either operand is a float; otherwise, adding or multiplying integers produces an integer.
π Converting a float to an integer with int() truncates the fractional part, so int(4.9) produces 4.
Further detail
input string β int or float β calculation
β Must-know
Each position in a binary number is weighted by an increasing power of 2, while each position in a decimal number is weighted by an increasing power of 10.
An 8-bit unsigned binary number can represent values from 0 through 255, making 255 the largest integer representable with 8 bits.
To convert a binary number to decimal, multiply each binary digit by its corresponding power of 2 and add the resulting values.
Further detail
A computer stores numbers using bits, and the finite number of bits allocated to a variable limits the range of values it can represent.
To convert a decimal number to binary, select the powers of 2 whose sum equals the decimal value and write 1 in those positions and 0 in the others.
Decimal uses powers of 10; binary uses powers of 2.
β Must-know
π Formula β The program estimates age in days by multiplying the user's age in years by 365: .
Further detail
Years β days β minutes and seconds β lifetime estimates
β Must-know
A list-basics program creates my_list and your_list in input order, concatenates them into our_list, appends their_flower, replaces my_flower2 with their_flower, removes the first occurrence of their_flower without using index(), and removes the second element.
List concatenation with the + operator combines the elements of my_list followed by the elements of your_list.
Further detail
Define β concatenate β append β replace β remove β delete
β Must-know
Further detail
Adding an element that is already present in a set does not create a duplicate or change the set's contents.
For the inputs apple, peach, lemon, apple, pear, and plum, the sorted set output after initialization is ['apple', 'lemon', 'peach'].
Lists preserve order and duplicates, whereas sets keep unique elements.
| Property | Strings | Lists |
|---|---|---|
| Mutability | Immutable | Mutable |
| Ordering | Characters have indices starting at 0 | Elements have indices starting at 0 |
| Modification | Reassign the variable to replace the string | Assign, append, pop, or remove elements |
| Type | Ordering | Mutability or association |
|---|---|---|
| list | Ordered | Mutable |
| tuple | Ordered | Immutable |
| set | Unordered | Unique elements |
| dictionary | Key-based | Key-value associations |
Test your knowledge on Python Strings and Lists with 44 multiple-choice questions with detailed corrections.
1. Which kind of data does a string represent in Python?
2. Which expression is a string literal written directly in source code?
Memorize the key concepts of Python Strings and Lists with 89 interactive flashcards.
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.
Import your course and AI generates sheets, quizzes and flashcards in 30 seconds.
Sheet generator