week 2 - mfa - 9.10.15
TRANSCRIPT
![Page 2: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/2.jpg)
● Intro to Python
![Page 3: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/3.jpg)
History of Pythonhttps://docs.python.org/2/tutorial/introduction.html#
● "Python is an interpreted, interactive, object-oriented programming language".
● Python is similar in some ways to Perl and Java. Knowing Perl or Java will help you learn Python, but it's just as easy to pick it up as a language. Python is great for both shell and web scripting and is making it's way into every facet of computing from APIs to video and interactivity.
● Guido van Rossum first created Python in the late eighties and early nineties. It is now maintained by a core development team.
![Page 4: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/4.jpg)
Why Python
Python is Efficient : Iterative processes, good with lists, memory management
Python is Fast : Even if interpretive language can add decorators to speed up. Increasing in performance
Python is Broad : Systems automation, testing, gaming, CGI, and web development. Many modules
Python is Easy : Readable syntax, easy for non-programmer types, plays well with other languages
Python is Fun : Broaden your programing base, show off, reason differently
![Page 5: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/5.jpg)
Basic Python math/stringsOn Laptop Terminal type - $ python
>>> 2+2 4>>> (5-20*2)/4 -9>>> 7**2 49
>>> width = 20>>> height = 5 * 9>>> width * height 900
>>> s="hot">>> p="dog">> print s+p hotdog
>>> j = "hotdog wa oishii desu">>> print j[10:16] oishii>>> len(j) 21
![Page 6: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/6.jpg)
Basic Python list/while
>>> s=[1,4,5,6]>>> s[1]=20>>> print s [1, 20, 5, 6]
>>> a, b = 0, 1>>> while b < 10:
… print b … a, b = b, a+b … 1.1.2.3.5.8
>>> a, b = 0, 5>>> while b<1000: … print b … a,b = b, a+b 5.5.10.15.25…
>>> import time>>> while True: … time.sleep(2) … print "sleeping" sleeping...
![Page 7: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/7.jpg)
Basic Python if/for
>>> x=int(raw_input(“#?”))>>> if x < 0:
… print “Neg Number” elif x == 0: … print “Number is 0” elif x == 1: … print “Number is 1” else: … print “Other”
>>> words = [‘neko’,’inu’,’ratto’,’sakana’]>>> for w in words: … print w, len (w) >>> for i in range (5,100,2):
… print i
>>> words = [‘ringo’,’nashi’,’hana,’ki’]>>> for i in range (len(words)):
… print i, words[i]
![Page 8: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/8.jpg)
Basic Python functions
>>> def fib(n): #fibonacci function… a, b =0,1… while a<n:… print a… a,b=b,a+b
>>> fib(2000)
>>> def greeting(n): #hello function… name = raw_input(“enter name ”)… print “hello ”+name+”you are ” + n
![Page 9: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/9.jpg)
Script - ip-email.pyRaspberry Pi
ssh and log into pi$ cd Desktop$ touch ip-email.py$ sudo nano ip-email.pypaste contents of “ip-email.py” into terminal$ sudo nano /etc/rc.localwrite path of script before “exit 0”$ sudo python /home/pi/Desktop/ip-email.py
Laptop Terminal
$ cd /home/path/toscript$ touch ip-email.py$ sudo nano ip-email.pypaste contents of script into terminal$ sudo python ip-email.py
![Page 10: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/10.jpg)
Script - email-read.py, email-notify.py
![Page 11: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/11.jpg)
Python update/GPIO
Install latest python 2.7 and python pip$ sudo apt-get install python-pip python2.7-dev
Update distribution$ sudo easy_install -U distribute
Install feedparser$ sudo pip install feedparser
![Page 12: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/12.jpg)
● GPIO
![Page 13: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/13.jpg)
GPIO
GPIO.BCM
http://elinux.org/RPi_Low-level_peripherals#GPIO_Code_examples
GPIO.BOARD
![Page 14: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/14.jpg)
Digital Output - gpio-blink.py
● BOARD / BCM● Resistors
![Page 15: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/15.jpg)
Digital Input - gpio-buttons.py
● Resistors● Debounce● Pullup● Pulldown● Digital in only
![Page 16: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/16.jpg)
Analog Input - gpio-rc.py
https://arduinodiy.wordpress.com/2013/10/19/793/
● ADC● RC analog input
http://www.raspberrypi-spy.co.uk/2013/10/analogue-sensors-on-the-raspberry-pi-using-an-mcp3008/
![Page 17: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/17.jpg)
Analog Output - gpio-angout.py
● PWM● Fading LED● Servo
![Page 18: Week 2 - MFA - 9.10.15](https://reader033.vdocument.in/reader033/viewer/2022042608/563db98d550346aa9a9e6d59/html5/thumbnails/18.jpg)
Assignment●