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 :
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).
Text Type: Dive into the world of characters and strings!
str
: Strings to represent textual data (e.g., "Hello, Python!").
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)).
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}).
Set Types: Embrace the uniqueness and unordered nature of sets!
set
: Unordered collections of unique elements (e.g., {1, 2, 3}).
Boolean Type ๐ฆ:
bool
: Represents eitherTrue
orFalse
.
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.
Lists ๐:
- Lists are ordered, mutable collections that can contain elements of different data types. Example:
[1, 'apple', True]
.
- Lists are ordered, mutable collections that can contain elements of different data types. Example:
Tuples ๐:
- Tuples are ordered, immutable collections similar to lists. Example:
(10, 20, 30)
.
- Tuples are ordered, immutable collections similar to lists. Example:
Dictionaries ๐๏ธ:
- Dictionaries are key-value pairs, where each key is unique. They are unordered and mutable. Example:
{'name': 'Bob', 'age': 25}
.
- Dictionaries are key-value pairs, where each key is unique. They are unordered and mutable. Example:
Sets ๐งฉ:
- Sets are unordered collections of unique elements. Example:
{2, 4, 6}
.
- Sets are unordered collections of unique elements. Example:
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.
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.
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.
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.
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.