Python Tutorial for Beginners - 3. Lists, Tuples, and Sets

Table of Contents

  1. Run Code 1 in Python
  2. Run Code 2 in Python
  3. Run Code 3 in Python
  4. Run Code 4 in Python
  5. Run Code 5 in Python
  6. Run Code 6 in Python
  7. Run Code 7 in Python
  8. Run Code 8 in Python
  9. Run Code 9 in Python
  10. Run Code 10 in Python
  11. Run Code 11 in Python
  12. Run Code 12 in Python
  13. Run Code 13 in Python
  14. Run Code 14 in Python
  15. Run Code 15 in Python
  16. Run Code 16 in Python
  17. Run Code 17 in Python
  18. Run Code 18 in Python
  19. Run Code 19 in Python
  20. Run Code 20 in Python
  21. Run Code 21 in Python
  22. Run Code 22 in Python
  23. Run Code 23 in Python
  24. Run Code 24 in Python
  25. Run Code 25 in Python
  26. Run Code 26 in Python
  27. Run Code 27 in Python
  28. Run Code 28 in Python
  29. Run Code 29 in Python
  30. Run Code 30 in Python
  31. Run Code 31 in Python
  32. Run Code 32 in Python
  33. Run Code 33 in Python
  34. Run Code 34 in Python
  35. Run Code 35 in Python
  36. Run Code 36 in Python
  37. Run Code 37 in Python
  38. Run Code 38 in Python
  39. Run Code 39 in Python
  40. Run Code 40 in Python
  41. Run Code 41 in Python
  42. Run Code 42 in Python

Run Code 1 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses)

start.py

Run Code 2 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(len(courses))

start.py

Run Code 3 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[0])

start.py

Run Code 4 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[3])

start.py

Run Code 5 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[-1])

start.py

Run Code 6 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[4]) #list index out of range

start.py

Run Code 7 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[0:2])

start.py

Run Code 8 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[:2])

start.py

Run Code 9 in Python

1
2
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[2:])

start.py

Run Code 10 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']

courses.append('Art')

print(courses) 

start.py

Run Code 11 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']

courses.insert(0, 'Art')

print(courses)

start.py

Run Code 12 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']
courses_2 = ['Art', 'Education']
courses.insert(0, courses_2)

print(courses)

start.py

Run Code 13 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']
courses_2 = ['Art', 'Education']
courses.insert(0, courses_2)

print(courses[0])

start.py

Run Code 14 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']
courses_2 = ['Art', 'Education']
courses.extend(courses_2)

print(courses)

start.py

Run Code 15 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']
courses_2 = ['Art', 'Education']
courses.append(courses_2)

print(courses)

start.py

Run Code 16 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']

courses.remove('Math')

print(courses)

start.py

Run Code 17 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']

courses.pop()

print(courses)

start.py

Run Code 18 in Python

1
2
3
4
5
6
courses = ['History', 'Math', 'Physics', 'CompSci']

popped = courses.pop()

print(popped) 
print(courses)

start.py

Run Code 19 in Python

1
2
3
4
courses = ['History', 'Math', 'Physics', 'CompSci']

courses.reverse()
print(courses)

start.py

Run Code 20 in Python

1
2
3
4
courses = ['History', 'Math', 'Physics', 'CompSci']

courses.sort()
print(courses)

start.py

Run Code 21 in Python

1
2
3
4
5
6
7
8
9
courses = ['History', 'Math', 'Physics', 'CompSci']

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

courses.sort()
nums.sort()

print(courses) 
print(nums)

start.py

Run Code 22 in Python

start.py

Run Code 23 in Python

start.py

Run Code 24 in Python

1
2
3
4
5
6
7
8
9
courses = ['History', 'Math', 'Physics', 'CompSci']

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

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

print(courses) 
print(nums)

start.py

Run Code 25 in Python

1
2
3
4
5
6
7
courses = ['History', 'Math', 'Physics', 'CompSci']

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

sorted(courses)

print(courses)

start.py

Run Code 26 in Python

1
2
3
4
5
6
7
courses = ['History', 'Math', 'Physics', 'CompSci']

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

sorted_courses = sorted(courses)

print(sorted_courses)

start.py

Run Code 27 in Python

1
2
3
4
5
6
7
8
9
10
courses = ['History', 'Math', 'Physics', 'CompSci']

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

sorted_courses = sorted(courses)

print(sorted_courses) 
print(min(nums))
print(max(nums))
print(sum(nums))

start.py

Run Code 28 in Python

1
2
3
4
5
courses = ['History', 'Math', 'Physics', 'CompSci']

print(courses.index('Math'))

print('Math' in courses)

start.py

Run Code 29 in Python

1
2
3
4
courses = ['History', 'Math', 'Physics', 'CompSci']

for item in courses:
    print(item)

start.py

Run Code 30 in Python

1
2
3
4
courses = ['History', 'Math', 'Physics', 'CompSci']

for index, courses in enumerate (courses, start =1):
    print(index, courses)

start.py

Run Code 31 in Python

1
2
3
4
courses = ['History', 'Math', 'Physics', 'CompSci']

courses_str = ', '.join(courses)
print(courses_str)

start.py

Run Code 32 in Python

1
2
3
4
courses = ['History', 'Math', 'Physics', 'CompSci']

courses_str = ' - '.join(courses)
print(courses_str)

start.py

Run Code 33 in Python

1
2
3
4
5
6
7
8
courses = ['History', 'Math', 'Physics', 'CompSci']

courses_str = ' - '.join(courses)

new_list = courses_str.split(' - ')

print(courses_str)
print(new_list)

start.py

Run Code 34 in Python

1
2
3
4
5
6
7
8
9
10
11
# Mutable
list_1 = ['History', 'Math', 'Physics', 'CompSci']
list_2 = list_1

print(list_1)
print(list_2)

list_1[0] = 'Art'

print(list_1)
print(list_2)

start.py

Run Code 35 in Python

1
2
3
4
5
6
7
8
9
10
11
# Immutable
tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
tuple_2 = tuple_1

print(tuple_1)
print(tuple_2)

# tuple_1[0] = 'Art'

# print(tuple_1)
# print(tuple_2)

start.py

Run Code 36 in Python

1
2
3
4
5
6
7
8
9
10
11
# Immutable
tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
tuple_2 = tuple_1

print(tuple_1)
print(tuple_2)

tuple_1[0] = 'Art'

print(tuple_1)
print(tuple_2)

start.py

Run Code 37 in Python

1
2
3
4
# Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}

print(cs_courses)

start.py

Run Code 38 in Python

1
2
3
4
# Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci', 'Math'}

print(cs_courses)

start.py

Run Code 39 in Python

1
2
3
4
5
# Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'CompSci'}

print(cs_courses.intersection(art_courses))

start.py

Run Code 40 in Python

1
2
3
4
5
# Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'CompSci'}

print(cs_courses.difference(art_courses))

start.py

Run Code 41 in Python

1
2
3
4
5
# Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'CompSci'}

print(cs_courses.union(art_courses))

start.py

Run Code 42 in Python

1
2
3
4
5
6
7
8
9
10
11
# Empty Lists
empty_list = []
empty_list = list()

# Empty Tuples
empty_tuple = ()
empty_tuple = tuple()

# Empty Sets
empty_set = {} # This isn't right! It's a dict
empty_set = set()

start.py