Skip to content

Commit

Permalink
Merge pull request #567 from realpython/python-lists-tuples-update
Browse files Browse the repository at this point in the history
Sample code for the article on lists vs tuples
  • Loading branch information
brendaweles authored Aug 23, 2024
2 parents f5140c8 + 935c02b commit 7dd6834
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python-lists-tuples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Lists vs Tuples in Python

This folder provides the code examples for the Real Python tutorial [Lists vs Tuples in Python](https://realpython.com/python-lists-tuples/).
17 changes: 17 additions & 0 deletions python-lists-tuples/create_lists_tuples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
colors = ["red", "green", "blue", "yellow"]
print(colors)

person = ("Jane Doe", 25, "Python Developer", "Canada")
print(person)

digits = list(range(10))
print(digits)

even_digits = [number for number in range(1, 10) if not number % 2]
print(even_digits)

print(["Pythonista", 7, False, 3.14159])
print(("Pythonista", 7, False, 3.14159))

print(list(range(1_000_000)))
print(tuple(range(1_000_000)))
5 changes: 5 additions & 0 deletions python-lists-tuples/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
numbers = [2, 7, 5, 4, 8]
print(len(numbers))
print(min(numbers))
print(max(numbers))
print(sum(numbers))
19 changes: 19 additions & 0 deletions python-lists-tuples/list_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
a = ["a", "b"]
a.append("c")
print(a)

a = ["a", "c"]
a.insert(1, "b")
print(a)

a = ["a", "b", "c", "d", "e"]
a.remove("b")
print(a)
a.remove("c")
print(a)

a = ["a", "b", "c", "d", "e"]
a.pop()
print(a)
a.pop()
print(a)
11 changes: 11 additions & 0 deletions python-lists-tuples/nested_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
x = ["a", ["bb", ["ccc", "ddd"], "ee", "ff"], "g", ["hh", "ii"], "j"]

print(x[0], x[2], x[4])

print(x[1])
print(x[3])

print(x[1][0])
print(x[1][1])
print(x[1][2])
print(x[1][3])
7 changes: 7 additions & 0 deletions python-lists-tuples/operators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
words = ["foo", "bar", "baz", "qux", "quux", "corge"]
print("qux" in words)
print("py" in words)
print("thud" not in words)

print(words + ["grault", "garply"])
print(words * 2)
7 changes: 7 additions & 0 deletions python-lists-tuples/remove_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fruits = ["apple", "orange", "mango", "grape"]
del fruits[0] # Remove apple

print(fruits)

person = ("John Doe", 35, "Web Dev")
# del person[1] # Try to remove the age value
7 changes: 7 additions & 0 deletions python-lists-tuples/slicing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
numbers = [1, 2, 3, 7]
numbers[3:4] = [4, 5, 6, 7]
print(numbers)

numbers = [1, 2, 3, 7]
numbers[3:3] = [4, 5, 6]
print(numbers)
21 changes: 21 additions & 0 deletions python-lists-tuples/unpacking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
t = ("foo", "bar", "baz", "qux")

s1, s2, s3, s4 = t
print(s1)
print(s2)
print(s3)
print(s4)

a = "foo"
b = "bar"
# Using a temporary variable
temp = a
a = b
b = temp
print(a, b)

a = "foo"
b = "bar"
# Using unpacking
a, b = b, a
print(a, b)

0 comments on commit 7dd6834

Please sign in to comment.