Day 13-14 Python and its Data types , Data structures :

Day 13-14 Python and its Data types , Data structures :

ยท

4 min read

What is Python?๐Ÿ

  • Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general-purpose language, meaning it can be used to create a variety of different programs and isn't specialized for any specific problems.

  • Python is an Open source, general-purpose, high-level, and object-oriented programming language.

  • It was created by Guido van Rossum

  • Python consists of vast libraries and various frameworks like Django, Tensorflow, Flask, Pandas, Keras etc.

How to install it?

  • For Windows - To download Python, you need to visit www.python.org, which is the official Python website.

  • For Ubuntu: apt-get install Data Types in Python :

๐Ÿ’ป Data Types in Python :

Python's beauty lies in its dynamic typing, allowing variables to change types based on their assigned values. Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.

Let's see some of them :

  1. Numeric Types: Embrace the magic of numbers!

    • int: Integers that represent whole numbers (e.g., 42).

    • float: Floating-point numbers with decimal precision (e.g., 3.14).

    • complex: Complex numbers with real and imaginary parts (e.g., 2 + 3j).

  2. Text Type: Dive into the world of characters and strings!

    • str: Strings to represent textual data (e.g., "Hello, Python!").
  3. Sequence Types: Unravel the power of ordered collections!

    • list: Mutable lists to store items of various types (e.g., [1, "apple", True]).

    • tuple: Immutable sequences, perfect for data integrity (e.g., (1, 2, 3)).

  4. Mapping Type: Unlock the magic of key-value pairs!

    • dict: Dictionaries with unique keys mapped to their corresponding values (e.g., {"name": "John", "age": 30}).
  5. Set Types: Embrace the uniqueness and unordered nature of sets!

    • set: Unordered collections of unique elements (e.g., {1, 2, 3}).
  6. Boolean Type ๐Ÿšฆ:

    • bool: Represents either True or False.
  7. None Type :

    • None: Represents the absence of a value or a null value.

Data Structures in Python:๐Ÿ—๏ธ๐Ÿงฑ

Data structures in Python allow us to organize and store data effectively. Python provides several built-in data structures that simplify programming and boost efficiency.

  1. Lists ๐Ÿ“œ:

    • Lists are ordered, mutable collections that can contain elements of different data types. Example: [1, 'apple', True].
  2. Tuples ๐Ÿ“Š:

    • Tuples are ordered, immutable collections similar to lists. Example: (10, 20, 30).
  3. Dictionaries ๐Ÿ—‚๏ธ:

    • Dictionaries are key-value pairs, where each key is unique. They are unordered and mutable. Example: {'name': 'Bob', 'age': 25}.
  4. Sets ๐Ÿงฉ:

    • Sets are unordered collections of unique elements. Example: {2, 4, 6}.

Strings ๐Ÿ”ค:

  • Strings are sequences of characters and are immutable. Example: 'Python'.

Tasks :

Task 1: Give the Difference between List, Tuple and Set. Do Handson and put screenshots as per your understanding.

  1. Mutability:

    • List: Lists are mutable, meaning their elements can be changed after creation.

    • Tuple: Tuples are immutable, meaning their elements cannot be changed after creation.

    • Set: Sets are mutable, but individual elements cannot be changed. However, you can add or remove elements from a set.

  2. Order:

    • List: Lists are ordered, maintaining the order in which elements are added.

    • Tuple: Tuples are ordered, just like lists.

    • Set: Sets are unordered, meaning they do not maintain the order of elements.

  3. Duplicates:

    • List: Lists can contain duplicate elements.

    • Tuple: Tuples can contain duplicate elements, like lists.

    • Set: Sets do not allow duplicate elements; they automatically remove duplicates.

Syntax:

  • List: Elements in a list are enclosed in square brackets [].

  • Tuple: Elements in a tuple are enclosed in parentheses ().

  • Set: Elements in a set are enclosed in curly braces {}.

1. List :

Let's create a list of colors and modify it by adding and changing elements.

2. Tuple:

Let's create a tuple of fruits and try to modify it (which will result in an error).

3. Set:

Let's create a set of numbers and demonstrate how duplicates are automatically removed.

Task 2: Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.

  1. Create a List of cloud service providers eg.

    cloud_providers = ["AWS", "GCP", "AZURE"]

    Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.

Conclusion:๐ŸŽฏ

Lists, tuples, and sets are essential data structures in Python, each serving unique purposes. Lists are suitable for ordered and mutable collections, tuples for ordered and immutable collections, and sets for unordered and unique collections. Understanding when to use each of these data structures can significantly improve the efficiency and readability of your Python code.

ย