Introduction For Python
Programming
Chapter 1 : DEC40092 (Computer Vision Programming)
What is Python?
Intro
Started by Guido van Rossum to express ideas in fewer lines of code without
reducing any readability.
Python is slower than C/C++ but easily extended with C/C++.
C++ code working in background using Python wrapper as in Python modules.
1.1 Remember features of Python
environment
Simple - no need specific IDE to run Object Oriented - python creating and using classes and
objects are downright easy
Free and Open Source - no charge, easy download
Extensible - can write other language (eg: C++) in Python
High Level Language - can be understable by human / code
English look-alike
Embeddable - can put system into different states, set
Portable - Can be run on any platform configurations and test all sort of real-world use cases
(Win/Linux/MacOS)
Extensive Libraries - huge collection libraries all over
Interpreted - because it goes through an interpreter internet
which turn code into computer language
1.2 Install Python
Windows - 7, 8.x, 10
Linux - Raspberry Pi, Odroid, so many
SBC on markets
Mac - OS X, MacOS
Others - AIX, IBM i, iOS, iPadOS,
OS/390, z/OS, Solaris, VMS, HP-UX
31.59%
Popularity Worldwide
Write a simple
Python program
“This is a super-important quote”
- From an expert
This is the most
important takeaway
that everyone has to
remember…..
1.3 Understand syntax in Python
1.3.1 Identify the basic syntax in Python
Identifier - An identifier is a name given to entities like Quotation - can be use as comment or also represent as
class, functions, variable, etc. It helps to differentiate string
one entity from another
Comments - as a reference but not included into our
Reserved Words created programming code
Lines and Indentation - to define a block of code Multiple statements - similar to multi-line statements
but this is how to make multiple line statements into a
Multi-line statement - to make it easier to write and read single line.
if the statements are too long
Docstring - short for documentation string
Identifier (The rules)
Identifiers can be a combination of letters in lowercase Python is a case-sensitive language
(a to z) or uppercase (A to Z) or digits (0 to 9) or an
underscore _. Names like myClass, var_1 and Using underscore for multiple words is allowed but can’t
print_this_to_screen, all are valid examples. use it as the first character. This is because to
differentiate either it is a docstring or not.
An identifier cannot start with a digit. 1variable is invalid,
but variable1 is a valid name. 36 keywords cannot be used as identifiers.
Cannot use special symbols like !, @, #, $, % etc. in our
identifier.
An identifier can be of any length.
Reserved Words
Additional : exec
All the keywords except True, False and None
are in lowercase and they must be written as
they are.
Lines and Indentation (1/2)
Generally using four (4) whitespaces OR tabs.
Lines and Indentation (2/2)
Also can be ignored in line continuation
Multi-line statement
Example 1 : using backslash \ Example 2 : using parentheses ( ) , brackets [ ]
and braces { }
Quotation
Single Quote ‘ OR Double Quote “
Comments
Multiple line of comment using # Multiple line of comment using ‘’’ or “””
Multiple statements
1.4 Identify basic structure in
Python
1.4.1 Explain the data types in Python
Numbers
int - positive or negative whole numbers (without a fractional part)
float - any real number with floating point
complex - a number with a real and imaginary component
OUTPUT:
Strings (1/2)
a collection of one or more characters put in single '___' , double ''___" or triple quotes '''___'''
Strings (2/2)
OUTPUT :
List (1/2)
A collection which is ordered and changeable.
Allow duplicate members.
Items separated by commas ; are enclosed
within brackets [ ].
All items in a list do not to be of the same type.
List (2/2)
Tuple (1/2)
A collection which is ordered and unchangeable.
Allow duplicate members.
Items separated by commas ; are enclosed
within parentheses ( ).
All items in a list do not to be of the same type.
Used to write-protect data and are usually faster
than list as they cannot change dynamically.
Tuple (2/2)
Dictionary (1/2)
A collection which is unordered, changeable
and indexed.
No duplicate members.
Defines within braces { } with each item being a
pair in the form key:value.
Key and value can be of any type.
Dictionary (2/2)
OUTPUT :
1.4.2 Explain basic operators in Python
Arithmetic Operators
Comparison / Relational Operators
Assignment Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic Operators
Comparison / Relational Operators (1/2)
Comparison / Relational Operators (2/2)
Assignment Operators
Bitwise Operators
Membership Operators (1/2)
to test whether a value or variable is found in a
sequence (string, list, set and dictionary)
Membership Operators (2/2)
The best way to fix
bug by print out the
value...
1.4.3 Explain program control in Python
Decision making Looping
- if Statement Syntax - for Loop
- if...else Statements Syntax - range() function
- if...elif...else Statements Syntax - for Loop with else
- Nested if Statements - while Loop
- while Loop with else
Python interprets : - loop with break statements
1. non-zero values as True - loop with continue statements
2. None and 0 are interpreted as False
if Statements Syntax
if...else Statement Syntax
if...elif..else Statement Syntax
Nested if Statements
Draw the flowchart...
for Loop
range() function (1/3)
also can be define the start, stop and step size
as range(start, stop,step_size) .
step_size defaults to 1 if not provided.
range() function (2/3)
Example 1: Output example 1:
range() function (3/3)
Example 2: Output example 2:
for Loop with else (1/2)
the keyword break can be used to stop a for
loop.
Example 1: Output example 1:
for Loop with else (2/2)
Example 2: Output example 2:
while Loop (1/2)
used to iterate over a block of code as long as
the test expression (condition) is true
while Loop (2/2)
Output:
while Loop with else
can be terminated with break statement Output:
loop with break statement (1/2)
terminates the loop containing it
loop with break statement (2/2)
Example: Output:
loop with continue statement (1/2)
to skip the rest of the code inside a loop for the
current iteration only
loop with continue statement (2/2)
Example: Output:
1.4.4 Describe modules in Python
To break down large programs into small Python Built-in Modules - type help (‘modules’)
manageable and organized files. in command prompt OR in IDLE.
Modules provide reusability. - OS Module
- Sys Module
a. import statement from Statement - Math Module
b. from .. import * Statement - Statistics Module
- Collection Module
- Random Module
Examples the usage of module
Python File - example.py Python Module - example (without extension)
import statement from Statement
Example: Output:
Example (import by renaming):
from .. import * Statement
Example 1: Example 3:
Example 2:
1.4.5 Functions (1/2)
Types of functions:
1. Built-in functions - Function that are built
into Python
2. User-defined functions - Functions defined
by the users themselves
1.4.5 Functions (2/2)
Scope and Lifetime of variables:
Output:
Advantages of user-defined functions (1/2)
Help to decompose a large program into small Example:
segments
Function can be used to include those codes
and execute when needed by calling that
function if code is repeated in a program
Can divide the workload by making different
functions
Advantages of user-defined functions (2/2)
Example to call function: Output:
Function arguments (1/3)
Example 1: Output 1:
Function arguments (2/3)
Any error?
Input Function
Try this: Output:
age = input(“What is your age : “)
print (“Your age is ” + age + “ years old”)
Thanks!
Contact me:
BP2
JKE PSAS
019 - 730 7570
[email protected]