-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #567 from realpython/python-lists-tuples-update
Sample code for the article on lists vs tuples
- Loading branch information
Showing
9 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |