Day 15 Task: Basics of Python for DevOps Engineers
Python Programming Essentials
Table of contents
What is Python?
Python is a general-purpose, dynamically typed, high-level, compiled and interpreted, garbage-collected, and purely object-oriented programming language that supports procedural, object-oriented, and functional programming.
Features of Python:
Easy to use and Read - Python's syntax is clear and easy to read, making it an ideal language for both beginners and experienced programmers. This simplicity can lead to faster development and reduce the chances of errors.
Dynamically Typed - The data types of variables are determined during run-time. We do not need to specify the data type of a variable during writing codes.
High-level - High-level language means human readable code.
Purely Object-Oriented - It refers to everything as an object, including numbers and strings.
Rich Standard Library - Python comes with several standard libraries that provide ready-to-use modules and functions for various tasks, ranging from web development and data manipulation to machine learning and networking.
Open Source - Python is an open-source, cost-free programming language. It is utilized in several sectors and disciplines as a result.
Variable in Python -
A variable is the name given to a memory location. A value-holding Python variable is also known as an identifier.
#!/usr/bin/python3.12
#Variable in Python
name = "Vibhuti Jain"
age = 24
print("Your name is: ", name)
print("Your age is: ", age)
#Type Conversion In Python - Give you the type of data types
print(type(name))
print(type(age))
As we can see in below example, it is printing my name and age along with the type of the variable because in my python script i used python type conversion concepts. Its a best way to find out the type of the data type of variable.
Tasks
How to Install Python?
- Install Python on your respective OS, and check the version.
$sudo apt-get update
$sudo apt-get install python-is-python3
$python --version
- Read about different data types in Python.
Python Data Types
In computer programming, data types specify the type of data that can be stored inside a variable. For example,
a=25
Here, 25 (an integer) is assigned to the num variable. So the data type of num is of the <int> class.
Data Types | Classes | Description |
Numeric | int, float, complex | holds numeric values |
String | str | holds sequence of characters |
Sequence | list, tuple, range | holds collection of items |
Mapping | dict | holds data in key-value pair form |
Boolean | bool | holds either True or False |
Set | set, frozenset | hold collection of unique items |
Python Numeric Data type
In Python, numeric data type is used to hold numeric values.
int
- holds signed integers of non-limited length.float
- holds floating decimal points and it's accurate up to 15 decimal places.
num1 = 5
print(num1, 'is of type', type(num1))
num2 = 2.0
print(num2, 'is of type', type(num2))
Python List Data Type
List is an ordered collection of similar or different types of items separated by commas and enclosed within brackets []. List can be modified we can chang the list. For example,
list = [10, 20.34, "Vibhuti", True]
print(list)
# modified list
list.append("Andrew")
print(list)
#Access the values of the list
print(list[0])
print(list[1])
print(list[2])
print(list[3])
print(list[4])
10
20.34
Vibhuti
True
Andrew
In above output file, we can see the changes that we can modified the existing list values.
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 = ('Vibhuti', 50)
# create a tuple
tuple = ('Vibhuti', 'Python', "DevOps")
# access element at index 0
print(tuple[0]) #Vibhuti
# access element at index 1
print(tuple[1]) #Python
If i tried to append the values in the tuple it will throw the errors, because tuples are immutable.
tuple1=()
tuple1.append("Vibhuti")
tuple1.append("AWS")
tuple1.append("Devops")
print(tuple1)
Python String Data Type
String is a sequence of characters represented by either single or double quotes. For example,
message= 'Python Is easy peasy'
print(message)
Output
Python Is easy peasy
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))
Python Dictionary Data Type
Python dictionary is an ordered collection of items. It stores elements in key/value pairs.
# create a dictionary named capital_city
capital_city = {'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}
print(capital_city)
Output -
{'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}
Follow for more updates:)