Python offers a wide range of versatile and powerful data structures. Among these, Lists, Tuples, Sets, and Dictionaries are fundamental to storing and organizing data efficiently. These built-in collection types support various operations that are crucial in almost every Python program, whether you're manipulating text, performing calculations, or managing datasets.
Python provides built-in types to store collections of items. Each type has distinct characteristics, making it suitable for specific use cases. Letβs explore these types in detail:
A list is an ordered collection of items. Lists are mutable, which means their elements can be changed after the list is created.
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
print(fruits[0]) # "apple"
print(fruits[-1]) # "cherry" (last item)
fruits[1] = "blueberry"
print(fruits) # ["apple", "blueberry", "cherry"]
fruits.append("date")
fruits.sort()
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4]) # [1, 2, 3]
squares = [x**2 for x in range(10)]
A tuple is similar to a list, but it is immutable, meaning its contents cannot be altered once defined. Tuples are useful for fixed collections of items.
coordinates = (10.0, 20.0)
single_item = (5,) # Comma required for single-element tuple
print(coordinates[0]) # 10.0
coordinates[0] = 15 # This will raise TypeError
point = (1, 2)
x, y = point
my_tuple = (1, 2, 2, 3)
print(my_tuple.count(2)) # 2
print(my_tuple.index(3)) # 3
A set is an unordered collection of unique items. It is mutable but only stores immutable (hashable) objects.
my_set = {1, 2, 3}
another_set = set([4, 5, 6])
my_set.add(4)
my_set.remove(2)
my_set.discard(100) # Won't raise error if not present
a = {1, 2, 3}
b = {2, 3, 4}
print(a.union(b)) # {1, 2, 3, 4}
print(a.intersection(b)) # {2, 3}
Dictionaries store data as key-value pairs. Keys must be unique and immutable. Values can be any type.
person = {"name": "Alice", "age": 25}
print(person["name"]) # Alice
person["age"] = 30
person.update({"gender": "female"})
print(person.get("city", "Not found"))
for key, value in person.items():
print(key, value)
| Type | Ordered | Mutable | Allows Duplicates | Syntax |
|---|---|---|---|---|
| List | Yes | Yes | Yes | [1, 2, 3] |
| Tuple | Yes | No | Yes | (1, 2, 3) |
| Set | No | Yes | No | {1, 2, 3} |
| Dictionary | Yes (from Python 3.7+) | Yes | Keys: No, Values: Yes | {"a": 1, "b": 2} |
students = [
{"name": "Alice", "age": 20, "grades": [85, 90]},
{"name": "Bob", "age": 22, "grades": [75, 80]}
]
text = "Python is powerful and easy to learn"
unique_words = set(text.lower().split())
keys = ["id", "name", "email"]
values = [101, "Alice", "alice@example.com"]
user = dict(zip(keys, values))
Mastering Lists, Tuples, Sets, and Dictionaries is fundamental for any Python developer. These data structures provide flexible, efficient, and powerful tools to manage and manipulate data in a variety of formats and scenarios. Understanding their unique features, strengths, and limitations helps in selecting the right structure for the task, leading to more readable and efficient code.
Whether youβre analyzing data, building web apps, or scripting automation, these core Python collections will be your best companions in writing elegant Python code.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is βΉ5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from βΉ3,000 - βΉ1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved