0% found this document useful (0 votes)
6 views16 pages

Python For DevOps

The document provides an overview of Python programming concepts, including data types, functions, loops, and exception handling. It covers key features such as mutable and immutable types, operators, and the use of decorators and lambda functions. Additionally, it discusses the structure of Python modules and packages, as well as the importance of code readability and documentation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

Python For DevOps

The document provides an overview of Python programming concepts, including data types, functions, loops, and exception handling. It covers key features such as mutable and immutable types, operators, and the use of decorators and lambda functions. Additionally, it discusses the structure of Python modules and packages, as well as the importance of code readability and documentation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

python Intuitive open source

Easyto understand
use

current version 3.13.3

print1 function Build in function


newline in

multiple arguments s

end 4 sep keywords

Literals value explicitly assigned to


variable

Integers Fooleans
Floating strings
point T 11011
numbers
octal Hexadecimal
00 Ox
variables container Mutable
Uppercase v
Lowerlase Shorcut
operators
digit
For
underscore code simplification
lase sensitive

coments H readability documentation

input function

promt input return entered


value as a string

Typecasting is
necessary to
convert output
into other datatype
for other usecases
eg Str 22

string methods

string concatenation t lombines 2 strings

string repetition
Type conversion str 1
operators

arithmetic
unkans
operators operator
1 Mator
V

I I II
Alwaysreturn
Floating point
number
Fists
pi
iii
nextlower awayto

it o iii no

operator precedence
High
priority sub expressions
Unary operator 1

Exponentiation
1 11
Addition substruction
1 9Y
comparision operators defines conditions

Logital and [Link] T

operators or FV F F
not T F
7 AMD 4 compare
Binary 141 71
representn

OR lumpare
Bitwise Binary 010 0
operators representn

XOR compare
be
Binary 9
representn
used
short cutly NOT N compare
Binary
representn
a isb returns true if a b
Identity
operator a is not b returns true if a b
in True if present
Mperabteoyship is s
or not not in True if absent
if

conditional if else
statements if elif else
nested if
indentation

While loop runs until specified londition


remains true
Used to Handle unknown
number of iterations

while loop with else clause


Executed only when loop is terminated
not triggered by break statement
normally

For loop iterate in times rangyfunction


1 Breaking out a loop exit loop entirely
Anycodebelow break not executed

ii skiping iteration with continue


skip only current iterationonly
insteadof terminating it completly

Filtering out unwanted cases is


its use case
shift 22951
right
Bit
shifting left shift as 22 1

right shift by integer division by 2


right shift by 2 integer division by 4
left shift by 1 integer multiplicath by 2
left shift by 2
Integer multiplicath by4

Function Reusability of code

def fun name Pml Pmn


code
syntax
returnstatement
Always define function before
invoking it
Order of parameters decides assignment
of arguments utilise named parameters
Avoid value assigning to same parameter
more than once cg Funct 10 num 20
Python automatically uses default
value for parameter if provided
If caller provides value then
default value is overided
70 stops execution once
encountered
return key word
Exit function early
based on condition

returns value none


by default
Local variables variables inside function
cannot be assessed outside
Global variables variables outside function

If same variable presents globally totally


local will take precedence called shadowing
If no Local variable Funeth use global variable

Declaring variable as global allows to


modify it inside function making it
allessible in the global slope after function
call
Mutable list passed by reference
changes within functions will
objects modify original object
Immutable scalars passedby value
Functions work with a copy
leaving original value unchanged
Arguments values passed to function call
parameters values given during function treation

Types of Funition arguments

Positional Arguments values passed in


correct order

keyword Arguments values passed alongwith


parameter names no order

Default Arguments Default values if not


passed

variable length positional argument args


for unknown no of arguments stored as tuple

variable length keyword arguments kwargs

to pass multiple named arguments dynamically

stored as dictionary

Relursion function calling itself


Python module contains python code that can
be used within any other python code by using

import module name Any function var can also be


imported using from module import funct var name

group of modules organised in


python package
directory Any palliage can be identified using init py
file and used using from [Link] import module name

Python workspace Environment in which we


lode interperter lib workingdiret
develope run python
Local environment systemwide create
Python in [Link]
virtual environment
source [Link] bin activate

Data types in Python

List ordered collection mutable

Tuple ordered lollection 1 Immutable

range range start val stop Val step Immutable


I
default 1 default

Dictionary key value No duplication Immutable


set unordered lollection No duplication i

Integer Float complex Boolean Numbers

string Array of characters


stt tens
Str upperi
str lower

Str stripal space removal

Str replace
words into list of words
str split 1 split
Str capitalize 1
of word
Str Find ins Returns starting index
create
Modify
List Mutable operations
length
ordered determination
contains elements Removeelement del
of various types
using negative
indexing
Index errors
list count lount of no of items with
givenvalue
list append at end
Methods list insert 1 at specific location
list sort c Ascending by default
list reverse 1
Reverse list
list index indexof 1st occurance of list
item
succint technique list copy
to swap 2 numbers list i list Citi list it list i

Iterating in list For var in list

understanding list Assigning list to another variable


lopies the reference not list
content
a
iii iii [Link] sIi
creates an entire copy of the
list modifying one not reflectother
Finding in
value in list
Returns T F
value not in pig

V 2D I
Nested List indices
Lists starts at 0
Basic syntax list start End
element start index included
slicing element last indent Excluded
Lists slicing with n n
single inde
using negative n n
indises
using delete keyword with
sliting del list ni n2
del list
eles eler
List concatenation lists lists

Tuples ordered data type Immutable


eled e2 eln
treating var nam

Iterating through tuples


7 For var in tuple name
tuple name appendlelement Yutported
tuple name n value
operations tuple length ten tuple name

tuple indexing tuple name n

s
packing unpacking up 171,772
x y tap
concatenation
typs tup2 e 2 112
Je ele in tuple name theclis elementintuple
returning multiple values from function
stores data in key value format
Dictionary
similar to hashmap associative array

dict name key value


creation
accessing dict name elley
key valu
Modification dirt name
operations
remove key pair dist name key

add new key pair dist name update


keys value

copying dictionary dictz dicts copy 1

keys l iterable view of keys


common cg For var in dirt Keys1
dictionary values iterable view of values
methods eg print dirt values 1
items l iterable view of key value
pair as tuple
cg For vari varz in dict values

sets unordered collection of unique elements

union
useful for
Mathematical intersection
operations
difference
creation set name eles elez
add remove set_nameadd ele remove ele

union set sets union sets


opertations intersection set sets intersection set 2
difference set sets difference set21
check subset set 1 issubset sets
check superset set 1 issuperset set 2

Decorator Function that wraps another function and


add extra behavior to a function without changing code
defining decorator

Applying decorator

use case Adding functionalities to function


other

running like 111 when function is talled


121 how much time it takes
3 triggered error or not
Exception handling
uses try and except block
Normal Execution
execution after error

It Lambda function in Python


function defined in single line
small anonymous
for short term operations

eg add lambda a b atb

print add 1,211

You might also like