SlideShare a Scribd company logo
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

More Related Content

What's hot

Infrastructure as Data - PuppetConf 2013
Infrastructure as Data - PuppetConf 2013Infrastructure as Data - PuppetConf 2013
Infrastructure as Data - PuppetConf 2013
Puppet
 
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
Roy van Rijn
 
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
 
Como programar melhor jogando game boy
Como programar melhor jogando game boyComo programar melhor jogando game boy
Como programar melhor jogando game boy
Gabriel Rodrigues Couto
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
karan saini
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterations
karan saini
 

What's hot (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
 

Similar to 3.Pi for Python

Python basics
Python basicsPython 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...
Edureka!
 
Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - Kalamassery
SHAMJITH KM
 
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
Abhishek Mishra
 
Session 02 python basics
Session 02 python basicsSession 02 python basics
Session 02 python basics
Sara-Jayne Terp
 
Session 02 python basics
Session 02 python basicsSession 02 python basics
Session 02 python basics
bodaceacat
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
Go Asgard
 
Class 1: Welcome to programming
Class 1: Welcome to programmingClass 1: Welcome to programming
Class 1: Welcome to programming
Marc Gouw
 
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
Osama Ghandour Geris
 
3. basics of python
3. basics of python3. basics of python
3. basics of python
Vishnu Vardhan
 
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
Mahmoud Samir Fayed
 
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
Chris Bailey
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on Python
Sumit Raj
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
mckennadglyn
 
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
Kellyn Pot'Vin-Gorman
 
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
it-people
 
Python slide
Python slidePython slide
5.Playtime
5.Playtime5.Playtime
5.Playtime
Mayank Joneja
 
Lesson1 python an introduction
Lesson1 python an introductionLesson1 python an introduction
Lesson1 python an introduction
Arulalan T
 
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
geekinlibrariansclothing
 

Similar to 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
 

Recently uploaded

ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
zwunae
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 

Recently uploaded (20)

ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 

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