Вы находитесь на странице: 1из 45

1

Introduction
RASPBERRY PI AND PYTHON
DAY TWO
2
What is Python Language???
 Python is a programming language that lets you work more
quickly and integrate your systems more effectively.
 Python is a widely used general-purpose, high-level
programming language.
 Its design philosophy emphasizes code readability.
 Its syntax allows programmers to express concepts in fewer
lines of code than would be possible in languages such as
C++ or Java.
 The language provides constructs intended to enable clear
programs on both a small and large scale.
3
About Python Language

 Highest Position (since


1992): #4 in Feb 2011
 Lowest Position (since
1992): #13 in Feb 2003
 Language of the Year:
2007, 2010
4
The IDLE

 IDLE is the standard Python development environment.


 An acronym of "Integrated DeveLopment Environment".
 It works well on both Unix and Windows platforms.
 It has a Python shell window, which gives you access to
the Python interactive mode.
 You can type Python code directly into this shell, at the '>>>'
prompt. Whenever you enter a complete code fragment, it
will be executed.
 For instance, typing: >>> print('Raspberry Pi Workshop')
Raspberry Pi via Putty

Windows PC
Python’s Consoles Screenshots
5
6
Python Statement
 Instructions that a Python interpreter can execute are called
statements.
 For example, a = 1 is an assignment statement.
 if statement, for statement, while statement are other kinds of
statements.
 In Python, end of a statement is marked by a newline
character.
7
Python Statement (Cont.)
 But we can make a statement extend over multiple lines with
the line continuation character (\). For example:
a= 1+2+3+\
4+5+6+\
7+8+9
8
Python Statement (Cont.)

 This is explicit line continuation. In Python, line continuation is


implied inside parentheses ( ), brackets [ ] and braces { }. For
instance, we can implement the above multi-line statement
as:
a= (1 + 2 + 3 +
4+5+6+
7 + 8 + 9)
9
Python Statement (Cont.)

Here, the surrounding parentheses ( ) do the line continuation


implicitly. Same is the case with [ ] and { }. For example:
colors = ['red',
'blue', 'green']
10
Python Indentation

 Most languages like C, C++, Java use braces { } to define a


block of code
 Python uses indentation.
 A code block (body of a function, loop etc.) starts with
indentation and ends with the first un-indented line.
 The amount of indentation is up to you, but it must be
consistent throughout that block.
 Generally four whitespaces are used for indentation and is
preferred over tabs.
11
Python Indentation (Cont.)

 Example:
for i in range(1,11):
print(i)
if i == 5:
break
12
Python Indentation (Cont.)

 The enforcement of indentation makes the code look neat


and clean.
 This results into Python programs that look similar and
consistent.
 Indentation can be ignored in line continuation. Example B
 But it's a good idea to always indent.
 It makes the code more readable. Example A
 Both are valid and do the same thing. But the former style is
clearer.
13
Python Indentation (Cont.)

