0% found this document useful (0 votes)
4 views3 pages

Python_Lists_Tuples_Sets_Study_Guide

The document provides a study guide on Python data structures: lists, tuples, and sets. It details the characteristics, creation, manipulation, and operations of each type, highlighting lists as ordered and mutable, tuples as ordered and immutable, and sets as unordered and unique. Additionally, it includes examples of common functions and methods for each data structure.

Uploaded by

anusanu162
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python_Lists_Tuples_Sets_Study_Guide

The document provides a study guide on Python data structures: lists, tuples, and sets. It details the characteristics, creation, manipulation, and operations of each type, highlighting lists as ordered and mutable, tuples as ordered and immutable, and sets as unordered and unique. Additionally, it includes examples of common functions and methods for each data structure.

Uploaded by

anusanu162
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Lists, Tuples, and Sets - Study Guide

1. Lists - Ordered, Mutable Collections

- Lists store multiple items in a single variable.

- Can change, add, or remove items after creation.

Creating a List:

courses = ['History', 'Math', 'Physics', 'CompSci']

print(courses)

Accessing Elements:

print(courses[0]) # 'History'

print(courses[-1]) # 'CompSci' (last)

Slicing:

print(courses[0:2]) # ['History', 'Math']

print(courses[2:]) # ['Physics', 'CompSci']

Adding Items:

courses.append('Art')

courses.insert(0, 'Art')

courses2 = ['Art', 'Education']

courses.extend(courses2)

Removing Items:

courses.remove('Math')

popped = courses.pop()

Sorting and Reversing:

courses.reverse()

courses.sort()
courses.sort(reverse=True)

sorted_courses = sorted(courses)

Built-in Functions:

nums = [1, 5, 4, 3, 2]

print(min(nums)), print(max(nums)), print(sum(nums))

Searching:

print(courses.index('CompSci'))

print('Math' in courses)

Looping:

for course in courses: print(course)

for index, course in enumerate(courses): print(index, course)

Joining and Splitting:

course_str = ', '.join(courses)

new_list = course_str.split(', ')

2. Tuples - Ordered, Immutable Collections

courses = ('History', 'Math', 'Physics', 'CompSci')

# Tuples cannot be changed.

3. Sets - Unordered, Unique Collections

cs_courses = {'History', 'Math', 'Physics', 'Math'}

print(cs_courses) # {'History', 'Math', 'Physics'}

Set Operations:

cs_courses.intersection(art_courses)

cs_courses.difference(art_courses)

cs_courses.union(art_courses)
Summary Table:

List - Ordered, Duplicates Allowed, Mutable

Tuple - Ordered, Duplicates Allowed, Immutable

Set - Unordered, Unique, Mutable

You might also like