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
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
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
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
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
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
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
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
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
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
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
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
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
accessed using the slice operator “[ ]”
with indexes starting at 0 in the beginning
of the list
eBook for You