Sunday, 12 January 2020

Python 🐍 Plots

A plot is a visual representation of the data and is especially valuable to analyze data graphically. You can plot with the matplotlib package. In the incubator example, we may want to see how the temperature changes with time. The x-axis may be time and y-axis may be the data. In an incubator, graphs could be used for a number of things. Some could be heater percentage, egg temperature, and much more.

IPython Notebooks on Github:

Tuesday, 7 January 2020

Begin Python 🐍 for and while loops

There are two basic types of loops including for and while. A for loop is to repeat code a predetermined number of times. A while loop is to repeat code but where the stopping condition may not be known before the loop starts.

Source code:

Monday, 6 January 2020

Begin Python 🐍 Course Introduction

Welcome to this introductory course on Python! This course is intended to help you start programming in Python from little or no prior experience. There are video tutorials for each exercise if you have questions along the way. One of the unique things about this course is that you work on basic elements to help you with a temperature control project. You will see your Python code have a real effect by adjusting heaters to maintain a target temperature, just like a thermostat in a home or office.

One of the best ways to start or review a programming language is to work on a simple project. These exercises are designed to teach basic Python programming skills to help you design a temperature controller. Temperature control is found in many applications such as home or office HVAC, manufacturing processes, transportation, and life sciences. Even our bodies regulate temperature to a specific set point. This project is to regulate the temperature of the TCLab. Each TCLab has thermochromic (changes color with temperature) paint that turns from black to purple when the temperature reaches the target temperature of 37°C (99°F).

Final Project Objective: Program the TCLab to maintain the temperature at 37°C. Display the heater level with an LED indicator as the program is adjusting the temperature. Create a plot of the temperature and heater values over a 10 minute evaluation period.

To make the problem more concrete, suppose that you are designing a chicken egg incubator. Temperature, humidity, and egg rotation are all important to help the chicks develop. For this exercise, you will only focus on temperature control by adjusting the heater.

There are 12 lessons to help you with the objective of designing the temperature control for the incubator. The first thing that you will need is to install Anaconda to open and run the IPython notebook files in Jupyter. Any Python distribution or Integrated Development Environment (IDE) can be used (IDLE (, Spyder, PyCharm, and others) but Jupyter notebook is required to open and run the IPython notebook (.ipynb) files.

Install Anaconda (Python):

All of the IPython notebook (.ipynb) files can be downloaded at this link. Don't forget to unzip the folder (extract the archive) and copy it to a convenient location before starting. The course is freely available for download with course files on Github:

1. Overview
2. Debugging
3. Variables
4. Printing
5. Classes and Objects
6. Functions
7. Loops
8. Input
9. If Statements
10. Lists and Tuples
11. Dictionaries
12. Plotting

You will also need a TCLab kit to complete the exercises and they are available for purchase on Amazon:

More information on the temperature control lab:

Begin Python 🐍 Overview and pip install

This introductory course uses the TCLab package that can be installed with:

pip install tclab


!pip install tclab --user

if you are running in a Jupyter notebook and do not have administrative privilege. The 12 modules of this course are intended to help you complete the final project.

Final Project: You have eggs that need to hatch in an incubator. One option is to constantly check the temperature and adjust the heaters manually. Another way is to automate the temperature control by constantly checking the temperature and adjusting the heaters with Python. Unfortunately, you only get three eggs for the test and one attempt to get it right. You do have a simulator of the incubator (TCLab) so you can practice Python, without having to worry about mistakes. The purpose of this lab is to develop a temperature controller (like a thermostat) that could be used for an egg incubator. There are other factors such as humidity and turning the eggs that are important with incubators but we'll only focus on the temperature for this project.

Source files:

Begin Python 🐍 Pseudo-Code and Debugging

One of the biggest time consuming parts of programming is debugging, or resolving mistakes in the program. This is true for every language because the computer needs exact commands, which is very important for precise measurements and control for incubating. A few steps can limit the time you are searching for mistakes, instead of completing the project.

1. Start with understanding the big picture. It seems silly, but once you start going over the whole thing you find a lot of gaps. Do this in whichever way suits you best, we’ll leave it up to you. For the egg incubator, it would be understanding what you actually need to do to help an egg hatch.

2. Start by outlining your code, writing **high-level instructions (pseudo-code)** what you want each section of the code to do. Break it into more specific tasks. You can do this even without understanding the basics of Python. Once you learn Python basics, you can translate these high level instructions into code. Organizing the outline helps to make sure your programming isn't more complex than it needs to be.

3. Program the specific tasks and connect them together. Direct tasks make it significantly easier to program because the program is modular. It’s a lot harder to program something if the program is large and complex. For the incubator, this may be programming something specific like how hard the heater should work, based on a low temperature reading.

4. Test and fix problems. This is basically debugging, but don’t only test the whole project all in one go. It’s much easier to find a problem if you test every once in a while, when you are programming smaller parts. A good way to do this is grab a specific piece of code, run it on a seperate file, and see if it does the job you want. An example would be fixing when the heater should stop working, so the egg doesn't go over temperature.

The better you follow these steps the less time you will have to spend fixing problems in your code for your incubator, or just Python programs in general.

Begin Python 🐍 integer, string, float

Variables store information and are objects in Python. For example, if you wanted to keep a set temperature for an egg, you would type egg = 37.5 for °C or egg = 99.5 for °F. The first part tells what the variable will be called, and the value after the = tells what is being stored.

See for source files.

Begin Python 🐍 print

Printing is displaying values to the screen. The word print comes from the time when programs previously put ink on paper. You use the built in python function print() to output values. You could use this to tell you what is the current temperature from a temperature sensor.

See for source files.

Begin Python 🐍 Classes and Objects

Classes are collections of objects and functions. Many Python packages such as time, tclab, numpy, scipy, gekko, and others are distributed as classes. A class is imported with the import statement such as import time. Time is a package that has timing functions that we will use to pause the program for a specified amount of time. TCLab package has functions created with tclab.TCLab(). The next lesson shows how to use the tclab functions.

Source files:

Begin Python🐍 functions

Functions create modular code that can do the same task repeatedly without you having to type out the same code each time. Functions make complex code accessible with a single statement. You also can create your own function, but there are also some are built in to Python or in many packages. One built in function you have already seen is the print() function.

Source code:

Popular Posts


Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (94) R (69) Selenium Webdriver (2) Software (13) SQL (27)