Pi Maker
Workshop
Powered by:
http://www.inventrom.com/http://www.robotechlabs.com/
Mayank Joneja
botmayank.wordpress.com
...
Mayank Joneja
Python Basics
 The “Pi” in Raspberry Pi stands for Python, a popular
high level language
 Start installing...
Mayank Joneja
The Zen of Python
 The core philosophy of the
language is summarized by the
document “PEP 20 –The Zen of
Py...
Mayank Joneja
Namaste Duniya
 Start Python in command line console
 print “Namaste Duniya”
 # is for single line commen...
Mayank Joneja
Arithmetic and Algebra
 print 2+2
 print “two plus two equals”, 2+2
 print 10/2
 print 2*4
 print 4-2
...
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...
Mayank Joneja
Lets get loopy
#While Loop
n = 0
while (n<=10):
print n
n+=1 #or n = n+1
#For Loop
for n in range (0,10):
pr...
Mayank Joneja
Random
import random
print (random.randint(10,40))
import random
for n in range (1,6+1):
print(random.randin...
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 inp...
Mayank Joneja
Line Break
test = raw_input(“Input word”)
print “Why “ + test + “ there a line break every time?”
#add a new...
Mayank Joneja
Illustrate input and print
applicant = raw_input(“Enter applicant’s name”)
supervisor = raw_input(“Enter sup...
Mayank Joneja
Function definition
def happyBirthdaySalman():
print “Happy Birthday dear Salman!”
def happyBirtthdayAishwar...
Mayank Joneja
Running Python on the Pi
 We’ll use Nano quite often for editing files on the Pi. Such a command line tool ...
Upcoming SlideShare
Loading in …5
×

3.Pi for Python

641 views

Published on

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 :)

Published in: Engineering
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
641
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

3.Pi for Python

  1. 1. Pi Maker Workshop Powered by: http://www.inventrom.com/http://www.robotechlabs.com/ Mayank Joneja botmayank.wordpress.com botmayank@gmail.com 3.Pi for Python
  2. 2. 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
  3. 3. 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
  4. 4. Mayank Joneja Namaste Duniya  Start Python in command line console  print “Namaste Duniya”  # is for single line comments, ‘’’ ‘’’ for multi-line comments
  5. 5. 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
  6. 6. Mayank Joneja Strings  a = “Raspberry”  b = “Pi”  c = “Rules”  print a,b,c
  7. 7. 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’
  8. 8. Mayank Joneja Lets get loopy #While Loop n = 0 while (n<=10): print n n+=1 #or n = n+1 #For Loop for 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
  9. 9. Mayank Joneja Random import random print (random.randint(10,40)) import random for n in range (1,6+1): print(random.randint(10,40)
  10. 10. Mayank Joneja Lists (Mutable arrays) mytext = [“I”, “Love”, “My”, “Raspberry”, “Pi”] for n in range(0,5): print (mytext[n])
  11. 11. 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
  12. 12. 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?”
  13. 13. 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
  14. 14. 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’)
  15. 15. 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

×