# Day 13 - Introduction to Python

# Let's Start with the Basics of Python: Essential for DevOps Engineers

In the ever-evolving field of DevOps, mastering programming languages is crucial. One of the most versatile and essential languages for DevOps engineers is Python. It helps in building automation scripts, developing tools, and managing infrastructure efficiently. Let's dive into the basics of Python and understand why it is important for DevOps engineers.

## What is Python?

Python is an open-source, general-purpose, high-level, and object-oriented programming language. It was created by Guido van Rossum and has gained immense popularity due to its simplicity and readability. Python's vast libraries and frameworks, such as Django, TensorFlow, Flask, Pandas, and Keras, make it a powerful tool for various applications, including web development, data analysis, machine learning, and more.

## Why Python is Essential for DevOps Engineers

1. **Simplicity and Readability**: Python's syntax is clean and easy to understand, making it accessible for beginners and efficient for experienced programmers.
    
2. **Extensive Libraries**: Python offers a wide range of libraries and frameworks that facilitate automation, web development, data processing, and more. This makes it easier to implement complex tasks with fewer lines of code.
    
3. **Automation and Scripting**: Python is widely used for writing automation scripts to manage infrastructure, deploy applications, and automate repetitive tasks.
    
4. **Integration**: Python integrates seamlessly with various tools and platforms used in DevOps, such as Docker, Kubernetes, Jenkins, and AWS.
    
5. **Community Support**: Python has a large and active community that contributes to its vast repository of libraries, frameworks, and resources. This ensures continuous improvement and support for Python developers.
    

## How to Install Python

You can install Python on various operating systems. Here are the instructions for some of them:

* [**Windows Installat**](https://www.python.org/downloads/)[**ion**](https://www.python.org/downloads/)
    
* [**Ubuntu**](https://www.python.org/downloads/): Use the command `apt-get install python3.6`
    

### Windows Installation

1. Go to the official Python website and download the latest version of Python for Windows.
    
2. Run the installer and follow the on-screen instructions.
    
3. Ensure that you check the box to add Python to your system PATH during installation.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720241720597/ab9c8c1e-24e3-44a6-8f85-ccf694a267a1.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720241807604/8dc38254-b149-4d62-a31f-283532b5eec7.png align="center")
    

### Ubuntu Installation

Open your terminal and run the following command to install Python:

```bash
 ubuntu@ip-172-31-10-117:~/test$ sudo apt-get install python3.6
```

### macOS Installation

1. Go to the [official Python website](https://www.python.org/downloads/) and download the latest version of Python for macOS.
    
2. Run the installer and follow the on-screen instructions.
    
3. Alternatively, you can use Homebrew to install Python by running:
    

```bash
 ubuntu@ip-172-31-10-117:~/test$ brew install python3
```

## Task 1: Getting Started with Python

1. **Install Python**: Follow the instructions above to install Python on your respective operating system.
    
2. **Check the Version**: After installation, open your terminal or command prompt and run the following command to check the installed Python version:
    

```bash
 ubuntu@ip-172-31-10-117:~/test$ python --version
```

or

```bash
 ubuntu@ip-172-31-10-117:~/test$ python3 --version
```

3. **Learn About Data Types**: Understanding different data types in Python is crucial. Start by reading about basic data types such as integers, floats, strings, lists, tuples, dictionaries, and sets.
    

## Modules in Python

Modules are code libraries you can use in your Python programs. There are two types of modules:

* **Built-in Modules**: Come preinstalled with Python.
    
* **User-defined Modules**: Created by you or installed via package managers like pip or conda.
    

**pip**: A package manager used to install Python modules.

## Data Types in Python

Data types specify the type of value a variable hold. You can get the data type of any object using the `type()` function.

![Python Data Types - javatpoint](https://static.javatpoint.com/python/images/python-data-types.png align="center")

### Numeric Data Types

* **Integer (int)**: Whole numbers without decimal points.
    
* **Floating-Point (float)**: Decimal values.
    
* **Complex (complex)**: Numbers with real and imaginary parts.
    

### String Data Type (str)

Represents a sequence of characters enclosed in single (' ') or double quotes (" ").

### Boolean Data Type (bool)

Represents either True or False.

### Sequence Data Types

* **list**: Ordered and mutable collection of items, e.g., `[8, 'mango', [-4, 5], ['Rose', 'Lily']]`
    
* **tuple**: Ordered and immutable collection of items, e.g., `(('parrot', 'sparrow'), ('lion', 'tiger'))`
    

### Mapped Data Type

* **dict**: Ordered collection of key-value pairs, e.g., `{"name": "Smriti", "id": "123"}`
    

### Set Data Type

* **set**: Unordered and mutable collection of unique elements, e.g., `{2, 4, 6, 3}`
    

## Print Statement in Python

The `print()` function prints the specified message to the screen.

```python
print("Hey", 6, 7, sep="~", end="003")
# Output: Hey~6~7003
```

## Comments in Python

Comments are used to explain code. They are not executed by the interpreter.

### Single-line Comment

```python
# This is a single line comment.
```

### Multi-line Comment

```python
"""
This is a 
multi-line
comment
"""
```

## Escape Sequence in Python

Escape sequences are used to insert characters that cannot be directly used in a string.

```python
print("This is an\nescape sequence")
# Output: This is an
#         escape sequence
```

## Typecasting in Python

Typecasting is converting one data type to another. It can be **implicit** (automatically by Python) or **explicit** (manually by the programmer).

### Implicit Typecasting

```python
a = 7
b = 3.0
c = a + b
print(c)  # Output: 10.0
```

### Explicit Typecasting

```python
string = "15"
number = 7
string_number = int(string)
sum = number + string_number
print("The sum is:", sum)  # Output: The sum is: 22
```

## Conclusion

Python is a versatile and powerful programming language widely used for various applications, from web development to data analysis and machine learning. Its simplicity, readability, and extensive library support contribute to its popularity and widespread adoption in the software development community.

Happy coding!

~ Tushar Ranjan🙂
