Skip to main content

Command Palette

Search for a command to run...

Day 13 - Introduction to Python

Published
5 min readView as Markdown
Day 13 - Introduction to Python
T

Hey there! 👋

I'm Tushar Ranjan, a DevOps Engineer passionate. Currently, on a learning adventure, I'm here to share my journey and Blogs in the world of cloud and DevOps.

🛠️ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space.

🌐 Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!

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 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.

Ubuntu Installation

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

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

macOS Installation

  1. Go to the official Python website 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:

 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:

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

or

 ubuntu@ip-172-31-10-117:~/test$ python3 --version
  1. 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

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.

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

# This is a single line comment.

Multi-line Comment

"""
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.

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

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

Explicit Typecasting

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🙂

More from this blog

Tushar Ranjan's blog

72 posts