Summary of "Lecture 3 : List & Tuple in Python | Python Full Course"
Lecture 3 — Lists & Tuples in Python
Main ideas
- Python has built-in sequence types: lists and tuples. They behave similarly to arrays in other languages but have Python-specific behaviors.
- Lists are mutable ordered collections; tuples are immutable ordered collections.
- Both support indexing, negative indexing, and slicing.
Lists
Definition and basics
- A list is a mutable, ordered collection created with square brackets:
- Example:
marks = [94.4, 95.2, 66.4, 45.1]
- Example:
- Lists can store mixed data types (strings, ints, floats, etc.).
- Indexing is 0-based:
marks[0],marks[1], … - Negative indices are allowed (
-1is the last element,-2the second last, …). - Slicing:
marks[start:end]returns elements fromstartup to but not includingend. Omittingstartuses0; omittingenduses the list length. Negative indices also work in slices. - Mutability: you can assign to elements (
marks[0] = new_value) and many list methods modify the list in-place.
Common list methods (behavior notes)
append(x)— add elementxto the end. Mutates list; returnsNone.insert(i, x)— insertxat indexi, shifting subsequent elements right.remove(x)— remove the first occurrence ofx.pop([i])— remove and return element at indexi; default is last element.sort(reverse=False)— sort in-place (ascending by default). Usesort(reverse=True)for descending. ReturnsNone; usesorted(list)to get a new sorted list.reverse()— reverse elements in-place. ReturnsNone.copy()— return a shallow copy of the list.count(x)— return the number of occurrences ofx.- There are other list-specific methods; consult the documentation or IDE suggestions.
Important: Many list-mutating methods (e.g.,
append,sort,reverse,insert,remove) change the list in-place and returnNone. Printing the result of such a method call will showNone.
Tuples
Definition and basics
- A tuple is an immutable, ordered collection created with parentheses:
- Example:
t = (1, 2, 3)(parentheses are optional for multiple elements; commas define the tuple).
- Example:
- Once created, you cannot change, add, or remove elements. Attempting
t[0] = 5raises aTypeError. - Single-element tuple syntax requires a trailing comma:
t = (1,). (t = (1)is just the integer1.) - Empty tuple:
t = (). - Indexing, negative indexing, and slicing work the same as for lists.
- Tuple methods:
index(x)(first index ofx) andcount(x)(number of occurrences).
Key differences (lists vs tuples)
- Mutability: lists are mutable; tuples are immutable.
- Typical usage: use lists for collections that change; use tuples when immutability is desired (and for some performance/semantic benefits).
- Python lists can mix types; arrays in C++/Java typically enforce same-type elements.
Step-by-step instructions / Examples
1. Creating and printing a list
marks = [94.4, 95.2, 66.4, 45.1]
print(marks)
print(type(marks))
print(len(marks))
2. Indexing and slicing
- Access element:
marks[i]whereiis 0-based. - Slice:
marks[start:end](end is excluded). - Missing
start→ starts at 0. Missingend→ goes to the end. - Negative indices:
marks[-3:-1]returns elements at-3and-2.
3. Mutating lists
marks[0] = new_value # assign to index
marks.append(4) # append
marks.insert(index, value) # insert
marks.remove(value) # remove first occurrence
removed = marks.pop(index) # pop by index (returns removed element)
marks.reverse() # reverse in-place
marks.sort() # sort in-place (ascending)
marks.sort(reverse=True) # sort in-place (descending)
copy = marks.copy() # shallow copy
count = marks.count(value) # count occurrences
4. Creating tuples
t = (1, 2, 3) # or t = 1, 2, 3
single = (1,) # single-element tuple needs trailing comma
empty = () # empty tuple
# Access:
first = t[0]
# You cannot modify: t[0] = x # raises TypeError
5. Palindrome check (list)
- Algorithm 1 (using copy + reverse):
copy = lst.copy()
copy.reverse()
if copy == lst:
# palindrome
else:
# not palindrome
- Algorithm 2 (concise, using slicing):
if lst == lst[::-1]:
# palindrome
6. Read three favorite movies from user into a list
- Option A (use variables):
movie1 = input()
movie2 = input()
movie3 = input()
movies = []
movies.append(movie1)
movies.append(movie2)
movies.append(movie3)
- Option B (direct append inputs):
movies = []
movies.append(input("Enter movie 1: "))
movies.append(input("Enter movie 2: "))
movies.append(input("Enter movie 3: "))
- Option C (build list directly):
movies = [input("Movie 1: "), input("Movie 2: "), input("Movie 3: ")]
7. Count grade ‘A’ occurrences in a tuple
grades = ('C', 'D', 'A', 'A', 'B', 'B', 'A')
count_A = grades.count('A') # returns 3
8. Convert tuple to list and sort alphabetically
grades_list = list(grades)
grades_list.sort() # sorts ascending (A → D)
Pitfalls and important notes
- Mutating methods often return
None(they operate in-place). If you need a new object, use functions that return new values (e.g.,sorted()instead oflist.sort()). - Boolean literals are capitalized:
True,False. Usingtrue/false(lowercase) will cause aNameError. - Single-element tuples require a trailing comma:
t = (1,). Otherwise parentheses are just grouping. list.copy()returns a shallow copy — sufficient for simple element comparisons, but watch out for nested mutable objects.
Practice problems (covered)
- Read three movie names from user and store them in a list (multiple approaches).
- Check whether a list is a palindrome (use copy + reverse or slicing).
- Count the number of students with grade
'A'in a tuple (usetuple.count). - Convert a tuple to a list and sort from A to D (
list(grades)thensort()).
References / Tools mentioned
- VS Code (editor/IDE) — useful for inspecting methods and autocomplete.
- Instructor referenced searching documentation or a site transcribed as “googlethalli.com” (may be a transcription error).
pythonanywhere.commentioned at the end of the transcript.
Note: The transcript used auto-generated subtitles; some names/websites may be transcribed incorrectly.
Speakers / sources
- Unnamed instructor / lecture presenter (YouTube video)
- Tools/sources mentioned: VS Code, (transcribed) googlethalli.com, pythonanywhere.com
Category
Educational
Share this summary
Is the summary off?
If you think the summary is inaccurate, you can reprocess it with the latest model.
Preparing reprocess...