UNIHIVE

Study Hub UNIPORT MTH 280

MTH 280 · Introduction to Computer programming

Variables, data types, expressions and input/output

Naming variables, the basic data types, assignment, operators and precedence, type conversion, and reading and printing values.

Our notes

2 min read

Variables

A variable is a named place in memory that holds a value that can change. Constants hold values that do not change (π = 3.14159).
Naming rules (most languages): start with a letter (or _), use only letters, digits and _, no spaces, not a keyword (if, while, print in some languages). Python is case-sensitive: Score and score are different. Use meaningful names: total_units, not tu.

Basic data types

TypePythonExample
Integerint25, -3
Real (floating point)float3.14, 2.5e3
Character/Stringstr"A", "UNIPORT"
Boolean (logical)boolTrue, False

Assignment

x = 5 means "store 5 in x" (the right side is worked out first, then stored). count = count + 1 adds one to count; it is not an equation.

Operators and precedence

OperatorMeaningExample (Python)Result
+ - × ÷add, subtract, multiply, divide7 / 23.5
//integer (floor) division7 // 23
%remainder (modulus)7 % 21
pow(a, b)powerpow(2, 3)8

Precedence: brackets first, then powers, then × ÷ // %, then + -; equal ones go left to right.
Worked example. 2 + 3 * 4 - 6 / 2 = 2 + 12 - 3.0 = 11.0. (10 - 4) / 2 * 3 = 3.0 * 3 = 9.0.

Relational operators: ==, !=, <, >, <=, >= give True/False. Logical: and, or, not.
Worked example. With a = 5, b = 10: (a > 3) and (b < 5) → True and False → False; not (a == 5) → False.

Input and output

In Python, input() always gives a string, so convert it to use it as a number:
length = float(input("Length in m: "))
width = float(input("Width in m: "))
area = length * width
print("Area is", area, "square metres")
Run with 4 and 2.5 → prints Area is 10.0 square metres.

  • Formatting: print(f"{area:.2f}") prints 10.00 (two decimal places).
  • Type conversion: int("12") → 12; float("3.5") → 3.5; str(40) → "40".

Mathematical formulas in code

Area of a circle: area = 3.14159 * r * r. Hypotenuse: c = math.sqrt(a * a + b * b) (after import math). Simple interest: I = P * R * T / 100.
Worked example. P = ₦50,000, R = 10, T = 2 years → I = 50000 × 10 × 2 / 100 = ₦10,000.

Exams usually ask: valid and invalid variable names; data types; evaluate expressions using precedence, // and %; write a program that reads values, computes a formula and prints the result.

Common mistake: adding two inputs without converting them: "3" + "4" gives "34", not 7.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MTH 280 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.