Raspberry Pi Linux LESSON 24: Running Python on the Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter with top tech boy comm and I am here with lesson number 24 on using your Raspberry Pi micro controller if you've been here for the first lessons we've gone through quite a bit of information on Linux you should be getting pretty comfortable with the Linux environment now and comfortable operating from that command line in that terminal window and so if you've gone through these first 23 lessons you're not like a networking expert I wasn't really trying to take you through all the painful depths of the Linux system but just get it to where you can kind of operate you can kind of do the things you want find things change things edit things move things set up users you know find your way around so for you know I'm kind of designing these for high school students or college students and kind of at that level you're sort of able to find your way around and so we're to the point now that we want to kind of start moving towards actually doing something and so what we're going to look at today is running Python how can we run Python on the Raspberry Pi I'm not going to really be teaching you Python programming if you want to learn that you can go back I did a series on using Arduino with Python and there's quite a bit of Python on there and you know that's probably the best place to start so I'm not going to try to go in and really teach you Python but just kind of how do we get the Python running on the PI the good news is and the default installation of the Raspberry Pi operating system excuse me has Python on it so all we got to do is just kind of find it and run it so let's start how would we write a Python program well we don't really have the fancy visual editors here operating in the command line so how do we edit a file we do Nano but let's kind of take a second and see where we are let's see we are in PWD we are in the slash home slash pi and i'm logged in as pi and so we probably should make a directory so make make directory let's go reference from our present location home and let's call it my files I'm going to collect my Python okay so they'll be a good place to put our Python files let's do an LS now we got desktop my Python and python games we're in my Python so how do we create a program well how would we edit a file we would do Nano okay and then let's just call it hello I put I gotta tell it where so let's go down to my PI my Python that's the folder no leading no leading slash because you're referencing from the folder we're in we're going down one folder to my Python hey and then we're going to call it hello the file is going to be hello.txt all right so now we reading the file so I'm going to say print and then I'm going to say so this is going to be my Python program ooh oh no no no no control X control X okay I shouldn't have called it dot txt let's look LS and LS my Python okay let's remove my Python slash hello.txt LS my Python okay nothing in there okay what I should have done is say nano my Python and then go hello dot python now it would have run you could have run it with a dot txt extension the extension doesn't really do anything but it's good practice to name your Python file spy that way if you want to go and say find all my Python file files you can say find on slash space minus name and then star dot PI and you can find everything with the pop top PI extension and so let's be good about being careful and neat with our extensions and so I got a little a little careless there so let's do that now we are editing a Python file in the folder my Python called hello pi and I'm going to say print and I'm going to say hello Texas okay I bet you thought I was going to say hello world but I said hello Texas I guess that might be a little bit rude since there might be people not from Texas and they would feel left out but somehow I want to get Texas into this so how about this Texas says howdy okay so that I get the Texas in but I don't leave anybody out so how do I exit out why do control o and that says file name to write my path on yes so I just click enter and then I control X to get out now do it LS my Python and boom there it is hello now can we run that this is the way you run a Python program on the PI you just type Python it'll find it it knows where it is as its installed it will know where Python is so you don't have to go and input path names or anything there but you do have to give the path name to the file that you want to run so if I wanted to start with the route I could say slash home slash PI slash my Python slash hello py okay let's see if it works look at the looky there it says Texas says howdy okay so we just ran a Python program pretty exciting isn't it but you're maybe wondering what version of Python are we running here well let's make a new file I'm going to CD down into there so I don't have to keep putting the paths in there so CD my Python LS okay I'm in it so now let's do a nano and this time let's say which PI that's going to be the program I'm going to start by saying import the system library and then I'm going to say print sis top version okay and then I'm going to ctrl o to save it enter and control now I'm going to say Python which dot pi look at that we are running we're just putting in the command Python it is running Python 2.7 point three and that's really a great version of Python the big controversy all the versions are kind of 2.7 or 2.6 and later all the sort of 2's are kind of the same and then when they went to python version 3 the latest and greatest there were some real changes the thing is there are millions and millions of libraries that were written in the sort of python vintage version too and some of those things are not really getting updated for version 3 maybe somebody made a smack-dab great job on a library for for version 2 but they got on to other things in their life and they didn't really ever go in and update the library for for version 3 and so what that means is is that as we're taking advantage of this huge legacy library base that exists for it for to a lot of those don't work if you go to 3 when I look at version 3 and I look at version 2 for me just being the simple guy I am I don't see a lot of reason for going to 3 I sort of give up things that I might really need and I don't really gain anything so I like staying with version not with version 2 ok but actually on the Raspberry Pi you can run different versions if you just save Python it's going to run two point seven point three but what if I say Python two until it specifically to run two and then which dot pi okay it's going to run two point seven point three but I told it specifically to run two what if I tell it specifically to run three and run which dot PI so I taught try to run version three if I do that it tries to run but boom I get an error why because print statements are different in Python three than they are in Python 2 so if this is going to run on three I'm going to have do a nano which PI and I'm going to have to make this version three friendly which means I need to come down and this needs to be in like a I guess this would be like a function you know you would have to do that alright and so now we're going to ctrl o enter ctrl X Nano which dot PI or I'm sorry we're going to do a Python 3 and which dot pi and look at that it says that I'm running three point two point three so when I say python 3 it runs three point two point three I bet I could say Python three point two and which PI and yeah it takes that as Python three point three point two point three I wonder if I could do Python three point one I wonder if that's on there I don't really know I'm just kind of playing around okay no it's not in so I can run Python three point two or I can run Python 2.7 I just wonder Python on two point six whether that's off which high okay command not found how about just Python again uh to which top pie look at that with that other print statement I guess that way it'll work in two point seven or three where you put the print inside the inside the parentheses so anyway what you can see here is now that you can suit you can nano program dot pi and now you can go in and you can just start doing all the things that you know how to do in Python let's say X is equal to one and then I can say while one equal one like that and then I cab over and I can say print X and then I can say X is equal to X plus one okay and I don't want this to go forever so while let's see why don't we say while X is less than 100 do this okay control o enter control X we're going to do Python I forgot something okay yeah that works control X okay so now we're going to go Python we're going to go program dot PI and boom okay look at that that thing counted to 100 before or 299 before we could even blink okay I can't help it nano pro program dot pi okay I'm going to go to a thousand I keep trying to use the mouse in the terminal window and you can't go to a thousand and also I'm going to say less than or equal to so that'll actually get that last point so we're going to say ctrl o ctrl X or ctrl o enter ctrl X and now we're going to go to Python this will be version 2 because I'm not putting anything in there dot hi and then look at that it printed to a thousand faster than we could even even see it and it went all the way to the thousand that time okay so like I say go back to my Arduino with python lessons top tech boy comm you can find the lessons there if you need to learn more about Python but kind of with what I have you going on here you're ready to sit down and start running Python in fact if you go back to those lessons where I showed you how to use our dueƱo with python what you could see that you could do know is is that you could get your arduino and you could actually run it from the raspberry pi right you could taught you could plug the the arduino into the Raspberry Pi and you could have Python you can have the PI working with the Arduino via Python because I've already shown you and those Arduino with pozzolana lessons how to communicate between the arduino and python now showing you how to run Python on the Raspberry Pi and so now you could make a little unit where you had the Raspberry Pi in the Python the Raspberry Pi and the Arduino working and talking together and doing a project because they could talk back and forth to each other because you can run Python on the Raspberry Pi hopefully that makes sense what I'm saying but go back and look at those earlier lessons in this series of lessons I'm not going to go in and actually teach Python go back and look at those earlier lessons what I would like to do is I would like to move ahead and sort of we've gotten the kind of overhead the bookkeeping out of the way of how to get the PI running how to find your way around with Linux you just got to know that and that's good stuff to know anyway now I'd like to start seeing can we maybe start building things kind of doing things as we start working with these pins remember in the Arduino how we could write and read from pins well can we now sort of start writing programs where we're running on the PI but we're starting to kind of kind of work more at the hardware level okay palma quarter at top tech boy comm thanks for tuning in if you guys like this give me a thumbs up think about subscribing to the channel maybe share this with other people want to get more people in the PI learning this high tech stuff it's good stuff talk to you guys later
Info
Channel: Paul McWhorter
Views: 46,661
Rating: undefined out of 5
Keywords: Raspberry Pi (Computer), Python (Programming Language), Linux, STEM, Tutorial, Maker
Id: AfV9c5kIj-k
Channel Id: undefined
Length: 14min 32sec (872 seconds)
Published: Fri May 29 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.