Introduction to Programming and Python#
Why do you need Python?#
Python is a computer programming language that is widely used in both academia and industries related to Civil Engineering, Environmental Engineering and Applied Earth Sciences. Being skilled in Python will help you on multiple occasions, including in your master courses.
What can computers do for us?#
As an engineer or scientist, you will deal with units of information which are called data. The most important tasks that a computer can do for us are:
Reading data
Processing data
Visualizing data
These are tasks that require many repetitions and high precision.
1. Reading data Reading data means the computer acquires data from a source and places it into its volatile memory for processing. Volatile memory keeps the data stored until the power supply is interrupted. The way data is stored in a computer determines how the computer will be able to use it.
2. Processing data Data processing is the manipulation of the stored data in a system. After processing data by performing transformations, calculations, and more, you get an output; results.
3. Visualizing data We map data (original or found) to graphic elements to communicate clearly and efficiently the information contained in the data. A graphic element is an element of a chart, such as a line or a point in the chart.
What is programming?#
Programming is giving your computer a list of instructions for computations in a language it can understand. In your case, this language is Python. A computation is a series of arithmetical (“math”) and non-arithmetical (“non-math”) steps that transform input to output (result). There are five different kinds of instructions for computations you use. By ordering and combining them, the computer can achieve results that fulfill the three tasks described earlier. The five kinds of instructions are:
input: Insert data from a file, the network, other devices, or simply by typing it in.
output: Display data on the screen, save them in a file, send it over the network, etc.
math: Perform basic mathematical operations like addition and multiplication.
conditional execution: Check for certain conditions before further instruction.
repetition: Perform instructions repeatedly, usually with some variation.
Introduction to Python#
The features of Python are what make it so popular. From the definition available on the corresponding Wiki page: “Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs, as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.”. Quite a simple, yet concise description due to the meaning hidden behind each buzzword:
- Interpreted means that there is an interpreter, a software tool, which reads and performs instructions written in Python. If your laptop, phone, or refrigerator has the interpreter, it can run most of the Python scripts!
- High-level means that the programming language operates with abstract and more easily understandable concepts. In other words, you don't have to write or understand, code related to the hardware part of your machine (like binary code or assembly).
- General-purpose means that the amount of Python application fields are endless. You can write Python scripts to manage and automate some primitive tasks for yourself, you can use it for data science, create your own machine / deep learning project, write your personal web application or even develop a game.
- Programming language means a set of specific predefined semantics, instructions, and syntax rules, which are used for writing necessary instructions. It is strictly defined, meaning that 99.99% of all errors in the code are made by the coder, not by the computer.
Python scripts run with the help of a Python interpreter, but they can be written by using different software tools. Just like as you write your essay (you can type it in Word, Google Docs, Overleaf, or even on plain paper) you can write your Python code with different editors. You could use the default notepad, notepad++, find a proper website with an inbuilt code editor & interpreter (IDEone, for example), or use specialized Python code editors (Spyder, PyCharm, Visual Studio, etc). In all cases you will produce a set of instructions, which are stored in a file with the *.py extension and the interpreter will run it completely (from top to bottom).
For this course we will use a slightly different approach to developing a Python script: IPython and Jupyter Notebooks. Think of these as two freely-available tools that add extra functionality to the basic Python programming language. As an example, imagine the camera built into your phone: on it’s own it can take pictures, but there are many apps that add filters or sharing via social media that make the impact of your photos much more powerful. This is exactly what IPython and Jupyter Notebooks do for Python, and why they are such important tools in the toolbox of modern engineers. The next Chapter will introduce you to your Python Toolbox for this course!