Introduction To Python Tutorial and How To Make Python Scripts
Introduction To Python Tutorial and How To Make Python Scripts
Shell: It is a program with text only interface for Linux and other Unix like operating systems
Command line: The space to the right of the command prompt is called the Command line. From the command line
users can enter commands.
Python is widely used and is the number one tool used in Astronomy
There are lots of programs and Libraries written for python which are called packages
Once youve opened the terminal utility once, you can alternatively simply search in the spotlight finder (upper right hand
corner magnifying glass) for terminal to skip a few steps. Once terminal is open, you can open python by simply typing
python in terminals command line. Once this is done you’re terminal shell should look something like this:
You have
now accessed python!
Page 1
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
There are two major versions of python: Version 2.7 and Version 3.x (You choose any version you want but please note
that the latest version of python uses slightly different syntax than the previous versions. Also note that astronomy software
may not work on Version 3; for the time being it is advisable to use Version 2.7)
On Macs, a basic version of Python is already installed. Basic Python doesnt include many software packages, so we
have to install different python packages and then import them into python to enhance its capabilities. For the type of
programming you will be doing in Astronomy, I recommend downloading Astroconda which is a free python package which
has many important packages like numpy for example. To download Astroconda go to:
What Is IPython?
IPython is an interactive shell built within python. Ipython can do a lot more than the standard python prompt and does
take the standard python commands.You can run a python program in IPython by simply typing IPython and the filename
in terminals command line. Ipython is great tool for helping you debug your script and it will tell you where the problems
are in your script.
Page 2
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
Start IPython:
Step 1: open terminal
Step 2: in the command line enter: IPython and the following image should appear
In python, user defined functions are made with the keyword def followed by the name you want to give your function
in parenthesis. Outside the parenthesis should be colon (:).
The arguments of your function should be listed in the parenthesis. Any equations you wish to add to your function
Page 3
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
should be indented and entered in the command line under your function at the end of your function enter the command
return which exits the function.
Example 1:
Example 2: In the Example below I created a function called printhis” and then called the function to execute it.
Page 4
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
Example 2:
Page 5
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
As explained earlier, the expression after the if statement is called the condition. If it is true, then all the indented statements
get executed. What happens if the condition is false, and work is not equal to ’boring’ ? In a simple if statement like this,
nothing happens, and the program continues on to the next statement.
If else statement:
The if else statement is useful when you want one action to happen when a statement is true and something different to
happen if the statement is false.
Example:
for loops:
A for loop is a control flow statement that defines iteration, it allows code to be carried out many times. Every programming
language uses for loops (the syntax will vary with the programming language). The general form a for loop is:
for variable in sequence:
statements
Example
Please note: There are also while loops which I do not talk about in this tutorial. If you would like more information on
while loops please refer to pythons official documentation:
Page 6
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
Example:
Using the editor macvim, I created a file called practice.py and saved it to my desktop. In my program, practice.py, I
wrote the command : Print I ran my first program. To run this program I open terminal and changed the directory to where
I had the filed save (Desktop). I then ran my program which output the line I ran my first program!
Every programming language has a different method of commenting. In Python, you can comment out a line by using
the pound symbol # . This indicates to Python that specific line of code should not be evaluated. Anything written after
the pound symbol is commented out, and anything written before the pound symbol is not.
The above method only comments out one line of code. If you want to comment out multiple lines use triple quotes
strings:
Page 7
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
1) You can make a python script called common imports.py . In this script, import the packages that you will use fre-
quently. Now when writing ever you write a new python script, just write the line the following line to import all the
packages you normally use from commontext imports import*
2) Use the environment variable PYTHONSTARTUP which is defined from the official Documentation as:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt
is displayed in interactive mode. The file is executed in the same namespace where interactive commands
are executed so that objects defined or imported in it can be used without qualification in the interactive
session. You can also change the prompts sys.ps1 and sys.ps2 and the hook sys. interactivehook in this file.
This method will automatically import the packages you want in interactive python while the first method will import
the packages you want in a python script.
Python Exercise 1
Write a script in Python to evaluate the following equation to derive the array Q and plot the result. (Please do this in two
ways, one of them by using numpy the other by using a loop.)
where the constants X,Y, and Z are values that the user inputs and the array c is : c = [2,4,6,8,10,12].
Hints: Use the python package linspace which you will have to import from numpy. To graph youll have to import
matplotlib.pyplot. You may also use the Python Cheat sheet in the link below:
Page 8
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
When you call your script in terminal it should look like this:
Page 9
Warren Sharpp Advisor: Dr. K.E Whitaker Python Tutorial
Your graph should look like this if you used the above values for X Y and Z:
Page 10