Download Python

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Embedded Software
Development with Python
and the Raspberry Pi
Class 1: Introduction to Python
August 18, 2014
Jacob Beningo, CSDP
Presented by:
© Jacob Beningo 2014
All Rights Reserved
© All Images and content in this presentation may not be used or copied without express written permission of the owner.
Course Overview
Introduction to Python
Python Fundamentals
An Overview of the Raspberry Pi
Controlling Raspberry Pi Peripherals with
Python
• An Internet of Things Weather Station
Example
•
•
•
•
Presented by:
2
© Jacob Beningo 2014
All Rights Reserved
Session Overview
Introduction to Python
•
•
•
•
•
•
•
•
Overview of Python
Installing Python
Python Data Types
Python Operators
Python Flow Control
Built-in functions
String Manipulations
Examples
3
© Jacob Beningo 2014
All Rights Reserved
Presented by:
Overview of Python
• What is Python?
– Interpreted language
– Interactive
– Object Oriented
• Why use Python?
– Entry level language that is easy to learn
– Is portable
– Supports a large set of libraries
Presented by:
4
© Jacob Beningo 2014
All Rights Reserved
Overview of Python
• Characteristics of Python Code
– Readable
– Object Oriented
– Typically 20 – 25% size of compiled C++ / Java code
– Can call C/C++/Java/.Net and other language
extensions
– Speed of development is extremely fast
– May not be as fast as low level compiled code
Presented by:
5
© Jacob Beningo 2014
All Rights Reserved
Installing Python
• www.python.org
Download this Version!!!
Presented by:
6
© Jacob Beningo 2014
All Rights Reserved
Installing Python
Presented by:
7
© Jacob Beningo 2014
All Rights Reserved
Python Reserved Words
Python Reserved Words
C Reserved Words
and - as - assert - break -
Auto –break - case - char - const-
class - continue - def - del -
Continue - default– do - double-
elif - else - except - exect -
Else – enum – extern - float - for
finally - for - from - global -
Goto - if - int – long - register-
if - import - in - is - lambda -
Return– short- signed- sizeof-
Not - or - pass - print -raise-
Static– switch- typedef- union-
return - try while - with –yield
unsigned –void-volatile-while
Presented by:
8
© Jacob Beningo 2014
All Rights Reserved
Python Data Types
• Numeric
– Int (14)
– Long (134563)
– Float (14.34)
– Complex numbers (14.3 + 0.35j)
• String
– Myname = ‘Jacob Beningo’
– S = Myname[0]
# s = ‘J’
– S = Myname[0:4] # s = ‘Jacob’
Presented by:
9
© Jacob Beningo 2014
All Rights Reserved
Python Data Types
• List Variables
– Mylist = [‘Jacob’, ‘Beningo’, 14, ‘November’]
– S = Mylist[0]
# s = ‘Jacob’
– S = Mylist[1:2]
# s = ‘Beningo’, 14
– Mylist[0] = ‘Jake’
• Tuple Variables
– Read only!
– Mytuple = (‘Jacob’, ‘Beningo’, 14, ‘November’)
– Mytuple[0] = ‘Jake’
Presented by:
10
© Jacob Beningo 2014
All Rights Reserved
Python Data Types
• Dictionary Variables
– Similar to hash table with keys and values
– Mydictionary = {‘Name’: ‘Jacob’, ‘Surname’: ‘Beningo’}
– S = mydictionary[‘Name’]
# s= ‘Jacob’
– S = mydictionary[‘Surname’] # s = ‘Beningo’
– S = mydictionary.keys()
# s = ‘Surname’, ‘Name’
– S = mydictionary.values()
# s = ‘Beningo’, ‘Jacob’
Presented by:
11
© Jacob Beningo 2014
All Rights Reserved
Python Operators
• Arithmetic Operators
Arithmetic Operations
Python
C
Addition
+
+
Subtraction
-
–
Multiplication
*
*
Division
/
/
Modulus
%
%
Exponent
**
NA
Presented by:
12
© Jacob Beningo 2014
All Rights Reserved
Python Operators
• Comparison Operators
Comparison Operators
Python
C
Checks if 2 operands are equal
==
==
Checks if 2 operands are not equal
!=
!=
Checks if the left operand is greater
than the right operand
>
>
Checks if the left operand is less than
the right operand
<
<
Checks if the left operand is greater
than or equal to the right operand
>=
>=
Checks if the left operand is less than
or equal to the right operand
<=
<=
Presented by:
13
© Jacob Beningo 2014
All Rights Reserved
Python Operators
• Logical Operators
Logical Operators
Logical AND of the two
operands (True if both
operands are true)
Logical OR of the two
operands (True if any
operand is true)
Logical inverse of the
operand
Python
C
And
&&
Or
||
Not
!
Presented by:
14
© Jacob Beningo 2014
All Rights Reserved
Python Operators
• Logical Operators
Bitwise Operators
Python
C
Bitwise AND. Corresponding elements of both
operators are AND ed
&
&
Bitwise OR. Corresponding elements of both
operators are OR ed
|
|
Bitwise EXOR. Corresponding elements are
EXCLUSIVE OR ed
^
^
Bitwise complement. All elements of an
operand are complemented
~
~
Binary left shift operator
<<
<<
Binary right shift operator
>>
>>
Presented by:
15
© Jacob Beningo 2014
All Rights Reserved
Python Operators
• Assignment Operators
Assignment
Python
Assignment
C
Operation
Assignment Operator
=
Addition
+=
a= a + b
Add AND Operator
+=
Subtraction
-=
a=a–b
Subtract AND Operator
-=
Multiplication
*=
a=a*b
Multiply AND Operator
*=
Division
/=
a=a/b
Divide AND Operator
/=
Modulo
%=
a=a%b
Bitwise AND
&=
a=a&b
Bitwise OR
[-
a=a{b
Bitwise XOR
^=
a=a^b
Bitwise left shift
<<=
a = << b
Bitwise right shift
>>=
a = >> b
Presented by:
16
© Jacob Beningo 2014
All Rights Reserved
Python Flow Control
• Overview
Python
C
If…else
If…Else
Elif
N/A
For
For
While
While
Break
Break
Continue
Continue
N/A
Switch
N/A
Do…while
Pass
N/A
Presented by:
17
© Jacob Beningo 2014
All Rights Reserved
Python Flow Control
• If statement
Python
C
If expression:
statements
Else:
statements
If (expression)
{
statements;
}
Else
{
statements;
}
If x == 14:
Delta = 3
Elif x > 15:
Delta = 2
Else:
Delta = 1
Presented by:
18
© Jacob Beningo 2014
All Rights Reserved
Python Flow Control
• for statement
Python
C
For variable in sequence:
statements
For(start value; end condition; increase value)
{
statements;
}
Example:
For letter in “Beningo”:
print(letter)
Presented by:
19
© Jacob Beningo 2014
All Rights Reserved
Python Flow Control
• while statement
Python
C
while expression:
statements
while(expression)
{
statements;
}
Count = 0
while Count < 14:
print Count
Count = Count + 1
Presented by:
20
© Jacob Beningo 2014
All Rights Reserved
Built-in Functions
•
•
•
•
•
•
•
•
abs(x)
bin(x)
hex(x)
log(x)
log10(x)
pow(x,y)
round(x,n)
sqrt(x)
•
•
•
•
•
•
•
•
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x,y)
degrees(x,n)
radians(x)
Presented by:
21
© Jacob Beningo 2014
All Rights Reserved
String Manipulations
• print
• print formatting characters
Formatting Characters
Character
%c
String
%s
Integer (Signed)
%d
Integer (Unsigned)
%u
Lower case hexadecimal
%x
Upper case hexadecimal
%X
Floating point
%f
Exponential notation
%E
Presented by:
22
© Jacob Beningo 2014
All Rights Reserved
String Manipulations
•
•
•
•
•
•
•
•
capitalize()
count(str, beg, end)
find(str, beg, end)
len(string)
isdigit()
islower()
lower()
upper()
•
•
•
•
•
isalpha()
isalnum()
lstrip()
rstrip()
swapcase()
Presented by:
23
© Jacob Beningo 2014
All Rights Reserved
Going Further
• Download Template Materials for
– Python Doxygen Script Template
– Personal Weather Station Python Script
– Raspberry Pi Example Python Scripts
From www.beningo.com under
– Blog and Articles > Software Techniques >
Embedded Software with Python
Presented by:
24
© Jacob Beningo 2014
All Rights Reserved
Contact Information
P.O. Box 400
Linden, Michigan 48451
: [email protected]
: 810-844-1522
: Jacob_Beningo
Jacob Beningo
: Beningo Engineering
Principal Consultant
: JacobBeningo
: Embedded Basics
Presented by:
25
© Jacob Beningo 2014
All Rights Reserved
© All Images and content in this presentation may not be used or copied without express written permission of the owner.