Introduction to Python#

Objective#

This is a practical course designed for chemical engineers that want to learn how to program in Python. The course is very practical and will cover various topics, such as basic Python syntax, solving ODEs, stoichiometry, data visualization and basic data analysis.

Introduction#

Recently, more and more chemical and biochemical engineering students have expressed the desire to acquire basic programming skills early on in their career, with Python being the most requested language by far. Therefore, we aim to provide material and practical tutorials to learn Python, so that you have the opportunity to use Python to solve the exercises and assignments.

This course is designed to be very practical since the best way to learn how to program is by programming. You will start writing and executing code from day 1, through carefully designed exercises. The target of this course is students and professionals in Chemical and bhichemical Engineering that want to learn how to program in Python.

The course may be challenging and overwhelming at the beginning, but don’t worry if you don’t manage to complete everything. Each theme has a series of exercices that you can complete at your own pace.

There is a discussion tab on the github repository where you can post any questions your might have.

Why Python?#

Python is one of the most popular languages in use today, thanks to its easy syntax, readability, libraries available and broad documentation. It is open-source, unlike other very popular programs such as MATLAB, which require a license. It has a big community, meaning that other people have probably already asked any possible question you might be having right now. Some forums where to ask and look for questions: StackOverflow (for practical questions about code), StackExchange (for more theoretical questions). It is the to-go language for data analysis and data science, both in academia and industry.

In this course, we will use Python 3.8.

Schedule#

The course provides material and exercises. Some of the topics:

  • Introduction to the course. Introduction, motivation, tips and tricks, package installation. General syntax: indentation, for loop, while loop, functions and classes.

  • Data structures. Data structures: lists, arrays, strings, dictionaries, pandas dataframe.

  • Data visualization. Plotting with matplotlib library

  • Solvers. Solving systems of equations and stoichiometry.

  • Data analysis. Data loading, extraction, cleaning, preprocessing, analysis.

  • Balances. Mass balance, energy balance.

Learning Objectives- Master the Python’s syntax, familiarize with arithmetic operations, assignment and compound statements.#

  • Learn reusing Python code efficiently through functions and classes.

  • Master the usage of main data structures used in Python, such as lists, generators, dictionaries, sets, dataframes and arrays.

  • Effectively visualize and analyze data.

  • Operate the reactor based on kinetics for elementary and non-elementary reactions.

  • Understand the ideal reaction models: batch, continuous-stirred tank and plug flow.

  • Understand the mathematical models for the three ideal types of reactor under isothermal conditions.

  • Understand the mass balances for the three ideal reactor systems.

  • Measure the reaction kinetics to obtain a rate expression.

  • Carry out energy balances for the reactor types for a single reaction.

Helpful tips and tricks before starting#

“The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.”

— Brian Kernighan, “Unix for Beginners” (1979)

  1. The best way to learn how to program is by programming. It’s normal that you cannot solve everything and you are overwhelmed at the beginning. Don’t give up and just practice.

  2. Write, test and improve code. Do not expect to write a full program in one sitting: most programmers write, test and rewrite code many times before their program works, so do not get discouraged!

  3. Brainstorm first. Instead of reading a problem and looking for a solution on the Internet, try to think about it yourself. Re-read the problem, brainstorm possible solutions and try to write some code. Make sure that you actually understand what is asked. Only after you have spent sufficient time on this, look for hints elsewhere.

  4. Learn to comment your code early on. Before coding, visualize what you plan to do and describe it in plain, non-technical text on paper. Try to be as detailed as possible and include all the steps, even the smallest ones. This will be helpful at the beginning of your coding experience to guide you through the process and ensure you do not forget important steps. Later on, when you are more comfortable coding, it will be helpful for reference for you or other teammates. Remember that just because something seems easy for you, it does not mean your colleague can read your code and understand it right away. This is a very important principle for code deployment.

  5. Understand what the program is doing. It is important that you understand what your program is doing. Often, it is useful to simply print what you are doing, this is an easy way to visualize, and therefore better understand, the output. This approach is called tracing or printf() debugging, after the print function name in the C language).

Note

this course has been taken and adapted from The course Chemical Reaction engineering in Python by Fiammetta Caccavale.