SlideShare uma empresa Scribd logo
1 de 15
Baixar para ler offline
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
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 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
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

Mais conteúdo relacionado

Mais procurados

20150613 self-puppet v4-avoiding_dragons
20150613 self-puppet v4-avoiding_dragons20150613 self-puppet v4-avoiding_dragons
20150613 self-puppet v4-avoiding_dragons
garrett honeycutt
 

Mais procurados (6)

Infrastructure as Data - PuppetConf 2013
Infrastructure as Data - PuppetConf 2013Infrastructure as Data - PuppetConf 2013
Infrastructure as Data - PuppetConf 2013
 
What Shazam doesn't want you to know
What Shazam doesn't want you to knowWhat Shazam doesn't want you to know
What Shazam doesn't want you to know
 
20150613 self-puppet v4-avoiding_dragons
20150613 self-puppet v4-avoiding_dragons20150613 self-puppet v4-avoiding_dragons
20150613 self-puppet v4-avoiding_dragons
 
Como programar melhor jogando game boy
Como programar melhor jogando game boyComo programar melhor jogando game boy
Como programar melhor jogando game boy
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterations
 

Semelhante a 3.Pi for Python

Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
Go Asgard
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
mckennadglyn
 
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
it-people
 

Semelhante a 3.Pi for Python (20)

Python basics
Python basicsPython basics
Python basics
 
PyGame Tutorial | PyGame Python Tutorial For Beginners | Python Certification...
PyGame Tutorial | PyGame Python Tutorial For Beginners | Python Certification...PyGame Tutorial | PyGame Python Tutorial For Beginners | Python Certification...
PyGame Tutorial | PyGame Python Tutorial For Beginners | Python Certification...
 
Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - Kalamassery
 
Introduction to Game programming with PyGame Part 1
Introduction to Game programming with PyGame Part 1Introduction to Game programming with PyGame Part 1
Introduction to Game programming with PyGame Part 1
 
Session 02 python basics
Session 02 python basicsSession 02 python basics
Session 02 python basics
 
Session 02 python basics
Session 02 python basicsSession 02 python basics
Session 02 python basics
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
 
Class 1: Welcome to programming
Class 1: Welcome to programmingClass 1: Welcome to programming
Class 1: Welcome to programming
 
Python week 2 2019 2020 for g10 by eng.osama ghandour
Python week 2 2019 2020 for g10 by eng.osama ghandourPython week 2 2019 2020 for g10 by eng.osama ghandour
Python week 2 2019 2020 for g10 by eng.osama ghandour
 
3. basics of python
3. basics of python3. basics of python
3. basics of python
 
The Ring programming language version 1.5.3 book - Part 39 of 184
The Ring programming language version 1.5.3 book - Part 39 of 184The Ring programming language version 1.5.3 book - Part 39 of 184
The Ring programming language version 1.5.3 book - Part 39 of 184
 
InterConnect: Server Side Swift for Java Developers
InterConnect:  Server Side Swift for Java DevelopersInterConnect:  Server Side Swift for Java Developers
InterConnect: Server Side Swift for Java Developers
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on Python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Programming simple games with a raspberry pi and
Programming simple games with a raspberry pi andProgramming simple games with a raspberry pi and
Programming simple games with a raspberry pi and
 
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
 
Python slide
Python slidePython slide
Python slide
 
5.Playtime
5.Playtime5.Playtime
5.Playtime
 
Lesson1 python an introduction
Lesson1 python an introductionLesson1 python an introduction
Lesson1 python an introduction
 
Magic 8 ball putting it all together
Magic 8 ball  putting it all togetherMagic 8 ball  putting it all together
Magic 8 ball putting it all together
 

Último

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Último (20)

Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 

3.Pi for Python

  • 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. 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. 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. Mayank Joneja Namaste Duniya  Start Python in command line console  print “Namaste Duniya”  # is for single line comments, ‘’’ ‘’’ for multi-line comments
  • 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. Mayank Joneja Strings  a = “Raspberry”  b = “Pi”  c = “Rules”  print a,b,c
  • 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. 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. 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. Mayank Joneja Lists (Mutable arrays) mytext = [“I”, “Love”, “My”, “Raspberry”, “Pi”] for n in range(0,5): print (mytext[n])
  • 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. 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. 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. 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. 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