Urgent.News

What's breaking now, across thousands of outlets.

Tech

Python Basics in the Matatu Context

Watch a matatu conductor work for one trip and you will see a programmer at full speed. He counts heads. He knows the fare. He takes a KSh 500 note from someone who owes KSh 70 and hands back the exact change without a pause. He also decides, by feel, when the vehicle is full enough to leave. Listen to what he tracks: a number plate, a seat count, a fuel bill and a yes or no on whether the…

In the context of a matatu (minibus taxi) driver, the article explains Python basics through a series of code examples. The conductor's job involves counting passengers, determining fares, handling change, and checking if the vehicle is full. These tasks involve various data types: text, whole numbers, decimal numbers, and true-or-false values.

Variables in Python are like sticky notes with names attached to them. When you assign a value to a variable, you're essentially sticking a note onto that value. The assignment operator (=) doesn't mean "equals" in the traditional mathematical sense; it means "stick this name on that value." Python evaluates the right side first, then attaches the name on the left. This explains why a line that might seem nonsensical in algebra doesn't cause an error in Python.

For instance, if the matatu sacco raises its fare by KSh 20, the code `fare_kes = fare_kes + 20` takes the current fare value (100), adds 20, and moves the sticky note onto the new result. The original value remains unchanged; the note simply moves to the new location.

Python has four primary data types: strings (text), integers (whole numbers), floats (decimal numbers), and booleans (true or false values). Each type determines what operations you can perform on the data. Strings are text inside quotes, and Python distinguishes between single and double quotes. Strings can be manipulated in various ways, such as counting characters with `len()`, converting to uppercase with `upper()`, concatenating with `+`, and slicing to extract portions of the text.

Integers are whole numbers, and Python has three division-related operators: `//` for integer division, `%` for the modulus (remainder), and `**` for exponentiation. For example, if a matatu has 47 riders, `47 // 14` would return 3 (since it's integer division), `47 % 14` would return 5 (the remainder), and `2 ** 10` would return 1024 (2 raised to the power of 10).

Booleans represent true or false values and are used in conditional statements. Python methods like `startswith()` can be used on strings to check conditions. For instance, if a Safaricom phone number starts with "07", the method `startswith("07")` would return `True`.

When combining different data types, such as strings and integers, Python will raise a `TypeError`. To concatenate a string and an integer, you must first convert the integer to a string using `str()`. For example, `print("Seats: " + str(14))` would correctly print "Seats: 14".

Understanding these fundamentals helps demystify Python's syntax and rules, making it easier to write and read code.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Bringing Caelestia Shell to Ubuntu-Based Distros Like Zorin OS

I really like the Caelestia Shell , and I wanted to use it on my system. However, it doesn't officially support Ubuntu-based distributions, and in my case, I'm using Zorin OS .

  • Author seeks to run Caelestia Shell on Ubuntu-based Zorin OS
  • Creates CLI script for power users to install Caelestia Shell
  • Develops comprehensive application to simplify Caelestia Shell setup

More from Tuesday 29 September →