What is Python?
Python is a high-level, interpreted, and general-purpose programming language. It was created by Guido van Rossum and first released in 1991. Python emphasizes readability, simplicity, and ease of use, making it a popular choice among beginners and experienced programmers alike.
Key features of Python include:
Readability: Python's syntax is designed to be easily readable, which makes it more intuitive and reduces the cost of program maintenance.
Simplicity: Python emphasizes simplicity and provides a clean and concise syntax that allows programmers to express concepts in fewer lines of code compared to other programming languages.
Versatility: Python is a versatile language and can be used for a wide range of applications, such as web development, data analysis, scientific computing, artificial intelligence, machine learning, and more.
Large Standard Library: Python comes with a comprehensive standard library that provides a wide range of modules and functions to facilitate common programming tasks, such as file handling, networking, and data manipulation.
Interpreted: Python is an interpreted language, which means that it does not need to be compiled before execution. This allows for rapid development and testing, making it ideal for prototyping and scripting.
Python for DevOps
Python as a Scripting Language: DevOps engineers leverage Python's scripting capabilities to automate repetitive tasks, streamline workflows, and orchestrate complex systems.
Infrastructure Provisioning: Python integrates seamlessly with popular DevOps tools like Ansible and Terraform, enabling infrastructure provisioning and management.
Continuous Integration and Deployment: Python's extensive library support and frameworks like Jenkins and GitLab facilitate seamless CI/CD pipelines, ensuring efficient delivery of software applications.
Monitoring and Logging: Python, along with libraries such as Prometheus and ELK stack, empowers DevOps engineers to monitor and analyze system metrics, logs, and alerts.
Containerization and Orchestration: With tools like Docker and Kubernetes, Python enables DevOps engineers to build, deploy, and manage containerized applications at scale.
Python Installation on Linux OS
To install Python on Ubuntu, use the below commands
Update Package Lists:
sudo apt update
Install Python: For Python 3:
sudo apt-get install python3.6
Data Types
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'>
Sequence Data type
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
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
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.
Boolean Data Type
Data type with one of the two built-in values, True or False. Boolean objects that are equal to True are truthy (true), and those equal to False are falsy (false). But non-Boolean objects can be evaluated in a Boolean context as well and determined to be true or false. It is denoted by the class bool.
# Boolean type
is_true = True
Set Data Type
In Python, a Set is an unordered collection of data types that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.
set: Represents an unordered collection of unique elements. For example:
unique_numbers = {1, 2, 3}
.frozenset: Represents an immutable set. For example:
frozen_numbers = frozenset({1, 2, 3})
.
Dictionary Data Type
A dictionary in Python is an unordered collection of data values, used to store data values like a map, unlike other Data Types that hold only a single value as an element, a Dictionary holds a key: value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon : , whereas each key is separated by a ‘comma’.
Accessing Key-value in Dictionary
In order to access the items of a dictionary refer to its key name. Key can be used inside square brackets. There is also a method called get() that will also help in accessing the element from a dictionary.
# Python program to demonstrate
# accessing a element from a Dictionary
# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# accessing a element using key
print("Accessing a element using key:")
print(Dict['name'])
# accessing a element using get()
# method
print("Accessing a element using get:")
print(Dict.get(3))
Output:
Accessing a element using key:
For
Accessing a element using get:
Geeks
Thanks for Reading