Python Programming 101 PDF
Python Programming 101 PDF
eafaisal@gmail.com
github.com/efaisal
twitter.com/efaisal
facebook.com/eafaisal
plus.google.com/100085338667833364551
ABOUT THIS COURSE
What is Programming?
● Instruction to do something to achieve a
desired result
● Using series of steps or actions that a
programmer defines (procedure)
● Each action works on certain object(s) or data
in order to attain a well defined goal
REAL WORLD EXAMPLE OF
PROGRAMMING
Shampoo Instruction
● Wash
● Rinse
● Repeat
An Overview
A little history:
● Desktop applications/games
● Web-based applications
● System administrator tasks
automation
● Scientific & engineering data analysis
WHAT IS PYTHON? (cont)
● Integers ● Fractions
● Repeat
>>> b = 'ha '
>>> b * 3
'ha ha ha '
>>>
DATATYPES: STRING
● Blank dictionaries
>>> a = {}
>>> b = dict()
● Another example:
>>> for i in range(2, 10):
… if i % 2 == 0:
… print(i, ' is an even number') # Python 3
… print i, ' is an even number' # Python 2
… continue
… print(i, ' is an odd number') # Python 3
… print i, ' is an odd number' # Python 2
● continue keyword means continues with next iteration
CONTROL FLOW: WHILE
● A named section of a
program that performs
specific task
● Allow to organize code block
which repeatedly used into a
reusable procedure or
routine
● Python comes with a lot of
built-in functions which we
already seen some of them
FUNCTION: DEFINING
● Another example:
>>> try:
… z/0
… except (NameError, ZeroDivisionError) as e:
… print(“Error: {0}.format(e.args[0]))
… except:
… print('some other error')
…
● There is also try-except-else and try-except-else-finally
statement
● The else block will be evaluated after successful run of try block
while the finally block will always executed
RAISING EXCEPTION
def func2():
print('func2 from mymod')
● Open Python shell from the same directory
>>> import mymod
>>> mymod.func1()
>>> mymod.func2()
● Module is accessed using dot notation
MODULE: PACKAGE
● enumerate([seq, start=0])
● raw_input([prompt])
● len(s)
● range([start,] stop[, step]), xrange()
● open(name[, mode])
● set([iterable])
● int(), oct(), hex(), bin()
STANDARD LIBRARIES
● sys
● os
● time
● datetime
● math
● decimal
● logging
● doctest & unittest
Introductory Course: Practical Python Programming
INTRODUCTORY COURSE TO
PRACTICAL PYTHON PROGRAMMING
eafaisal@gmail.com
github.com/efaisal
twitter.com/efaisal
facebook.com/eafaisal
plus.google.com/100085338667833364551
Instructor: E A Faisal 1
Introductory Course: Practical Python Programming
Instructor: E A Faisal 2
Introductory Course: Practical Python Programming
INTRODUCING PROGRAMMING
What is Programming?
● Instruction to do something to achieve a
desired result
● Using series of steps or actions that a
programmer defines (procedure)
● Each action works on certain object(s) or data
in order to attain a well defined goal
Instructor: E A Faisal 3
Introductory Course: Practical Python Programming
Shampoo Instruction
● Wash
● Rinse
● Repeat
Instructor: E A Faisal 4
Introductory Course: Practical Python Programming
COMPUTER PROGRAMMING
Instructor: E A Faisal 5
Introductory Course: Practical Python Programming
WHAT IS PYTHON?
An Overview
Instructor: E A Faisal 6
Introductory Course: Practical Python Programming
A little history:
Instructor: E A Faisal 7
Introductory Course: Practical Python Programming
Instructor: E A Faisal 8
Introductory Course: Practical Python Programming
● Desktop applications/games
● Web-based applications
● System administrator tasks
automation
● Scientific & engineering data analysis
Instructor: E A Faisal 9
Introductory Course: Practical Python Programming
Show demo:
1. Game
2. Facial recognition
Instructor: E A Faisal 10
Introductory Course: Practical Python Programming
INTRODUCING IDLE
Instructor: E A Faisal 11
Introductory Course: Practical Python Programming
Instructor: E A Faisal 12
Introductory Course: Practical Python Programming
STORING VALUE
Instructor: E A Faisal 13
Introductory Course: Practical Python Programming
DATATYPES: NUMBERS
● Integers ● Fractions
Instructor: E A Faisal 14
Introductory Course: Practical Python Programming
Instructor: E A Faisal 15
Introductory Course: Practical Python Programming
PRIMITIVE
DATATYPES
Instructor: E A Faisal 16
Introductory Course: Practical Python Programming
DATATYPES: STRINGS
Instructor: E A Faisal 17
Introductory Course: Practical Python Programming
DATATYPES: STRINGS
● Repeat
>>> b = 'ha '
>>> b * 3
'ha ha ha '
>>>
Instructor: E A Faisal 18
Introductory Course: Practical Python Programming
DATATYPES: STRING
Instructor: E A Faisal 19
Introductory Course: Practical Python Programming
DATATYPE: STRINGS
Instructor: E A Faisal 20
Introductory Course: Practical Python Programming
BASIC
DATA STRUCTURE
Instructor: E A Faisal 21
Introductory Course: Practical Python Programming
Instructor: E A Faisal 22
Introductory Course: Practical Python Programming
Instructor: E A Faisal 23
Introductory Course: Practical Python Programming
● Blank dictionaries
>>> a = {}
>>> b = dict()
Instructor: E A Faisal 24
Introductory Course: Practical Python Programming
CONTROL
FLOWS
Instructor: E A Faisal 25
Introductory Course: Practical Python Programming
CONTROL FLOW: IF
Instructor: E A Faisal 26
Introductory Course: Practical Python Programming
Instructor: E A Faisal 27
Introductory Course: Practical Python Programming
● Another example:
>>> for i in range(2, 10):
… if i % 2 == 0:
… print(i, ' is an even number') # Python 3
… print i, ' is an even number' # Python 2
… continue
… print(i, ' is an odd number') # Python 3
… print i, ' is an odd number' # Python 2
● continue keyword means continues with next iteration
Instructor: E A Faisal 28
Introductory Course: Practical Python Programming
Instructor: E A Faisal 29
Introductory Course: Practical Python Programming
FUNCTIONS
Instructor: E A Faisal 30
Introductory Course: Practical Python Programming
WHAT IS A FUNCTION?
● A named section of a
program that performs
specific task
● Allow to organize code block
which repeatedly used into a
reusable procedure or
routine
● Python comes with a lot of
built-in functions which we
already seen some of them
Instructor: E A Faisal 31
Introductory Course: Practical Python Programming
FUNCTION: DEFINING
Instructor: E A Faisal 32
Introductory Course: Practical Python Programming
Instructor: E A Faisal 33
Introductory Course: Practical Python Programming
Instructor: E A Faisal 34
Introductory Course: Practical Python Programming
Instructor: E A Faisal 35
Introductory Course: Practical Python Programming
FUNCTION: ANONYMOUS
Instructor: E A Faisal 36
Introductory Course: Practical Python Programming
OBJECT ORIENTED
PROGRAMMING (OOP)
Instructor: E A Faisal 37
Introductory Course: Practical Python Programming
OOP: INTRODUCTION
Instructor: E A Faisal 38
Introductory Course: Practical Python Programming
Instructor: E A Faisal 39
Introductory Course: Practical Python Programming
Instructor: E A Faisal 40
Introductory Course: Practical Python Programming
OOP: INHERITANCE
Instructor: E A Faisal 41
Introductory Course: Practical Python Programming
Instructor: E A Faisal 42
Introductory Course: Practical Python Programming
Instructor: E A Faisal 43
Introductory Course: Practical Python Programming
EXCEPTION HANDLING
Instructor: E A Faisal 44
Introductory Course: Practical Python Programming
● Another example:
>>> try:
… z/0
… except (NameError, ZeroDivisionError) as e:
… print(“Error: {0}.format(e.args[0]))
… except:
… print('some other error')
…
● There is also try-except-else and try-except-else-finally
statement
● The else block will be evaluated after successful run of try block
while the finally block will always executed
Instructor: E A Faisal 45
Introductory Course: Practical Python Programming
RAISING EXCEPTION
Instructor: E A Faisal 46
Introductory Course: Practical Python Programming
MODULES
Instructor: E A Faisal 47
Introductory Course: Practical Python Programming
MODULE: INTRODUCTION
Instructor: E A Faisal 48
Introductory Course: Practical Python Programming
MODULE: EXAMPLE
def func2():
print('func2 from mymod')
● Open Python shell from the same directory
>>> import mymod
>>> mymod.func1()
>>> mymod.func2()
● Module is accessed using dot notation
Instructor: E A Faisal 49
Introductory Course: Practical Python Programming
MODULE: PACKAGE
Instructor: E A Faisal 50
Introductory Course: Practical Python Programming
BATTERIES INCLUDED
Instructor: E A Faisal 51
Introductory Course: Practical Python Programming
BATTERIES INCLUDED
Instructor: E A Faisal 52
Introductory Course: Practical Python Programming
BUILT-IN FUNCTIONS
● enumerate([seq, start=0])
● raw_input([prompt])
● len(s)
● range([start,] stop[, step]), xrange()
● open(name[, mode])
● set([iterable])
● int(), oct(), hex(), bin()
Instructor: E A Faisal 53
Introductory Course: Practical Python Programming
STANDARD LIBRARIES
● sys
● os
● time
● datetime
● math
● decimal
● logging
● doctest & unittest
Instructor: E A Faisal 54