Programming with Python: Print, Comment
Hello guys, today I am going to try my best to teach you some methods in programming. I am not a professional at this, but a guy who is learning it himself, but I figured by teaching others it would benefit them in some way while being able to me to get a review going on. So how are we going to do this? First, let me tell you what you will need. You’re going to need a text editor. No no, I don’t mean textedit or notepad. Here, you will need these.
(Mac) http://www.barebones.com/products/textwrangler/
(Windows) http://notepad-plus-plus.org/
Now we are going to need the actual programing software itself. This will allow us to work with the python interface while being able to edit the code in the long run. Since we are starting with python, I suggest you download Python 2.7.5, as we will be working with that in this series.
(Python for Windows, Mac, & Linux) http://www.python.org/download/
Last but not least you’re going to need a terminal. In windows it’s your Command prompt. Just click the start button and search for CMD. Run it and find the directory you will be working with. As explained in the video I will be working with a lesson folder dedicated for this series of videos. I suggest you do the same, so in this case we will list our directories. To do this type in “DIR” and “cd desktop” for the desktop directory. This is an easy concept if you understand “cd” is “change directory”.
Now for mac users. Go to the top right on the screen and just search for terminal. Much like CMD you need to be aware of how to use the terminal. ” ls” lists the directories while cd directory (or cd desktop) changes the directory. You might want to search how to use your terminal to get a better understanding on it.
Alright, now that we have everything set up and running, let’s teach you two important tags that we will be using throughout this series. The print “” tag, and the “#” tag. Okay, first open up your text editor and save the blank file as “ex1.py” for exercise 1. .py is nothing more than a file extension. So we understand that it is a python file. Okay, now let’s type in a couple lines of code.
- #This is a comment tag. What ever you write in here is going to be shown only in the text editor and not the terminal itself
- print “This is a print tag, whatever you put in here with open and close quotes will pop up in the terminal.” #See how easy that was?
Okay, now that we got that out of the way, let’s do one more thing. Just as an extra bonus let’s teach you how to do math in python. It’s not so bad once you get used to the concept. So lets begin.
Essentially all you have to understand are these symbols.
+- Addition
- - Subtraction
* - Multiplication
/ - Division
So in code all we have to do it
- 5 + 5
And the equation will pop up. However, let’s ask python the question and make him/her answer it.
- print “What is 2 + 2?”, 2 + 2
I would like for you to notice the comma after the code, you have to remember this or else python will just give you a syntax error. This is kind of like a dividing line for the print and equation tag. So all you have to do is run the program.
How do we do this?
Go to your terminal. If you are using windows, navigate to your directory through Command prompt and type the file name in. In this case it is “ex1.py” and it should load.
However, for mac users you have to type in “python ex1.py” I am not 100% sure why that has to be the way it is, but that’s how it works. There you have it! You just made your first terminal program. Nothing fancy, but we will get there along the way. If you want to study ahead, go check out this cool website that will help you out in the long run. It’s also free, but if you pay $29 you can get a pdf copy of the lessons and videos with a better explanation. (For those who don’t feel they can teach themselves).
http://learnpythonthehardway.org/book/ - Learning Python the Hard Way.
In the meantime please get used to the idea of the print tag and the # tag. We will be using these tags through on out. Next time will be looking at functions and variables. So be prepared.
Reader Comments