🐍 Comprehensive Analysis of Python Collection Data Types and Conditional Statements
Python provides various collection data types, each with its unique characteristics and applicable scenarios. This article will comprehensively introduce the four collection types: lists, tuples, sets, and dictionaries, as well as the use of conditional statements in Python.
1. Python Collection Data Types
Python has four main collection data types, each with its features:
| Type | Order | Mutability | Allows Duplicates | Example |
|---|---|---|---|---|
| List | Ordered | Mutable | Allowed | <span>["apple", "banana", "cherry"]</span> |
| Tuple | Ordered | Immutable | Allowed | <span>("apple", "banana", "cherry")</span> |
| Set | Unordered | Mutable | Not Allowed | <span>{"apple", "banana", "cherry"}</span> |
| Dictionary | Unordered (Ordered in 3.7+) | Mutable | Keys Not Allowed | <span>{"name": "John", "age": 30}</span> |
2. List Methods
Lists are the most commonly used mutable sequence type in Python, providing a rich set of methods:
| Method | Description | Example |
|---|---|---|
<span>append()</span> |
Adds an element to the end | <span>lst.append(4)</span> |
<span>clear()</span> |
Clears the list | <span>lst.clear()</span> |
<span>copy()</span> |
Copies the list | <span>new_lst = lst.copy()</span> |
<span>count()</span> |
Counts occurrences of an element | <span>lst.count(2)</span> |
<span>extend()</span> |
Extends the list | <span>lst.extend([4,5])</span> |
<span>index()</span> |
Returns the index of an element | <span>lst.index(3)</span> |
<span>insert()</span> |
Inserts an element at a specified position | <span>lst.insert(1, "x")</span> |
<span>pop()</span> |
Removes and returns an element at a specified position | <span>lst.pop(1)</span> |
<span>remove()</span> |
Removes the first matching element | <span>lst.remove(2)</span> |
<span>reverse()</span> |
Reverses the list | <span>lst.reverse()</span> |
<span>sort()</span> |
Sorts the list | <span>lst.sort()</span> |
3. Tuple Methods
Tuples are immutable sequences with fewer methods:
| Method | Description | Example |
|---|---|---|
<span>count()</span> |
Counts occurrences of an element | <span>tup.count(2)</span> |
<span>index()</span> |
Returns the index of an element | <span>tup.index(3)</span> |
4. Set Methods
Sets are unordered collections of unique elements, providing rich mathematical operation methods:
| Method | Description | Example |
|---|---|---|
<span>add()</span> |
Adds an element | <span>s.add(4)</span> |
<span>clear()</span> |
Clears the set | <span>s.clear()</span> |
<span>copy()</span> |
Copies the set | <span>new_s = s.copy()</span> |
<span>difference()</span> |
Returns the difference | <span>s.difference(t)</span> |
<span>intersection()</span> |
Returns the intersection | <span>s.intersection(t)</span> |
<span>union()</span> |
Returns the union | <span>s.union(t)</span> |
<span>remove()</span> |
Removes an element (raises error if not found) | <span>s.remove(2)</span> |
<span>discard()</span> |
Removes an element (does not raise error if not found) | <span>s.discard(2)</span> |
5. Dictionary Methods
Dictionaries are collections of key-value pairs, providing various operation methods:
| Method | Description | Example |
|---|---|---|
<span>clear()</span> |
Clears the dictionary | <span>d.clear()</span> |
<span>copy()</span> |
Copies the dictionary | <span>new_d = d.copy()</span> |
<span>fromkeys()</span> |
Creates a new dictionary from keys | <span>dict.fromkeys(keys, value)</span> |
<span>get()</span> |
Gets the value for a key (returns None if not found) | <span>d.get("key")</span> |
<span>items()</span> |
Returns a view of key-value pairs | <span>d.items()</span> |
<span>keys()</span> |
Returns a view of keys | <span>d.keys()</span> |
<span>pop()</span> |
Removes and returns the value for a specified key | <span>d.pop("key")</span> |
<span>update()</span> |
Updates the dictionary | <span>d.update({"k": "v"})</span> |
<span>values()</span> |
Returns a view of values | <span>d.values()</span> |
6. Python Conditional Statements
Python uses the if statement for conditional judgment, supporting various comparison operators:
1. Comparison Operators
| Operator | Description | Example |
|---|---|---|
<span>==</span> |
Equal | <span>a == b</span> |
<span>!=</span> |
Not Equal | <span>a != b</span> |
<span>></span> |
Greater Than | <span>a > b</span> |
<span><</span> |
Less Than | <span>a < b</span> |
<span>>=</span> |
Greater Than or Equal | <span>a >= b</span> |
<span><=</span> |
Less Than or Equal | <span>a <= b</span> |
2. Basic Structure of if Statement
Example Code
age = 18
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")
3. pass Statement
When the if statement block is empty, you can use pass to avoid syntax errors:
Example Code
x = 5
if x > 10:
# To be implemented
pass
else:
print("x is not greater than 10")