TRANSMISSION &
DISTRIBUTION
A Division of Global Power
POWER SYSTEM STABILITY CALCULATION TRAINING
D13
It d ti t Pth
D
ay
13
-
I
n
t
ro
d
uc
ti
on
t
o
P
y
th
on
December 4, 2013Prepared by: Mohamed El Chehaly
eBook for You
OUTLINE
2
OUTLINE
• Basic Syntax
• Variables
• Control Flow Tools
• Built-In Functions and Methods
• User-Defined Functions
• Files and Directories
eBook for You
3
BASIC SYNTAX
BASIC
SYNTAX
eBook for You
Introduction
4
BASIC SYNTAX
Introduction
Easy to learn, powerful programming tool
Efficient high
level data structures
Efficient
high
-
level
data
structures
Simple but effective approach to object-
oriented programming
oriented
programming
Programs in Python much shorter than
equivalent C C++ or Java programs:
equivalent
C
,
C++
or
Java
programs:
Complex operations can be expressed in a single
statement
Statement grouping is done by indentation instead
of beginning and ending brackets
Nibl tdlti
N
o var
i
a
bl
e or argumen
t
d
ec
l
ara
ti
ons are
necessary
eBook for You
Introduction
5
BASIC SYNTAX
Introduction
Interpreted: It is processed at runtime by
the interpreter and you do not need to
the
interpreter
and
you
do
not
need
to
compile your program before executing it
Interactive:
You can actually sit at a
Interactive:
You
can
actually
sit
at
a
Python prompt and interact with the
inter
p
reter directl
y
py
Object-oriented: Python supports style or
technique of programming that
encapsulates code within Objects
Beginner’s language: Great language for
beginner programmers
eBook for You
Python Basic Syntax
6
BASIC SYNTAX
Python
Basic
Syntax
Interactive mode programming
Open IDLE (Python GUI)
Open
IDLE
(Python
GUI)
eBook for You
Python Basic Syntax
7
BASIC SYNTAX
Python
Basic
Syntax
Interactive mode programming
Write the following
Write
the
following
print “Hello; Python!”
eBook for You
Python Basic Syntax
8
BASIC SYNTAX
Python
Basic
Syntax
Script mode programming
Open new window
Open
new
window
eBook for You
Python Basic Syntax
9
BASIC SYNTAX
Python
Basic
Syntax
Script mode programming
Write the following
Write
the
following
print “Hello, Python!”
eBook for You
Python Basic Syntax
10
BASIC SYNTAX
Python
Basic
Syntax
Script mode programming
Save as “Hello Python py”
Save
as
“Hello
Python
.
py”
Run module
eBook for You
Python Identifiers
11
BASIC SYNTAX
Python
Identifiers
Name used to identify
Variable
Variable
Function
Class
Class
Module
Other ob
j
ects
j
Starts with
Letter A to Z
Letter a to z
Underscore
No punctuation characters such as @,$,%
eBook for You
Python Identifiers
12
BASIC SYNTAX
Python
Identifiers
Python is case sensitive (name and Name)
CtifPth
C
onven
ti
on
f
or
P
y
th
on
Class names start with an uppercase letter
Starting an identifier with a single leading
Starting
an
identifier
with
a
single
leading
underscore “_” indicates that the identifier is
private
Starting an identifier with two leading underscores
“__”indicates a strongly private identifier
If the identifier also ends with two trailing
If
the
identifier
also
ends
with
two
trailing
underscores, the identifier is a language-defined
special name
eBook for You
Python Identifiers
13
BASIC SYNTAX
Python
Identifiers
Reserved words
eBook for You
Lines and Indentation
14
BASIC SYNTAX
Lines
and
Indentation
Blocks of code for class and function
definitions or flow control denoted by line
definitions
or
flow
control
denoted
by
line
indentation
No use of braces
“
{}
”
No
use
of
braces
{
}
Test the following code
Test the following code
Test
the
following
code
eBook for You
Multi
Line Statements
15
BASIC SYNTAX
Multi
-
Line
Statements
Statements in Python typically end with a
new line
Continuation on next line allowed with “\”
Statements contained within [ ], { } and ( )
do not need to use the line continuation
ht
c
h
arac
t
er
eBook for You
Quotation in Python
16
BASIC SYNTAX
Quotation
in
Python
Python accepts single (‘), double (“) and
triple (‘’’ or “””) quotes to denote string
The same types of quote should start and
dth ti
en
d
th
e s
t
r
i
ng
The triple quotes can be used to span the
t i lti l li
s
t
r
i
ng across mu
lti
p
l
e
li
nes
eBook for You
Comments in Python
17
BASIC SYNTAX
Comments
in
Python
A hash sign “#” that is not inside a string
lateral begins a comment
All characters after the # and up to the
hilli d tfth t
p
h
ys
i
ca
l
li
ne en
d
are par
t
o
f
th
e commen
t
A line can be commented by ALT+3 and
tdb ALT+4
uncommen
t
e
d
b
y
ALT+4
eBook for You
Waiting for the User
18
BASIC SYNTAX
Waiting
for
the
User
The following line of the program displays
th t “P th t k t i t ”
th
e promp
t
“P
ress
th
e en
t
er
k
ey
t
o ex
i
s
t
.
”
The program will wait for the user to
press the Enter key
press
the
Enter
key
Here “\n\n” are being used to create two
new lines before displaying the actual line
new
lines
before
displaying
the
actual
line
This is a nice trick to keep a console
window open until the user is done with an
window
open
until
the
user
is
done
with
an
application
eBook for You
19
VARIABLES
VARIABLES
eBook for You
Standard Data Type
20
VARIABLES
Standard
Data
Type
Numbers
Strin
g
g
List
List
Tuple
Tuple
Dictionary
Dictionary
eBook for You
Assigning Values to Variables
21
VARIABLES
Assigning
Values
to
Variables
Python variables do not have to be
explicitly declared
The declaration happens automatically
when you assign a value to a variable
eBook for You
Numbers
22
VARIABLES
Numbers
Python supports four different numerical
types
types
int: signed integers
lon
g
: lon
g
inte
g
ers
(
can also be re
p
resented in
g
gg( p
octal and hexadecimal)
float: floating point real values
complex: complex numbers
Numbers with different base can be
represented
represented
Binary: start with 0B or 0b
Octal: start with 0O or 0o
Octal:
start
with
0O
or
0o
Hexadecimal: start with 0x or 0X
eBook for You
Numbers
23
VARIABLES
Numbers
Examples
Complex numbers
a = 2.1+3.14j
l
21
a.rea
l
=
2
.
1
a.Imag = 3.14
eBook for You
Strings
24
VARIABLES
Strings
Set of characters between quotation
marks
marks
>>>str1 = 'Hello World!‘
>>>print str1 # Prints complete string
Hello World!
>>>print str1[0] # Prints first character of the string
H
d
th
>>>print str1[2:5] # Prints characters starting from 3
r
d
to 5
th
llo
>>>print str1[2:] # Prints string starting from 3
rd
character
llo World!
>>>print str1*2 # Prints string two times
Hello World! Hello World!
>>> print str1 + "TEST" # Prints concatenated string
Hello World!TEST
eBook for You
Lists
25
VARIABLES
Lists
Most versatile of Python’s compound data
types
types
A list contains items separated by
commas and enclosed within square
commas
and
enclosed
within
square
brackets “[ ]”
All the items belonging to a list can be of
All
the
items
belonging
to
a
list
can
be
of
different data type
The values stored in a list can be
The
values
stored
in
a
list
can
be
accessed using the slice operator “[ ]”
with indexes starting at 0 in the beginning
of the list
eBook for You