Python Lesson 1
1 Lesson outline
Pythonprogramming language main features.
- Variable assignment and arithmetics
- A first data with
Apart from the methods given below, you can also create an independent
Python program and launch it from a terminal.
Python programs have
.py suffix and to run a program you should use the command
python filename.py. We will go back to this at the end of the
The following methods try to facilitate code development and make possible to explore results in an interactive way.
You can start a
Python interpreter in a given terminal.
(py3) [pts/18][curro.modesto:~]$ python Python 3.7.4 (default, Aug 9 2019, 18:51:30) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> 2+2 4 >>>
To exit type
IPython project is a friendly alternative to the plain
Python interpreter and it provides
- tab completion
- pretty-printed output
- facilitates to execute arbitrary blocks of code (%run)
- help ? and ??
- additional context if exceptions are raised
- magic commands: %run, %timeit, %%timeit
It is launched with the command
(py3) [pts/18][curro.modesto:~]$ jupyter qtconsole
Note that in some (older) environments the console should be launched as
(py3) [pts/18][curro.modesto:~]$hem/Python]$ ipython qtconsole [TerminalIPythonApp] WARNING | Subcommand `ipython qtconsole` is deprecated and will be removed in future versions. [TerminalIPythonApp] WARNING | You likely want to use `jupyter qtconsole` in the future
This is the method we will mostly use in the course. The notebooks are launched with the command
(py3) [pts/18][curro.modesto:~]$ jupyter notebook [I 18:10:08.941 NotebookApp] Serving notebooks from local directory: /home/curro/ISP/Python [I 18:10:08.941 NotebookApp] The Jupyter Notebook is running at: [I 18:10:08.941 NotebookApp] http://localhost:8888/?token=d3225c10006bab47630b8013dfe3961e80e8a4f751536901 [I 18:10:08.941 NotebookApp] or http://127.0.0.1:8888/?token=d3225c10006bab47630b8013dfe3961e80e8a4f751536901 [I 18:10:08.941 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 18:10:09.021 NotebookApp] To access the notebook, open this file in a browser: file:///home/curro/.local/share/jupyter/runtime/nbserver-2413-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=d3225c10006bab47630b8013dfe3961e80e8a4f751536901 or http://127.0.0.1:8888/?token=d3225c10006bab47630b8013dfe3961e80e8a4f751536901
And the landing page will be displayed in a tab of a predefined
browser. If the browser is not running it will also be launched. In
the landing page click on the
New button and select
Python 3. A
new notebook will open and you can run your first command in it.
Commands are run in cells, and one should distinguish between plain text cells, markdown cells, and command or code cells (which may have an accompanying output or not).
In markdown cells you can use this popular markup language (a superset
of HTML) to add explanations and comentaries beyond the usual use of
Python comment within code cells. Check in jupyter markdown support to learn more about this kind of cells. You can even use LaTeX commands to include equations and formulas into these cells.
Code cells, as the rest of the cells, have two modes: edit mode and
command mode. This is similar to the way the text editor
and sometimes could be a little exasperating. It is easy to get used
to it. In command mode the left cell edge is light blue colored and
your keystrokes are translated into commands or operations within the
Jupyter provides a list of keybord shortcuts under the menu
Help > Keyboard Shortcuts or by pressing
H in command mode. Some of the most useful commands are
- to insert a new cell above the current cell.
- to insert a new cell below the current cell.
- to change the current cell to Markdown.
- to change the current cell to to code.
- D D
- (press the key twice) to delete the current cell.
- Shift Tab
- to display the Docstring (documentation) for the the object you have just typed in a code cell (keep pressing this shortcut to cycle through modes of documentation).
- Ctrl Shift –
- to split the current cell into two from where your cursor is.
- Esc F
- to find and replace on your code but not the outputs.
- Esc O
- to toggle cell output.
- Shift Up/Down
- to select the next cell in an upwards/downwards direction.
- Shift M
- to merge multiple cells.
The edit mode is marked by a green left edge in the cell, your key typing is included as text into the cell.
You can swicth from command to edit mode (a) double clicking into
the cell or (b) if the cell is highlighted, pressing
return. To go
back to command mode, click anywhere outside the cell or press
You are now ready for your first
Python experience. Type the following statement in a cell and press
SHIFT-RETURN to run the cell.
When you save the notebook (the system backups them from time to time too) the information will be saved (both inputs and outputs) into an
Once you launch
Spyder you will find the code editor in the left
hand side of the screen and two panels in the right side of the
screen. The code editor provides, among other goodies, syntax
highlighting, automatic complations, and warnings.
The upper right panel by
default is the
Help Screen and it has three other tags under it that
can be used to display in this panel
Help– This panel provides information, documentation, and many actions. Shortcut to display help on a command or statement:
Variable Explorer– In this panel you find names and properties of variables in the active session. Double clicking on a variale you can access its contents and change it.
Plots– – From this panel you can access plots.
Files– – From this panel you can access files.
The lower right panel is the
IPython console with tags
IPython Console– Interactive Python shell with many more features than a console. It makes system commands available directly and it allows you to see plots inline.
History Log– Command history. It is available in both consoles using the arrow key.
You can use
help(COMMAND) in a console to get help on a particular command. It is possible to customize the Spyder interface by choosing from different syntax themes. You can access the settings menu by clicking
Tools --> Preferences.
Click on the different options to test the setting for different windows. E.g., you can change the syntax color scheme from
3 Variable assignment and arithmetics
3.1 Variable assignation
Assiging one variable value
mass = 64.0 print("the variable mass value is ", mass, " kg")
Assiging several variables simultaneously
first_var, second_var, forward = "hello", 243., 1 print(first_var, forward) print(second_var)
You can check
Tab-completion in the notebook. Another feature that
works in the notebook is object introspection, using the
character after any instance will open a pager where a basic
information about the instance is displayed.
You can check what are the variables you’ve created and the modules you’ve loaded in the notebook with the
3.2 Basic arithmetics
You can include comments in the preceding line or after your code using the hash mark (
# Addition print (6+2) # Substraction print (6-2) # Product and quotient print (6*2) print (6/2) # Beware: difference between integer and float numbers in Python 2 # Integer quotient and modulus print (5//2) print (5%2) # Raising to a power print (6**2)
4 Import the
We start importing the
Numpy library wich can be considered the foundational library for numerical calculations in Python.
import numpy as np
We perform some simple array operations in order to test Numpy, and its most important contribution, the
ndarray type. It stands for N-dimensional array object and it is possible the most salient feature of Numpy, providing fast array-oriented arithmetics and broadcasting possibilities. Apart from this, Numpy also provides a set of mathematical functions optimized to work with such arrays, tools for file I/O, and linear algebra, fast Fourier transforms and a pseudo-random number generator (and an API for communicating with C or Fortran libraries).
We can get a first example of this structure building an array of normally distributed random data
nrand_array_a = np.random.randn(3,3) print(nrand_array_a) print(nrand_array_a*100) print(nrand_array_a - 2*nrand_array)
Every Numpy array has a shape and data type that can be obtained using the
print(nrand_array_a.shape) print(nrand_array_a.size) print(nrand_array_a.dtype)
Numpy first impressions
You have in Moodle or Blackboard a simple dataset consisting on the output of a climate model for average monthly temperatures between the years 1961 and 2096 for several cities in Cyprus. The files are of csv type, in the folder
TData and can be downloaded as supplementary documentation for the course from this link. This dataset will be our guinea pig in our first steps with Numpy.
5.1 Data reading
We first read the data using the
loadtxt. You can get help on it using object introspection and later we will examine with more detail file input and output.
Read one of the provided files and store the data in a variable. You can check using tab completion if the data file path is correct.
metdata_orig = np.loadtxt(fname='files/TData/T_Alicante_EM.csv', delimiter=',', skiprows=1)
5.2 Data handling
You can check that the variable you’ve created is of
ndarry type with
%whos or using the
As previously, you can use the
shape to characterize the data array
dtype has a different role, related to the building of derived types in Numpy, and it will not be addressed in this course.
In Python objects one can distinguish between attributes (data, objects are a named collection of attributes) and methods (functions). These functions provide information about the variable and they can be accessed using the syntax
Numpy arrays have some particular features that are built to optimize code speed and efficiency, saving data in contiguous memory blocks and allowing to perform complex operations on such arrays.
You can access individual array elements (negative indexes imply counting from the end of the given dimension).
print(metdata_orig[0,0]) print(metdata_orig[0,1]) print(metdata_orig[1,0]) print(metdata_orig[-3,3])
Or you can also work with full array slices or subarrays and perform operations with the arrays
small_a = metdata_orig[0:5,0:4] small_b = small_a small_c = small_a -small_a + small_b*small_c/100
In this case the default initial value is zero and the default last value is the last array element. Therefore, we can extract the temperature data and remove the information about the years as follows
temp_data = metdata_orig[:,1:]
|Exercise 1.1||Create an array containing only the year data in the first column of the array read from the datafile.|
We can compute, e.g. the maximum, minimum, and average monthly temperatures for the year 1961 as
print('maximum monthly temperature:', np.max(temp_data[0, :])) print('minimum monthly temperature:', np.min(temp_data[0, :])) print('average monthly temperature:', np.mean(temp_data[0, :]))
As mentioned before you can use the attribute syntax, applying the
mean methods to the array
print('maximum monthly temperature:', temp_data[0, :].max()) print('minimum monthly temperature:', temp_data[0, :].min()) print('average monthly temperature:', temp_data[0, :].mean())
|Exercise 1.2||How would you compute minimum, maximum and average March temperatures for the given dataset?|
|Exercise 1.3||Check the info on the numpy mean function and compute the average monthly temperatures and the average annual temperatures for the given dataset (Hint: axis option)|
|Exercise 1.4||Get the maximum and minimum monthly temperatures for year 2000 and at what months did these temperatures occur.|
- Exercise 1.1
- Create an array containing only the year data in the first column of the array read from the datafile.
- Exercise 1.2
- How would you compute minimum, maximum and average March temperatures for the given dataset?
- Exercise 1.3
- Check the info on the numpy mean function and compute the average monthly temperatures and the average annual temperatures for the given dataset (Hint: axis option).
- Exercise 1.4
- Get the maximum and minimum monthly temperatures for year 2000 and at what months did these temperatures occur.
Created: 2023-09-30 Sat 05:06