# Programming with Python: A simple example4 min read

By | February 2, 2018

Python:

Python is a scripting language that is commonly used in scientific computing. Being popular amongst researchers and growing in popularity by the day, it is a very important skill set to have for young people to be successful in their careers. So after that opening note lets get down to programming.

There are many tutorials regarding the basics of Python programming so I won’t be covering that (unless you people ask me to). Also since I have not shown the use of advanced libraries yet, I will try to keep the commands simple.

Objective (Continued from previous post):

For a given equation:

write a short program that calculates the values of force for given values of “x”.

Lines starting with “#” are comments and do nothing.

```# Generate a list of values (same in the table) for which to calculate Force
# The square brackets allow multiple numbers to be assigned to a single
# variable as a list or array.
x = [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
# List the value of k (the spring constant)
k = 0.5
# Using given equation:
# F = kx
# Since there are a lot of values of x which need to
# be multiplied we can use a for loop. For loops are
# used when the same operation has to be done many
# times.
for i in x:
# The command for i in x will create a variable “i”
# which will go over each value in “x”. So in this case
# the for loop will run 9 times as there are 9 values in &nbsp;“x”
# And each time the loop runs “i” will take on one value in order from “x”.
F = k * i
print i,'\t',F

```

The last command print i,’\t’,F  will print the value of “i” and “F” in each iteration of the loop with a tab-space between them. The above code will display a list of values when you will run it. This brings us to an important point. How do you run Python code?

In scientific circles, Linux is a popular operating system. In Linux you can access Python just by going to your terminal and typing Python (all pieces of code are intended to be run on Python 2.7 unless stated otherwise).

In case you are on Windows you will need to install an environment for Python. There are many flavours out there e.g. Anaconda, IDLE which you can use in Eclipse and .

I know it can be a bit confusing to decide what to install so for those of you weak of heart I would recommend using the Jupyter notebook. Click on the link and visit it. In your top right corner you will see “New”, click it and select Python 3 from the drop-down list and then paste the code in the box that appears and press Run in the top panel. (Note that if you close the session you will have to close your internet browser and then restart to avoid any errors).

Note that Python 2.7 and Python 3 are sometimes very different depending on what you are doing. While I will provide code in Python 2.7 I will tell you what thing will be different when doing the same in Python 3. E.g. the above code without comments is:

Python 2.7

```x= [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
k = 0.5
for i  in x:
F = k * i
print i,'\t',F
```

Python 3.x

```x= [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
k = 0.5
for i  in x:
F = k * i
print(i,'\t',F)
```

This concludes our very basic task-oriented first programming lesson. At present it may feel that this wasn’t hard, or pointless but with for loops you can do something millions of times (don’t do that, which in theory it works but in practices it depends on what you are doing inside the for loop and if your computer is able to handle all that raw loop-power).

I intend in this series of posts to bring readers to a point where they can at least (coupled with the simulation series of posts) generate their own models, run simulations and do it with their own code.

Please like share and subscribe IDRACK’s Facebook page and group. Talk to your friends about us and as said before if you have any questions and problems following all this, please feel free to get in touch, either in comments below or Facebook or through details on our Contact Us page.

## One thought on “Programming with Python: A simple example4 min read”

1. Pingback: Python: A Monte Carlo simualtion to calculate Pi