View
101
Download
1
Category
Tags:
Preview:
DESCRIPTION
This is a part of the slide set used at the MakerSpace Noida (India) launch event, Pi Maker Workshop. This slide set is designed to help people get started with the Raspberry Pi and also serves as a collection of innovative projects and some core basic concepts that can aid anybody with their first few steps into the world of DIY electronics or maybe serve as a refresher for the experienced. Feel free to refer and share but please don't alter the watermarks :)
Citation preview
Pi Maker
Workshop
Powered by:
http://www.inventrom.com/http://www.robotechlabs.com/
Mayank Jonejabotmayank.wordpress.com
botmayank@gmail.com
3.Pi for Python
Mayank Joneja
Python Basics
The “Pi” in Raspberry Pi stands for Python, a popular
high level language
Start installing Python on your PC
If you know C then this is a much easier and intuitive
language
Most of the codes we write here are in Python
The only reason to perhaps use C/C++ with regards to
Raspberry Pi based applications could be for running
code faster as the kernel and assembler all are in C
However Python in most of the cases is good enough
for reducing the length of code by a factor of 10 or
more
Mayank Joneja
The Zen of Python
The core philosophy of the
language is summarized by the
document “PEP 20 –The Zen of
Python”
https://www.youtube.com/watch?
v=tKTZoB2Vjuk (Nick Parlante
Google Developer Python Classes)
www.codecademy.com
Mayank Joneja
Namaste Duniya
Start Python in command line console
print “Namaste Duniya”
# is for single line comments, ‘’’ ‘’’ for multi-line comments
Mayank Joneja
Arithmetic and Algebra
print 2+2
print “two plus two equals”, 2+2
print 10/2
print 2*4
print 4-2
22/7 (default int)
22.0/7
a = 2
b = 3
C = 6
print a+b+C
Mayank Joneja
Strings
a = “Raspberry”
b = “Pi”
c = “Rules”
print a,b,c
Mayank Joneja
Conditionals
We’ll be dealing a lot with these when we start with GPIO as well
temperature = float(input(‘What is the temperature in deg C ?’))
if temperature > 35:
print (‘Wear Shorts!’)
else:
print (‘Wear trousers’)
print ‘Get some exercise outside’
Mayank Joneja
Lets get loopy
#While Loop
n = 0
while (n<=10):
print n
n+=1 #or n = n+1
#For Loopfor n in range (0,10):
print n
for n in range (0,10+1)
print n
for n in range (0,10,2):
print n
for n in range (10,0,-1):
print n
Mayank Joneja
Random
import random
print (random.randint(10,40))
import random
for n in range (1,6+1):
print(random.randint(10,40)
Mayank Joneja
Lists (Mutable arrays)
mytext = [“I”, “Love”, “My”, “Raspberry”, “Pi”]
for n in range(0,5):
print (mytext[n])
Mayank Joneja
Input and output
var = raw_input(“Enter something: ”)
print “you entered ”, var
#raw_input() reads every input as a string
#then its up to the user to process the string
str1 = raw_input(“Enter anything”)
print “raw_input = ” , str1
#input() actually uses raw_input() and then #tries to convert the input data to a number using eval()
x = input(“Enter a number”)
print “input= “, x
Mayank Joneja
Line Break
test = raw_input(“Input word”)
print “Why “ + test + “ there a line break every time?”
#add a newline to the end for testing
test = test + ‘/n’
#simple way to remove a trailing newline
if ‘\n’ in test:
test = test[:-1]
print “Why” + test + “ there a line break every time?”
Mayank Joneja
Illustrate input and print
applicant = raw_input(“Enter applicant’s name”)
supervisor = raw_input(“Enter supervisor’s name”)
time = input(“Enter time”)
print “interview will be at”, time, ”by”, supervisor, “for”, applicant
Mayank Joneja
Function definition
def happyBirthdaySalman():
print “Happy Birthday dear Salman!”
def happyBirtthdayAishwarya():
print “Happy Birthday dear Aishwarya!”
happyBirthdaySalman()
happyBirthdayAishwarya()
def happyBirthday(person):
print “Happy Birthday “ + person + “.”
happyBirthday(‘Salman’)
happyBirthday(‘Aishwarya’)
Mayank Joneja
Running Python on the Pi
We’ll use Nano quite often for editing files on the Pi. Such a command line tool (vim, nano)
is used to edit the files that can’t be accessed in r/w mode through Samba, for e.g. some
.conf files or other system files
Make sure that you don’t use a module name as the name for your script. eg. random.py
We’ll be using sudo to launch the editor for such files in order to run them as root user
But for most scripts, we can use any text editor on our laptop, (I prefer to use Sublime Text)
and then transfer the files on to the Pi in our Project directory through Samba
Then in order to run the script:
sudo python filename.py
Recommended