Python 101 Series Part 3 - Guide to Python Programming

Python 101 Series Part 3 - Guide to Python Programming

Python 101 Series Part 3 — Guide to Python Programming

Welcome to Part 3 of the Python 101 Series!

If you’ve missed Part 1 or Part 2, make sure to check them out to stay on track.

Today, we’ll explore Data Types in Python.

Data Types in Python

Python has several built-in data types for storing and managing data. This blog will cover four key data types: List, Tuple, Dictionary, and Set.


1. Lists

A list can hold multiple values in a single variable, and each value can be accessed using its index number. The index represents the position of an element in the list, starting from 0 and going up to the last element.

Lists are:

  • Ordered: Elements have a defined order that will not change unless explicitly modified.
  • Changeable: You can add, remove, or update elements.
  • Allow Duplicates: Lists can store duplicate values.

Lists are created using square brackets []. They can hold elements of different data types, such as numbers, strings, or booleans.

fruit_basket = ["Apple", "Banana", "Mango", "Orange", "Pineapple"]

# Accessing elements using index numbers
print(fruit_basket[0]) # Output: Apple
print(fruit_basket[3]) # Output: Orange

# Checking the length of the list
print(len(fruit_basket)) # Output: 5

2. Tuples

A tuple is similar to a list, but unlike lists, tuples are immutable, meaning their elements cannot be changed, added, or removed after creation.

Tuples are:

  • Ordered: Like lists, tuples have a defined order.
  • Unchangeable: Elements cannot be modified.

Tuples are created using round brackets ().

Here’s an example of a tuple:

my_fruit_tuple = ("Apple", "Banana", "Mango", "Orange", "Pineapple")

# Accessing elements using index numbers
print(my_fruit_tuple[0]) # Output: Apple
print(my_fruit_tuple[3]) # Output: Orange

3. Dictionaries

A dictionary is a collection of key-value pairs, where each key is unique, and each key maps to a specific value.

Dictionaries are:

  • Ordered (Python 3.7+): Elements maintain insertion order.
  • Changeable: You can add, update, or delete key-value pairs.
  • Unique Keys: Duplicate keys are not allowed.

Dictionaries are created using curly brackets {} and have the following structure: {Key: Value}

student1 = {
"name": "Saadat",
"roll_number": 2211,
"section": "B",
}

# Accessing values using keys
print(student1["name"]) # Output: Saadat

# Another example
my_car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

print(my_car["brand"]) # Output: Ford

4. Sets

A set is an unordered collection of unique elements. Sets do not allow duplicate values and cannot be accessed using an index since they are unordered.

Sets are:

  • Unordered: The elements have no defined order.
  • Unchangeable: Although you cannot modify individual elements, you can add or remove elements.
  • Unique Elements Only: Duplicate values are automatically removed.

Sets are created using curly brackets {}.

fruits_set = {"Apple", "Banana", "Mango", "Orange", "Pineapple"}

# Displaying the set
print(fruits_set)

# Note: Sets are unordered, so the output order may vary.

Bonus Tip: If you’re unsure about the type of a variable, use the type() function to check.

print(type(fruit_basket))  # Output: <class 'list'>
print(type(my_fruit_tuple)) # Output: <class 'tuple'>
print(type(student1)) # Output: <class 'dict'>
print(type(fruits_set)) # Output: <class 'set'>


These four data types are foundational to Python programming. Understanding their properties will help you choose the right one based on the problem you are solving. Stay tuned for the next part of this series, where we’ll dive deeper into Python programming!

Getting Info...

About the Author

Hey! It's Saadat Khalid Awan, a Freelance Content Writer | Copywriter | Blogger, and Data Scientist. I'm very enthusiastic about writing. I enjoy the writing process because words have a magical power that can do wonders.

إرسال تعليق

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.