0% found this document useful (0 votes)
5 views

Python_Advanced

The document provides a comprehensive overview of Python, covering its applications in automation, data visualization, data analytics, AI, web development, and database management. It includes installation steps, variable types, data structures, operators, control flow, functions, recursion, modules, OOP concepts, and file handling. Additionally, it explains various data types like lists, tuples, dictionaries, and sets, along with their methods and operations.

Uploaded by

Krish Varma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python_Advanced

The document provides a comprehensive overview of Python, covering its applications in automation, data visualization, data analytics, AI, web development, and database management. It includes installation steps, variable types, data structures, operators, control flow, functions, recursion, modules, OOP concepts, and file handling. Additionally, it explains various data types like lists, tuples, dictionaries, and sets, along with their methods and operations.

Uploaded by

Krish Varma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Python

• Automation -> Business needs and day to day activities


• Data Visualization -> Plots and graphical
representations.
• Data Analytics -> Analyse and understand raw data for
insights and trends.
• AI and Machine Learning -> simulate human behaviour
and to learn from past data without hard coding.
• Create web applications.
• Handle databases.
• Business and accounting to perform complex
mathematical operations along with quantitative and
qualitative analysis.
Steps to Install Python:

1. Visit the official python website: https://www.python.org/


2.Download the executable file based on your Operating System
and version specifications.
3.Run the executable file and complete the installation process.
Version:
After installation check the version of python by typing
following command: ‘python --version’.
Python Variables Scope of variable:
• Variables are containers Local/Global Variable:
• Variable type does not need to be def f():
declared explicitly. #local variable
s = "DevOps course."
print(s)
# Global variable
Example: s = "Python Data"
name = "Singam" #type str f()
print(s)
batch = 1.0 #type int
Job = True #type bool

Unordered list — Used to create a list of related items,


in no particular order.
Ordered list — Used to create a list of related items, in a specific order
Data Types 1) list_data = [9, 2.9, [-3, 5], [”jenkins", ”Jira"]]
Sequenced data:
print(list_data)
1)list
A list is an ordered collection of data with elements separated by a comma Output: [9, 2.9, [-3, 5], ['jenkins', 'Jira']]
and enclosed within square brackets.
2) tuple1 = ((”cicd", ”Jenkins"), (” security", ”fortify"))
Lists are mutable and can be modified after creation. print(tuple1)
2) Tuple
A tuple is an ordered collection of data with elements separated by a Output: (('cicd', 'Jenkins'), ('security', 'fortify'))
comma and enclosed within parentheses.
3) sequence1 = range(4,14,2)
Tuples are immutable and can not be modified after creation for i in sequence1:
3) Range print(i)
Returns a sequence of numbers as specified by the user. If not specified by
the user, then it starts from 0 by default and increments by 1. Output:4 6 8 10 12

4) data: dictionary 4) dict1 = {"name":"Devops", "batch":1.0,


"canVote":True,"name":"test"}
A dictionary is an ordered collection of data containing a key:value pair.
print(dict1)
The key:value pairs are enclosed within curly brackets
No duplicated allowed print(dict1["name"])
5) Set info = {"test", 19, False, 5.9, 19}
Set is an unordered collection of elements in which no element is repeated print(info)
and they store multiple values in a single variable.
Python
Numbers/Operators Comparison operators:

1) In python numbers are of the following data types:


• Int -> int1 2345698
• Float -> flt1 = -8.35245
• Complex -> cmplx1 = 2 + 4j
2) Data Conversion -> Python allows data conversion
num1 = 25
num2 = float(num1) Identity operators:
Output: 25.0

3) Python Booleans
print("Integer:",bool(23))
Bitwise operators:

