#Day13:- 90 Days DevOps Challenge@Basics of Python For DevOps Engineer.

#Day13:- 90 Days DevOps Challenge@Basics of Python For DevOps Engineer.

Mastering Python for DevOps: A Beginner-Friendly Guide

🐍Why Python?😕

🕸️In the field of DevOps, efficiency and collaboration are paramount. Enter Python, a game-changing programming language. Whether you're new to DevOps or a pro, this blog introduces you to Python's basics and its role in automating tasks for smoother software development. Let's learn and discover how Python simplifies DevOps for better workflows, faster deployment, and effective teamwork.

🐍What is Python?😕

📝🐍Python is a high-level, general-purpose programming language with an elegant syntax that allows programmers to focus more on problem-solving than on syntax errors. One of the primary goals of Python Developers is to keep it fun to use. Python has become a big buzz in the field of modern software development, infrastructure management, and especially in Data Science🔭 and Artificial Intelligence🔬. Most recently, Python has risen to the top 3 list of TIOBE index of language popularity.

Python is becoming increasingly everywhere, but you must be wondering why Python has become such a hot topic in the developers’ world. In this tutorial, you will understand all the reasons behind Python’s popularity.

🏃‍♂️Let's Understand In Short Way:-

  • Python is a 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 Python?

📝You can install Python in your System whether it is windows, MacOS, ubuntu, centos etc. Below are the links for the installation:

Task1: How To Install Python

Install Python in your respective OS, and check the version.

👨‍💻SOLUTION:-

📝Step1:- I am going to install python in ubuntu server and type sudo apt install python3 in the terminal.

🐧Step2:- Check if python installed or not by python3 --version.

Task2: Data Types In Python

  1. Read about different Data Types in Python.

👨‍💻SOLUTION:-

🥏Python Data Types

In computer programming, data types specify the type of data that can be stored inside a variable. For example,

num = 24

Here, 24 (an integer) is assigned to the num variable. So the data type of num is of the int class.

👨‍💻📜Data Types Table:

Data TypesClassesDescription
Numericint, float, complexholds numeric values
Stringstrholds sequence of characters
Sequencelist, tuple, rangeholds collection of items
Mappingdictholds data in key-value pair form
Booleanboolholds either True or False
Setset, frozeensethold collection of unique items

Since everything is an object in Python programming, data types are actually classes and variables are instances(object) of these classes.

🔢Python Numeric Data type:

In Python, numeric data type is used to hold numeric values.

Integers, floating-point numbers and complex numbers fall under python numbers category. They are defined as int, float and complex classes in Python.

  • int - holds signed integers of non-limited length.

  • float - holds floating decimal points and it's accurate up to 15 decimal places.

  • complex - holds complex numbers.

We can use the type() function to know which class a variable or a value belongs to.

Let's see an example,

num1 = 5
print(num1, 'is of type', type(num1))

num2 = 2.0
print(num2, 'is of type', type(num2))

num3 = 1+2j
print(num3, 'is of type', type(num3))

👨‍💻Output:

5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is of type <class 'complex'>

In the above example, we have created three variables named num1, num2 and num3 with values 5, 5.0, and 1+2j respectively.

We have also used the type() function to know which class a certain variable belongs to.

Since,

  • 5 is an integer value, type() returns int as the class of num1 i.e <class 'int'>

  • 2.0 is a floating value, type() returns float as the class of num2 i.e <class 'float'>

  • 1 + 2j is a complex number, type() returns complex as the class of num3 i.e <class 'complex'>

📃Python List Data Type:

List📝 is an ordered collection of similar or different types of items separated by commas and enclosed within brackets [ ]. For example,

languages = ["Swift", "Java", "Python"]

Here, we have created a list named languages with 3 string values inside it.

Access List Items🧏

To access items from a list, we use the index number (0, 1, 2 ...). For example,

languages = ["Swift", "Java", "Python"]

# access element at index 0
print(languages[0])   # Swift

# access element at index 2
print(languages[2])   # Python

In the above example, we have used the index values to access items from the languages list.

  • languages[0] - access first item from languages i.e. Swift

  • languages[2] - access third item from languages i.e. Python

🐢👨‍💻Python Tuple Data Type:

Tuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified.

In Python, we use the parentheses () to store items of a tuple. For example,

product = ('Xbox', 499.99)

Here, product is a tuple with a string value Xbox and integer value 499.99.

🐢Access Tuple Items

Similar to lists, we use the index number to access tuple items in Python . For example,

# create a tuple 
product = ('Microsoft', 'Xbox', 499.99)

# access element at index 0
print(product[0])   # Microsoft

# access element at index 1
print(product[1])   # Xbox

🧵Python String Data Type:

String is a sequence of characters represented by either single or double quotes. For example,

name = 'Python'
print(name)  

message = 'Python for beginners'
print(message)

Output

Python
Python for beginners

In the above example, we have created string-type variables: name and message with values 'Python' and 'Python for beginners' respectively.

📐Python Set Data Type:

Set is an unordered collection of unique items. Set is defined by values separated by commas inside braces { }. For example,

# create a set named student_id
student_id = {112, 114, 116, 118, 115}

# display student_id elements
print(student_id)

# display type of student_id
print(type(student_id))

Output

{112, 114, 115, 116, 118}
<class 'set'>

Here, we have created a set named student_info with 5 integer values.

Since sets are unordered collections, indexing has no meaning. Hence, the slicing operator [] does not work.

📔Python Dictionary Data Type:

Python dictionary is an ordered collection of items. It stores elements in key/value pairs. Here, keys are unique identifiers that are associated with each value.

Let's see an example,

# create a dictionary named capital_city
capital_city = {'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}

print(capital_city)

Output

{'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}

In the above example, we have created a dictionary named capital_city. Here,

  1. Keys are 'Nepal', 'Italy', 'England'

  2. Values are 'Kathmandu', 'Rome', 'London'

👨‍💻Access Dictionary Values Using Keys🔐

We use keys to retrieve the respective value. But not the other way around. For example,

# create a dictionary named capital_city
capital_city = {'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}

print(capital_city['Nepal'])  # prints Kathmandu

print(capital_city['Kathmandu'])  # throws error message

Here, we have accessed values using keys from the capital_city dictionary.Since 'Nepal' is key, capital_city['Nepal'] accesses its respective value i.e. Kathmandu However, 'Kathmandu' is the value for the 'Nepal' key, so capital_city['Kathmandu'] throws an error message.

🥏Conclusion

👨‍💻👨‍💻In our blog today, we've discovered why Python is important for organizations. We've also taken a look at different types of data in Python, which might sound complex but is actually quite simple. Python helps organizations work better, and learning about its different data types can be fun and useful. Stay with us as we keep exploring the amazing world of Python programming together in my next upcoming blog!!.