Getting Started with Python: A Beginner's Guide
In our journey through the basics of Python, we'll start with the essentials, including what Python is and how to install it on your system. From there, we'll dive into the fundamental concepts of data types and data structures, building a strong foundation for your Python adventures.
Python is renowned for its simplicity and ease of use, making it a fantastic language to kickstart your programming journey. So, let's embark on this exciting voyage into Python and discover the wonders it holds.
What is Python?
Python is a high-level, versatile, general purpose and interpreted object-oriented programming language known for its readability and ease of use. Created by Guido van Rossum and first released in 1991, Python has become one of the most popular programming languages worldwide.How to Install Python?
For Windows:
Follow the link for installing Python on Windows.For macOS:
Open your Terminal.
You can check if Python is already installed by running
python3 --version
. If it's installed, you'll see the version number.If Python is not installed, you can install it using Homebrew, a package manager for macOS. First, install Homebrew if you haven't already, and then run the following command to install Python 3:
brew install python
For Linux (Ubuntu/Debian):
Python is typically pre-installed on many Linux distributions. You can check if it's installed by running
python3 --version
. If not, you can install it using the package manager:Update your package list to get the latest package information:
sudo apt update
Install Python 3:
sudo apt install python3
You may also choose to install Python through distribution-specific package managers, like
yum
for CentOS.
Data Types in Python
In Python, data types are used to categorize and manage different types of data. Python supports several built-in data types, which can be categorized into the following main groups:Numeric Types:
int (integer): Used for whole numbers, such as 0, 1, 100, or -10.
float (floating-point): Used for real numbers with a decimal point, such as 3.14 or -0.5.
Sequence Types:
str (string): Used for text and character data, such as "Hello, Python."
list: An ordered collection of items that can be of any data type, enclosed in square brackets, e.g.,
[1, 2, 3]
.tuple: An ordered collection of items, similar to lists, but enclosed in parentheses and immutable, e.g.,
(1, 2, 3)
.
Mapping Type:
- dict (dictionary): Used to store key-value pairs, enclosed in curly braces, e.g.,
{'name': 'Brett', 'surname': 'Lee'}
.
- dict (dictionary): Used to store key-value pairs, enclosed in curly braces, e.g.,
Set Types:
- set: An unordered collection of unique items, enclosed in curly braces, e.g.,
{1, 2, 3}
.
- set: An unordered collection of unique items, enclosed in curly braces, e.g.,
Boolean Type:
- bool (boolean): Represents the truth values
True
orFalse
. Typically used in conditional expressions and control structures.
- bool (boolean): Represents the truth values
Data Structures in Python
Data Structures allow you to organize your data in such a way that enables you to store collections of data, relate them and perform operations on them accordingly. Python provides a variety of built-in data structures, each with its own characteristics and use cases. Here are some of the most commonly used data structures in Python:Lists: Lists are ordered collections of items that can be of any data type. They are mutable, meaning you can change their contents. Lists are created using square brackets, e.g.,
[1, 2, 'xyz']
.Tuples: Tuples are similar to lists but immutable, which means their contents cannot be changed after creation. They are created using parentheses, e.g.,
(1, 2, 'music')
.Dictionaries: Dictionaries are used to store key-value pairs, and they are unordered and mutable. You can access values using their keys. Dictionaries are created using curly braces, e.g.,
{'name': 'Bosh', 'age': 32}
.Sets: Sets are unordered collections of unique elements. They are useful for tasks like removing duplicates from a list. Sets are created using curly braces or the
set()
constructor, e.g.,{1, 2, 3}
.
Difference between List, Tuple and Set
At first glance list, tuple and set might seem similar but they are slightly different from each other and used accordingly for different purposes by the programmers.
List | Tuple | Set |
Ordered collection. | Ordered collection. | Unordered collection. |
Mutable (you can change their elements after creation). | Immutable (you can't change their elements after creation). | Mutable (you can add or remove elements). |
Defined using square brackets: [ ] . | Defined using parentheses: ( ) . | Defined using curly braces: { } . |
Can contain duplicate elements. | Can contain duplicate elements. | Do not contain duplicate elements. |
Lists are often used when you need a collection of items that might change during the program. | Tuples are used when you want to store a collection of items that should not change during the program. | Sets are useful when you need to ensure uniqueness or perform set operations like union and intersection. |
List:
Tuple:
Set:
Tasks:
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" }
Create a List of cloud service providers eg.
cloud_providers = ["AWS","GCP","Azure"]
Conclusion
We explored the Python installation process, ensuring you have everything you need to start your Python programming journey. We delved into Python's data types, including integers, floats, strings, and booleans, understanding how each plays a crucial role in Python's dynamic nature. We also ventured into data structures, learning about lists, tuples, sets, and dictionaries, and their unique characteristics.