4) Python Strings
A string is essentially a sequence or array of textual
data.
Operations with Strings
• Length of a String: Length of a string using len() function.
• String as an Array: String is essentially a sequence of characters also
called an array.
• Loop through a String: Strings are arrays and arrays are iterable.
• String Methods: For Example str = ”jenkins”
• str.upper()
• str.lower()
• str.strip()
• str.replace(“Jen”,”Doc”)
• str.split(“k”)
• str.capitalize()
• str.find(”ins"))
List Indexes
Item/element in a list has its own unique index. List Comprehension

ToolsData = ["Maven", "Ansible", "Jenkins", "Sonar"] List comprehensions are used for
CloudData = [”AWS", ”AZURE", ”GCP"] creating new lists from other iterable’s like
Indexing : [0] [1] [2] [3]
lists, tuples, dictionaries, sets, and
even in arrays and strings.

Add list Items : ToolsData.append()


Syntax:
Insert list Items: ToolsData.insert(1,”Docker”) List = [expression(item) for item in iterable if condition]
Extend list Items: ToolsData.extend(CloudData) [ Add two lists, set, dict ]
POP list items: ToolsData. pop() [#removes the last item of the list]
REMOVE list items: ToolsData.remove(“Maven”) EXAMPLE
Delete list items: del ToolsData[3]
ToolsData = ["Maven", "Ansible", "Jenkins", "Sonar"]
Clear list items: clear():

ToolsData = ["Maven", "Ansible", "Jenkins", "Sonar"] names = [item for item in ToolsData if "o" in item]
EXAMPLE if "Maven" in ToolsData: print(names)
print("Maven is present.")
else:
print("Maven is absent.")

ToolsData.append("Fortify")
List Methods
• sort(): This method sorts the list in ascending order.
• reverse(): This method reverses the order of the list.
• index(): This method returns the index of the first occurrence of the list item.
• count(): Returns the count of the number of items with the given value.
• copy(): Returns copy of the list.

Conditional Statements

if Statement
if-else Statement
elif Statement
Python for Loop Nested Loops

Iterating over string #NESTED LOOPS


name = 'SINGAM' i=1
for Loop for i in name: while (i<=3):
print(i) #for loop will run till end
for k in range(1, 4):
#iterating over a tuple print(i, "*", k, "=", (i*k))
i=i+1
tools = ("Maven", "Jenkins", "sonar", "jira") print()
for x in tools:
print(x) for i in range(1, 4):
While Loop k=1
#PYTHON WHILE LOOP while (k<=3):
count = 5
print(i, "*", k, "=", (i*k))
while (count > 0): k=k+1
print(count) print()
count = count - 1
Python Functions
Block of code that performs a specific task.
There are two types of functions:
built-in functions -> min(), max(), len(), sum(), type(), range(), dict(),
list(), tuple(), set(), print(), etc.
user-defined functions -> Functions to perform specific tasks as
per our needs.

EXAMPLE:

def toolname(security, analysis)


print("Tools,", security, analysis)
toolname("Fortify", "Sonar")
Function Arguments CASE 1
def name(fname, mtame = "Jenkins", boardname = "Jira"):
print("Hello,", fname, mtame, boardname)

name("Sonar")
• Default Arguments CASE 2
def name(firsttool, secondtool, thirdtool):
print("Hello,", firsttool, secondtool, thirdtool)
• Keyword Arguments
name(thirdtool = "Ansible", firsttool = "Terraform",
secondtool = "Jmeter")

• Required Arguments CASE 3


def name(firsttool, secondtool, thirdtool):
print("Hello,", firsttool, secondtool, thirdtool)

name("Ansible", "Terraform", "Jmeter")


• Variable-length CASE 4
Arguments def name(*name):
print("Hello,", name[0], name[1], name[2])

name("Ansible", "Terraform", "Jmeter")


Python Recursion
Function calling a function

Python Modules

• These are the python files


which helps us to write
the python code in easy
way.
Python Packages

• Python packages are essentially


folders that contain many python
modules. As such, packages help us NumPy, SciPy, Pandas, Seaborn, sklearn, Matplotlib, etc.
to import modules from different
folders.
Python OOPS
Class is a blueprint or a template for creating
objects.

self method:

• The self parameter is a reference to the


current instance of the class and is used to
access variables that belongs to the class.
• It must be provided as the extra parameter
inside the method definition.

__Init__ method:

Init-> Self->
Class Object Binds the
Initialize attributes
Python Inheritance
ANY DIFFERENCE DID YOU FOUND?????
Converting JSON string to
python/Python to Json:
JSON stands for JavaScript Object Notation. It is a built-in
package provided in python that is used to store and
exchange data.

TRY CATCH
Python File Handling
file = open("someText.txt")
print(file.read())

1) Create a File: file = open("Text.txt", "x")


2) Write onto a File:
file = open("Text.txt", "w")
file.write(”New DevOps.")
file.close

3) Read a File:
file = open("Text.txt", "r")
print(file.read())
file.close

You might also like