Day 14 : Python Data Types & Data Structures
Python Data Types & Data Structures Hands-on Practice
Python, a popular programming language, equips developers with an array of data types and data structures to manage and organize information effectively. In this blog, we'll delve into the essentials of Python data types and structures, breaking them down in a way that's easy for everyone to understand.
Python Data Typesπ
Integers (int) π’
Integers are like the building blocks of counting. They handle whole numbers, such as the quantity of items in a shopping cart, the number of pages in a book, or anything you can count.
Floating-Point Numbers (float) π
Floating-point numbers are more versatile; they accommodate numbers with decimal points. Think of these as your currency with cents, or measurements like your height or weight.
Strings (str) π
Strings are the text enthusiasts. They handle words, sentences, and any text-based data. Imagine your favorite book, which is essentially a collection of strings.
Booleans (bool) βοΈβ
Booleans are akin to light switches. They have two states: "On" (True) or "Off" (False). You'll find them particularly handy for making decisions in your code.
NoneType (None) π¦
NoneType represents the absence of data. It's like having an empty container, ready to be filled with something meaningful.
Python Data Structuresππ²
Lists π
Lists are like versatile containers. They allow you to store multiple items, much like your shopping list or a collection of friends' names.
Dictionaries π
Dictionaries function as your personalized phone book. They associate names (keys) with corresponding information (values). Whenever you need quick access to specific data, dictionaries are your go-to.
Tuples π¦π
Tuples are similar to lists, but with a twist: once you create them, you can't change their content. Think of them as read-only lists.
Sets ποΈ
Sets are perfect for holding unique items. They're like a bag filled with distinct objects, ensuring you don't have duplicates.
Understanding these Python data types and structures is essential for effective data management and processing. Python's versatility, as seen through these tools, makes it a go-to language for a wide range of tasks, from simple number crunching to handling complex text and data structures. π§°π’π
β³Tasks:
Give the Difference between List, Tuple and set.
In the world of Python, there are three ways to organize your stuff: lists, tuples, and sets. Each works a bit differently. Here's how:
Lists: Your Everyday Containers π
Think of lists as your everyday containers. They can hold all sorts of things, like a bag for groceries.
You can add, remove, and change items in a list. It's like rearranging your groceries in the bag.
For example, you can start with a list of fruits and later add more or swap one for another.
fruits = ["apple", "banana", "cherry"] #Created List fruits.append("date") #Added to List fruits[1] = "kiwi" #Swapped with List
Tuples: The "Don't Touch" Collections π¦π
Tuples are like collections that you don't want to change. Once you make one, it stays as it is, like a locked box.
You can use tuples when you want to make sure no one accidentally messes with your collection.
For instance, you can put your coordinates in a tuple, and they won't change.
coordinates = (3, 5) x, y = coordinates
In this example, we create a tuple with coordinates, and we can conveniently unpack the values into
x
andy
.
Sets: The No-Duplicate Bags ποΈ
Sets are like bags where you put unique things. You don't want to carry the same item twice.
They don't care about the order; they just care about not having duplicates.
For instance, you can put fruits in a set, and if you add the same one twice, it won't show up twice.
fruits = {"apple", "banana", "cherry", "banana"} unique_fruits = set(fruits)
In this example, we create a set of fruits, including a duplicate "banana," and then we convert it to a set, resulting in unique elements.
So, which one to use? It depends on what you need:
Lists are your everyday choice when you need a container that can change.
Tuples are for things that should stay the same.
Sets are for making sure you don't have duplicates.
Create the below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary.
fav_tools = { 1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef" }
To print a tool using the key of the dictionary:
favorite_tool = fav_tools.get(4) print(favorite_tool) #Kubernetes
Write a program to add
Digital Ocean
to the list of cloud_providers and sort the list in alphabetical order.
cloud_providers = ["AWS","GCP","Azure"]
Add Digital Ocean
to this list.
cloud_providers.append("Digital Ocean")
cloud_providers.sort()
print(cloud_providers)
βEndcard:
π Thank you for joining me on this insightful journey into the world of DevOps!
β€ If you found this blog helpful and informative, don't forget to give it a like!
π Share this valuable knowledge with your friends and colleagues, so they can also benefit from understanding the power of DevOps!
π Stay updated with my latest posts and never miss out on exciting content! Click that Follow button to join and stay in the loop!
Follow me on Linkedin --> abdallah-qamar π
Stay tuned for Day 15...π