Python Slides PDF
Python Slides PDF
Part I: Python
1
Introduction To The Course – Cont.
2
Introduction To The Course – Cont.
Part I: PYTHON
3
Introduction To Python
Free
Fast to develop
Portable (no change to code needed)
Wide variety of libraries
One of the most popular languages
Example applications:
YouTube, Instagram, Dropbox, Spotify(desktop)
4
Tools and Installation
Python 2.7
Windows: will need to install
Go to www.ptyon.org and install
5
Tools and Installation
Pip
Install pip
Editor or IDE
PyCharm
Sublime
Eclipse
Notepad++
6
Variables
Variables
7
Variables – Cont.
Variables – Cont.
8
Variables – Cont.
Reserved Words
Variables – cont.
9
Variables – Cont.
Variables –Cont.
Multiple assignment
Can assign single value to multiple variables
Ex:
>>> my_var1 = my_var2 = my_var3 = 500
Can assign multiple variable to multiple values in one line
Ex:
>>> car1, car2, car3 = ‘Honda’, ‘Toyota’, ‘BMW’
10
Data Types
Data Types
Python has many datatypes
Also referred as Built-in types
Few examples:
• Numeric types (integers, floats…)
• Sequence types (strings, lists, tuple,….)
• Mapping type (dictionaries)
• Booleans (True, False)
• And more ….
Will use most of the above types in Automation
11
INTEGERS
Integers – Cont.
Operations on integers
12
Integers – Cont.
Operator Precedence
5 * 2 + 4 is it 14 or 40
High school math tell us multiplication has precedence over
addition and …..
So 5*2 gets evaluated first then 4 is added.
“Please Excuse My Dear Aunt Sally” easy way to remember
precedence.
Parenthesis, Exponents, Multiplication, Division, Addition,
Subtraction
Use parenthesis to avoid confusion and having to remember
FLOAT
13
Floats – Cont.
STRINGS
14
String - Cont.
Strings – Cont.
15
Stings – Cont.
>>> abc.split()
>>> [‘Hello’, ‘World’]
https://docs.python.org/2/library/stdtypes.html
Strings – Cont.
String Formatting:
– You will use string formatting frequently in automation
– Specially in reporting and displaying error messages
– Place holders in strings are %s, %d, %f
– %s string
– %d integers
– %f float
Ex:
>>> ap = ‘Oakland’
>>> my_string = “I am flying to %s airport” % ap
>>> print(my_string)
>>> I am flying to Oakland airport
16
Strings – Cont.
LISTS
17
Lists – Cont.
Lists – Cont.
18
List - Continued
Methods:
Just like string methods there are also several methods for lists
Ex:
>>> pc = [‘Dell’, ‘HP’, ‘Toshiba’] >>>x = pc.pop()
>>> len(pc) >>>print pc
>>> 3 >>>[‘Dell’, ‘HP’, ‘Toshiba’]
>>> pc.append(‘Apple’) >>>print x
>>> print pc >>>’Apple’
>>> [‘Dell’, ‘HP’, ‘Toshiba’, ‘Apple’] >>> pc.remove(“HP”)
>>>print pc
DICTIONARIES
Not sequenced
Key:Value pair
19
Dictionaries – Cont.
Ex:
>>> NFL = {“Oakland” : “Raiders” , “San Francisco” : “49ers”, “Denver” : “Broncos”}
Dictionaries – Cont.
20
Dictionaries – Cont.
TUPLES
21
TUPLES – Cont
Examples:
>>> tuple_a = (1, 2, 3, 4)
TUPLES – Cont.
22
Control Flow
23
Boolean – Cont.
X Y operation Result
True True X and Y True
24
Control Flow – “if – else” statements
Made decisions based on it a condition is true or false
If some condition is true do this but if its not, then do this
instead
Code block for each ‘if’ and ‘else’ is indented (4 spaces
typically)
Each statement must end with “:”
Syntax:
>>> if <something is true>:
<then do this>
else:
<do something else>
25
Control Flow - Loops
• ‘for’ loop
• ‘while’ loop
FOR loop
‘for’ loop is counting loop
Need to use iterable object like a list
The block of code for the ‘for’ loop is indented
The ‘for’ statement must end with “:”
Syntax:
for <variable> in <iterable object>:
Do some action
– If the iterable object has X number of items the “Do some action” will
repeat X times.
26
for loop – Cont..
Ex.
>>> my_list = [‘houme’, ‘car’, ‘bike’, ‘boat’]
>>> for i in my_list:
print i
>>> house
>>> car
>>> bike
>>> boat
27
While loop
execute code repeatedly until a condition is
met
risky to get infinite loop
the condition must change to False at some
point
“ctr + c” to stop infinite loop in most systems
python has its own timeout (do not rely on it)
Ex:
>>> counter = 0
>>> while counter <= 4:
counter += 1
print ‘Currently counter is: %d’ % counter
>>> Currently counter is: 1
Currently counter is: 2
Currently counter is: 3
Currently counter is: 4
Currently counter is: 5
28
“break” and “continue”
29
“break” and “continue” – Cont.
Syntax:
>>> for i in my_list:
Do this
Do this
Do this
if <something is true>:
continue
Do this
Do this
** while looping the list when the if statement is true then the last two
statements will not execute. The loop will go to top and start the next
Functions
30
Functions
Functions are block of code packaged in one line
Functions help us avoid repeated code
Define a function (a task) once and use repeatedly
Function definition start with the word ‘def’
Syntax:
>>> def my_first_function(input parameters):
<some code here>
return <something>
The ‘return’ statement is optional if nothing to return
Also the input parameters are optional
Functions – Cont.
Ex:
>>> def my_adding_func(a,b):
total = a + b
return total
“Calling a function” means using the function
Ex:
>>> my_adding_func(5,10)
>>> total_value = my_adding_func(5,10)
>>> print total_value
>>> 15
31
Functions – Cont.
>>> my_adding_func(5,10)
5 and 10 are the arguments when calling the function
Arguments take place of the parameters throughout the
function
If function is defined with parameters it must have
arguments when called.
Number or parameters and arguments must match
Exception Handling
Exceptions are Errors
We will learn how to handle them
Several different types of exceptions
>>> dir(exceptions)
Ex:
TypeError, IOError, DivisionByZeroError
>>> Usually we can anticipate the errors and handle
them
32
Exception Handling – Cont.
33
Exception Handling – Cont.
34
Exception Handling – Cont.
35