Skip to content

Commit 7dd6834

Browse files
authored
Merge pull request #567 from realpython/python-lists-tuples-update
Sample code for the article on lists vs tuples
2 parents f5140c8 + 935c02b commit 7dd6834

File tree

9 files changed

+97
-0
lines changed

9 files changed

+97
-0
lines changed

python-lists-tuples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Lists vs Tuples in Python
2+
3+
This folder provides the code examples for the Real Python tutorial [Lists vs Tuples in Python](https://realpython.com/python-lists-tuples/).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
colors = ["red", "green", "blue", "yellow"]
2+
print(colors)
3+
4+
person = ("Jane Doe", 25, "Python Developer", "Canada")
5+
print(person)
6+
7+
digits = list(range(10))
8+
print(digits)
9+
10+
even_digits = [number for number in range(1, 10) if not number % 2]
11+
print(even_digits)
12+
13+
print(["Pythonista", 7, False, 3.14159])
14+
print(("Pythonista", 7, False, 3.14159))
15+
16+
print(list(range(1_000_000)))
17+
print(tuple(range(1_000_000)))

python-lists-tuples/functions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
numbers = [2, 7, 5, 4, 8]
2+
print(len(numbers))
3+
print(min(numbers))
4+
print(max(numbers))
5+
print(sum(numbers))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
a = ["a", "b"]
2+
a.append("c")
3+
print(a)
4+
5+
a = ["a", "c"]
6+
a.insert(1, "b")
7+
print(a)
8+
9+
a = ["a", "b", "c", "d", "e"]
10+
a.remove("b")
11+
print(a)
12+
a.remove("c")
13+
print(a)
14+
15+
a = ["a", "b", "c", "d", "e"]
16+
a.pop()
17+
print(a)
18+
a.pop()
19+
print(a)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
x = ["a", ["bb", ["ccc", "ddd"], "ee", "ff"], "g", ["hh", "ii"], "j"]
2+
3+
print(x[0], x[2], x[4])
4+
5+
print(x[1])
6+
print(x[3])
7+
8+
print(x[1][0])
9+
print(x[1][1])
10+
print(x[1][2])
11+
print(x[1][3])

python-lists-tuples/operators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
words = ["foo", "bar", "baz", "qux", "quux", "corge"]
2+
print("qux" in words)
3+
print("py" in words)
4+
print("thud" not in words)
5+
6+
print(words + ["grault", "garply"])
7+
print(words * 2)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fruits = ["apple", "orange", "mango", "grape"]
2+
del fruits[0] # Remove apple
3+
4+
print(fruits)
5+
6+
person = ("John Doe", 35, "Web Dev")
7+
# del person[1] # Try to remove the age value

python-lists-tuples/slicing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
numbers = [1, 2, 3, 7]
2+
numbers[3:4] = [4, 5, 6, 7]
3+
print(numbers)
4+
5+
numbers = [1, 2, 3, 7]
6+
numbers[3:3] = [4, 5, 6]
7+
print(numbers)

python-lists-tuples/unpacking.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
t = ("foo", "bar", "baz", "qux")
2+
3+
s1, s2, s3, s4 = t
4+
print(s1)
5+
print(s2)
6+
print(s3)
7+
print(s4)
8+
9+
a = "foo"
10+
b = "bar"
11+
# Using a temporary variable
12+
temp = a
13+
a = b
14+
b = temp
15+
print(a, b)
16+
17+
a = "foo"
18+
b = "bar"
19+
# Using unpacking
20+
a, b = b, a
21+
print(a, b)

0 commit comments

Comments
 (0)