Example - A Example - B
if True: if True: print('Hello'); a = 5
print('Hello')
a=5
14
Python Comments
 Comments are very important while writing a program.
 It describes what's going on inside a program.
 So a person looking at the source code will easily figure it out.
 In Python, we use the hash (#) symbol to start writing a
comment.
 It extends up to the newline character.
 Comments are for programmers for better understanding of a
program.
 Python Interpreter ignores comment.
15
Python Comments (

 Example:
#This is a comment
#print out Hello
print('Hello')
16
Python Comments (

 Now Multi-line comments.


 If we have comments that extend multiple lines, one way of
doing it is to use hash (#) in the beginning of each line. For
example:
#This is a long comment
#and it extends
#to multiple lines
17
Python Comments (

 Another way is:


"""This is also a
perfect example of
multi-line comments"""
18
Python Variables
 A variable is a location in memory used to store some data
(value).
 They are given unique names to differentiate between
different memory locations.
 We don't need to declare a variable before using it.
 In Python, we simply assign a value to a variable and it will
exist.
 We don't even have to declare the type of the variable.
 This is handled internally according to the type of value we
assign to the variable.
19
Python Variables (Cont.)
 We use the assignment operator (=) to assign values to a variable.
 Any type of value can be assigned to any valid variable.
a=5
b = 3.2
c = "Hello"
 Above are 3 assignment statements. 5 is an integer assigned to
the variable a.
 Similarly, 3.2 is a floating point number.
 "Hello" is a string assigned to the variables b and c respectively
20
Python Variables (Cont.)
Multiple assignments
 Multiple assignments can be made in a single statement as
follows:
a, b, c = 5, 3.2, "Hello"
 If we want to assign the same value to multiple variables:
x = y = z = "same"
21
Python Variables (Cont.)
 Set is an unordered collection of unique items.
 Set is defined by values separated by comma inside braces { }.
 Items in a set are not necessarily ordered.
a = {5,2,3,1,4}
 Auto sorted when called:
>>> a = {5,2,3,1,4}
>>> a
set( [ 1, 2, 3, 4, 5 ] )
>>>
22
Python Input, Output and Import

 Python language provides numerous built-in functions.


 That are readily available to us at the Python prompt.
 Some of the functions like input() and print() are widely used
for standard input and output operations respectively.
23
Python Output
 We use the print() function to output data to the screen.
 An example use is given below:
>>> print('This sentence is output to the screen')
This sentence is output to the screen
>>> a = 5
>>> print('The value of a is', a)
The value of a is 5
24
Python Output (Count.)
 Now we can notice that a space was added between the
string and the value of variable a.
 This is by default, but we can change it.
 The actual syntax of the print() function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
 Here, objects is the value(s) to be printed.
 sep is the separator used between the values.
 It defaults into a space character.
25
Python Output (Count.)
 end is printed after printing all the values.
 It defaults into a new line. file is the object where the values are
printed and its default value is sys.stdout (screen).
 Here are an example to illustrate this.
print(1,2,3,4)
print(1,2,3,4,sep='*')
print(1,2,3,4,sep='#',end='&')
Output: 1234
1*2*3*4
1#2#3#4&
26
Python Input
 In Python, we have the input() function to allow this.
 The syntax for input() is:
input([prompt])
 Where prompt is the string we wish to display on the screen. It
is optional.
>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'
27
Python Input (Cont.)
 We can see that the entered value 10 is a string, not a
number.
 To convert this into a number we can use int() or float()
functions.
>>> int('10')
10
>>> float('10')
10.0
28
Python Import
 it is a good to break it into different modules
 A module is a file containing Python definitions and statements
 Python modules have a filename and end with the extension .py
 Definitions inside a module can be imported to another module
 For example, we can import the math module by typing in import
math.
>>> import math
>>> math.pi
3.141592653589793
29
Python Import (cont.)
 Now all the definitions inside math module are available in
our scope.
 We can also import some specific attributes and functions
only, using the from keyword. For example:
>>> from math import pi
>>> pi
3.141592653589793
 While importing a module, Python looks at several places
defined in sys.path. It is a list of directory locations. (Example on
Next slide)
30
Python Import (cont.)

>>> import sys


>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-
packages', '/usr/lib/python2.7/dist-packages',
'/usr/lib/pymodules/python2.7']
31
Python Operators - Arithmetic
Operator Meaning Example
x+y
+ Add two operands or unary plus
+2
x-y
- Subtract right operand from the left or unary minus
-2

* Multiply two operands x*y


Divide left operand by the right one (always results into
/ float)
x/y

Modulus - remainder of the division of left operand by x % y (remainder of


% the right x/y)
Floor division - division that results into whole number
// adjusted to the left in the number line
x // y

** Exponent - left operand raised to the power of right x**y (x to the power y)
32
Python Operators – Arithmetic (Cont.)

x = 15
y=4
print('x + y = ',x+y) x + y = 19
print('x - y = ',x-y) x - y = 11
print('x * y = ',x*y) x * y = 60
print('x / y = ',x/y) x / y = 3.75
print('x // y = ',x//y) x // y = 3
print('x ** y = ',x**y) x ** y = 50625
33
Python Operators - Logical
Logical operators in Python

Operator Meaning Example

and True if both the operands are true x and y

or True if either of the operands is true x or y

True if operand is false (complements the


not not x
operand)
34
Python Operators – Logical (Count.)

x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
35
Keywords
 Keywords are the reserved words in Python.
 They cannot be used as variable name, function name or
any other identifier.
 They are used to define the syntax and structure of the
Python language.
 In Python, keywords are case sensitive.
 There are 33 keywords in Python, on next slide
 All the keywords except True, False and None are in
lowercase and they must be written as it is.
36
Keywords (Cont.)

False class finally is return


None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
37
Things to care about !!!!

 Python is a case-sensitive language.


 Variable and variable are not the same.
 Always name identifiers that make sense.
 c = 10 is valid. Writing count = 10 would be easier to figure
out.
 Multiple words can be separated using an underscore,
this_is_a_long_variable.
38
Things To Care About !!!! (Count.)

 We can also use camel-case style of writing:


Capitalize every first letter of the word except the initial word
without any spaces.
 For example: CamelCaseExample
39

Interfacing using Python


40
Recalling GPIOs
41
Recalling Wiring Diagram
42
Simple Blink Python Code - Steps
 $ nano blink.py
 Add the codes lines (next slide) in the file
 Ctrl + O and enter to save
 Ctrl + X to exit text editor
 $ sudo python blink.py
 Ctrl + C to break
43
Simple Blink Python Code
References 44
1. https://www.python.org
2. https://www.raspberrypi.org/
3. https://en.wikipedia.org/wiki/Python_(programming_language)#cite_note-AutoNT-34-20
4. http://www.tiobe.com/index.php/paperinfo/tpci/Python.html
5. http://www.programiz.com/
6. https://en.wikipedia.org/wiki/Raspberry_Pi
7. https://learn.sparkfun.com/tutorials/raspberry-gpio
8. http://raspi.tv/2013/how-to-use-soft-pwm-in-rpi-gpio-pt-2-led-dimming-and-motor-speed-control
9. http://razzpisampler.oreilly.com/ch07.html
45

The End

Вам также может понравиться