Basic Python Syntax



As a result of this lesson the learner will be able to

    1. Create a project folder
    2. Create a basic python program
    3. Utilize basic python syntax
    4. Use basic operations
    5. Create variables and assignments
    6. Identify numeric data types
    7. Analyze mathematical model using python code

Files for this lesson

    1. Download Python File


How can we use a computer program to help us evaluate $y(t)=v_{0}t-\frac{1}{2}gt^{2}$ over an interval of time if we are given various initial velocities. This formula is used to describe the vertical motion of a ball as it is thrown up into the air.


Open up Aptana and create a new project and then choose a PyDev Project since we will be working with python for this tutorial. Make sure to name the project something you will remember. In this example I named it "bnf_tutorial". Then you have to choose the grammar syntax (3.0) and the interpreter we created in the previous lesson.

This is how you will create new python projects to work on and create projects for different programming languages. Projects will simply be a top level folder that will hold all the files that our code is in. We will continue to organize the folder as we proceed though the tutorial. When we create a new project, we are also creating a folder that is in the workspace we selected earlier. You can also access the information by navigating to the workspace location within any windows explorer and see your files there.

Now right click on the new project that you just created and create a new PyDev Module. Name the module "vertical_motion" and select "Module:Main from the template box. This will be our starting point of creating a python program to solve the mathematical model above. You will now see a .py file appear in your project folder and it will have code inside it that looks something like this

Created on Nov 26, 2013
@author: Apodaca
if __name__ == '__main__':

These are images of what you should see

Many of the examples I will be working with will have the if __name__ == "__main__": main() in the program. This is not needed to have our programs run but it simply means that we are able to have our program run as a module and a stand alone program itself. If you wish to understand the topic now you can review this page on modules. Whenever python runs a .py file it assigns values to certain special variables. All this does is check if the program is being run as a main program or if it is being imported into another program. Don't worry too much about all this just yet. For now make sure the code in your file has this in it now. I have added the main function and the print statement to the program. Also notice the '#' next to main(). Everything on the line that follows a # sign will be ignored by the program. This is called a comment and is used just as helpful information for the person coding the program and those that may review it later. It is ALWAYS good technique to add comments to your programs to document what the program should be doing and as a reminder.

def main():
    print("Hello World")
if  __name__ == "__main__":
    main() #Allows functions to be run in any order

once you have the code entered, you can go to Run and choose Python Run to get the program to run. You may be asked to save changes first and you should see "Hello World" printed in the console below. So far so good!!

Lets take a second look at what we have so far in our program.

This part of the code is not exactly code. It is here to give information about the date the code was created and who created it. Later we will look at how to edit this but for now we will leave it the way it is by default.

Created on Nov 26, 2013
@author: Apodaca

This part of the code we already looked at briefly. What this is going to do for us is give an entry point for our code. This way, every program we create will start with the main function. What ever we wish to run will be under our main function.

if  __name__ == "__main__":
    main() #provides entry point and running functions in any order

This part of the code is called a function. Functions start with "def" followed by a space and the name of the function. After you name the function you will put "():". This is how every function is created. You state in the program that you are defining a function with def and give it a name. Now this is where python is a bit different from other languages. Other languages will follow def main(): with curly braces {}. Python decides to have it's language use white spaces to define its block or suite of code. When you define a function you create a block of instructions under it. The block will be defined by four blank spaces and everything that is enclosed in the function should be spaced the same amount under the def main():. In our example we only have the print statement.

def main():
    print("Hello World")
Now we are finally at a point to actually create some code. This main block that we have set up will be how many of our programs are going to be run. Take a look at some of these common terms and become familiar with them as we move forward. I will try to build or refresh our programming vocabulary as we progress through the lessons. These first few hurdles will be a bit slow if you are new but hang in there!!


Lets review what we have learned so far and keep in mind of where we are going.


  • Create a project folder
  • Create a basic python program
  • Utilize basic python syntax

We will still continue to learn more about python syntax as we move forward as well. But remember the following list of items that still needs to be learned. It will take all of these items to evaluate a mathematical model such as the one described at the start of this page.


  • Use basic operations
  • Create variables and assignments
  • Identify numeric data types
  • Analyze mathematical model using python code

When we start our program the first thing we will do is initiate all of our variables. We can do this by assigning them an exact known value or a place holder value to be changed later within the program. So lets start by defining some of the variables in the equation we are trying to evaluate.

The equation $y(t)=v_{0}t-\frac{1}{2}gt^{2}$ has the variables $y\;v_{0}\;t\;and\;g$. When we introduce variables into our program it is useful to give them descriptive names that are close to the mathematical problem we are going to solve. This provides readability to the program. A variable name can contain any lower or upper case letters, the numbers 0-9, and an underscore. A variable may not start with a number. The point of creating variables is so that we are easily able to keep track of them and quickly modify them in one location. For example, we would have to change the value of t two places within the equation if we did not use variables.

Go ahead and create your main function like the one listed below.

def main(): # this is the function be declared
	v0 = 5 # this lists all the variables
	t = 1
	g = 9.81
	y = v0*t - .05 *g*t**2
    print(y) # prints the value that y evaluates to

if  __name__ == "__main__":
    main() #Allows functions to be run in any order

Here we created 4 variables. The first three were given a numerical value and the y variable was evaluated from the previous three variables. There are four standard arithmetic operators +, -, *, and /. You can create exponents with a double astrisk **. Now as we move forward in our programs it is important to have the text you create be exact. Creating a variable v0 is different from a variable V0 and even vO. Notice the use of capitalization and a mix of numbers and letters between the variables. Be clear with the variables you create and try not to make them too confusing for others that may use your code. Also, If we were to use the code


the program would not understand it but a human reader may be able to understand that you intended to write an output to be read. The next thing we need to look at are the different numerical values we assigned to our variables. We chose 5, 1, and 9.81. You may think all of these are just numbers but they are actually different numerical types. Numbers can be complex(denoted by j), an integer, long (large integers), or float(a decimal value). In later lessons we will get into this in more detail but the data python stores can also be strings, lists, tuples, or dictionaries. For now we are mainly looking at the different ways to work with numbers.

Lets take one more look at our code we created. On one of the lines we put

	y = v0*t - .05 *g*t**2

and this is called a statement. We are assigning the right hand side of the = sign to the left hand side. This is how we are creating variables. The right hand side is called an expression and it is a combination of values, variables, and operatores. When the expression is evaluated it then is assigned to the left hand side. Make special note of this because this will allow us to write statements like

	y = y + 5

Mathematically this is not true but since we are assigning the right hand side to the left hand side we are able to add 5 to the current value of 5 and then assign it back to y.

A final note on this lesson is about integer division. I'll leave this as an exercise for you to look at. Open up the interactive python that we looked at in our first lesson and try out the following statements to see what they evaluate to.


Review Questions

  1. (9.0/5)*5+32
  2. (9/5)*5+32


Leave a Reply

Your email address will not be published. Required fields are marked *