Aug 14, 2024
[]
to define a list.courses = ["History", "Math", "Physics", "Comp Sci"]
len()
to find the number of items in a list.
len(courses) # Returns 4
courses[0] # Returns "History"
courses[3] # Returns "Comp Sci"
courses[-1] # Returns "Comp Sci"
courses[0:2] # Returns ["History", "Math"]
courses[2:] # Returns ["Physics", "Comp Sci"]
courses.append("Art")
courses.insert(0, "Art") # Inserts "Art" at index 0
courses.extend(["Art", "Education"])
courses.remove("Math")
popped_course = courses.pop()
courses.reverse()
courses.sort()
sorted_courses = sorted(courses)
min(numbers), max(numbers), sum(numbers)
"Art" in courses
for
loops to iterate through a list.
for item in courses:
print(item)
for index, course in enumerate(courses):
print(index, course)
course_string = ', '.join(courses)
new_list = course_string.split(', ')
()
instead of square brackets.
courses_tuple = ("History", "Math", "Physics")
{}
or set()
function.
courses_set = {"History", "Math", "Physics"}
"Math" in courses_set
[]
or list()
.()
or tuple()
.set()
(not {}
as it creates an empty dictionary).