Skip to content

Basic values

  • Lesson 4
  • 35 min
  • Module 2: Values and control flow
Lesson 4 of 18 · Python for Practical AI EngineeringBasic values

What you will be able to do

Store the facts about one support ticket in variables, and convert text into numbers that you can calculate with. Data from files always arrives as text, so this conversion is the first step of every program in this course.

NotebookDo this lesson in a notebookColab · Kaggle · GitHub

The notebook has every example, task and check of this lesson, with the instructions. It needs no install and no local project: only Python. Read the explanations here, and run the code there.

Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.

Before you startWhat to know and to have readyAbout 35 minutes

What you need to know

What you need on your computer (only for the local route; the notebook and the boxes on this page need none of it)

  • The project’s environment .venv, active in your terminal (the prompt starts with (.venv)). You made it in Isolate dependencies: Guided practice. If it is missing or broken, delete it and make it again from the lock file, as in Make the environment again from nothing.
  • The practical check at the end makes ticket_values.py in ~/projects/ticket-cleaner.
  • The boxes in this lesson run Python in your browser. The first time that you press Run, the page downloads Python (about 13 MB). You can also type the code into a .py file and run it in your terminal. The result is the same.

Tested with Python 3.14 (terminal) and Pyodide 314.0 (browser). The notebook was tested in Jupyter.

Here is one support ticket, as a person writes it down:

Ticket T-1042, priority 2, open for 3.5 hours, urgent: yes, assigned to: nobody yet.

In Python, each fact is a value, and each value has a type. The type decides what you can do with the value. You can add two numbers, but you cannot add a number to a name.

ticket_values.py
ticket_id = "T-1042"
priority = 2
hours_open = 3.5
is_urgent = True
assigned_to = None
Variable Value Type What the type is for
ticket_id "T-1042" str (string) Text. It is always in quotes.
priority 2 int (integer) Whole numbers.
hours_open 3.5 float Numbers with a decimal point.
is_urgent True bool (Boolean) Only two values: True or False.
assigned_to None NoneType “No value”. Here: not assigned yet.

A variable is a name for a value. The = sign does not mean “is equal to”. It means “let this name refer to this value”. You read priority = 2 as “priority refers to 2”.

Run Python in your browser

See the type of each value

Press Run. Each line shows a value and its type. Then change priority = 2 to priority = "2", and run it again. What is the type now?

A variable refers to one value at a time. When you use = again, the name refers to the new value. Python runs the lines from top to bottom, so you can follow the value step by step:

hours_open = 3.5
hours_open = hours_open + 2
hours_open = hours_open * 2
After line hours_open Why
1 3.5 The first value.
2 5.5 Python calculates the right side first (3.5 + 2), then gives the name the result.
3 11.0 5.5 * 2. The result of a calculation with a float is a float.

A table like this one, with one row per line of code, is a state table. When code does something that you do not expect, make one. It shows where your idea and Python’s steps are different.

Python calculates with the usual signs: +, -, * (multiply) and / (divide). Three more are useful:

Code Result Meaning
7 / 2 3.5 Division. The result is always a float, even for 6 / 2 (3.0).
7 // 2 3 Whole-number division: how many whole times 2 goes into 7.
7 % 2 1 The remainder after whole-number division.
round(3.14159, 2) 3.14 Round to 2 decimal places.

A float is stored in binary, so some decimal numbers are not exact. 0.1 + 0.2 gives 0.30000000000000004. For hours and scores, this does not matter. Use round() when you show the result. (For money, there are better tools, which this course does not need.)

A string (str) is text in quotes. Single quotes and double quotes work the same: "open" and 'open' are the same string. Strings have methods: actions that you call with a dot after the value. A method gives you a new string. It does not change the original.

raw_status = " Open "
status = raw_status.strip().lower() # "open"

strip() removes spaces at the start and end, and lower() makes all the letters small. Data that people type often has extra spaces and mixed capital letters, so you use these two often. The text after # is a comment. Python ignores it. It is a note for the person who reads the code.

To put values into text, use an f-string: a string with f before the quote. Python replaces each name in { } with its value:

print(f"Ticket {ticket_id} has priority {priority}.")
Output
Ticket T-1042 has priority 2.

A comparison gives a bool: True or False.

Code Result Meaning
priority == 2 True Is equal to. Two = signs. One = gives a name to a value.
priority != 2 False Is not equal to.
hours_open > 24 False Greater than. Also <, >= and <=.
priority == 1 or hours_open > 24 False True if at least one side is True.
is_urgent and assigned_to is None True True only if both sides are True.
not is_urgent False The opposite.

None means “there is no value”. It is different from 0 and from the empty string "". A ticket with priority 0 has a priority. A ticket with priority None does not. To check for it, use is None or is not None.

When Python reads a file, every value arrives as a string, even the numbers. "2" is text that looks like a number, and you cannot calculate with it. Convert it first:

Code Result Note
int("2") 2 Text to whole number.
float("3.5") 3.5 Text to decimal number.
str(2) "2" Number to text.
int("2.5") error int() accepts only whole-number text. Use float() first.
int("three") error The text is not a number. You learn to handle this in Module 5.

Run Python in your browser

Convert, then calculate

Press Run first, and read the error. Then fix the two conversion lines, so the output is Priority 2: 20.5 hours left. The box checks your result.

Python accepts almost any name. People who read your code need more than that. A good name says what the value is, so the reader does not have to work it out.

Instead of Write Why
x, val, data2 hours_open Says what it is.
h hours_open Says the unit. hours_open = 3.5 cannot mean minutes.
flag is_urgent A name that starts with is_ or has_ reads as a yes/no question.
hoursOpen hours_open Python’s style is small letters with _ between words.

Names are case-sensitive: Priority and priority are two different variables. A name cannot start with a number or contain spaces or -.

A ticket has been open for "50" hours. The value is text, because it came from a file. Write code that shows this line:

T-1042 has been open for 2 days and 2 hours.

Use // and %. Give each variable a name that says what it is and its unit.

Run Python in your browser

Your turn: hours to days

The check looks for an int variable whose name ends with days.

Show one answer
ticket_id = "T-1042"
hours_text = "50"
total_hours = int(hours_text)
full_days = total_hours // 24
extra_hours = total_hours % 24
print(f"{ticket_id} has been open for {full_days} days and {extra_hours} hours.")

50 // 24 is 2 (two full days), and 50 % 24 is 2 (the hours that are left).

Common mistake

can only concatenate str (not "int") to str

Symptom: a line that joins text and a number stops the program:

print("Priority: " + priority)
TypeError: can only concatenate str (not "int") to str

Cause: + adds two numbers, or joins two strings. It cannot join a string to a number, because Python does not guess which one you want. The opposite mistake happens too: "2" + 2 when a value from a file is still text. Concatenate means “join text together”.

Fix: decide which type you need.

  1. To show a number in text, use an f-string: print(f"Priority: {priority}").
  2. To calculate with text that holds a number, convert it first: int(priority_text) + 2.
  3. If you are not sure what type a value has, print it: print(type(priority)).

Check your understanding

Predict the output of each line before you run it: print(7 / 7), print(7 // 2), print("7" * 2).

Show the answer

1.0, 3 and 77. Division with / always gives a float. // gives the whole-number part. * with a string repeats the text: "7" two times is "77", not 14. This is another reason to convert text from files before you calculate.

Check your understanding

A file has the value "False" in its is_urgent column. What does bool("False") give, and why is that a problem?

Show the answer

It gives True. bool() of a string is False only for the empty string "". Every other string, including "False" and "no", is True. To read yes/no text, compare the text instead: is_urgent = urgent_text.strip().lower() == "true".

Check your understanding

Practical check: in your ticket-cleaner project, make ticket_values.py. Start from the text values "T-1042", "2" and "3.5", convert them, and print one f-string line with the ticket ID, the priority and the hours left until a 24-hour deadline.

Show the answer

One answer:

ticket_values.py
ticket_id = "T-1042"
priority = int("2")
hours_open = float("3.5")
hours_left = 24 - hours_open
print(f"{ticket_id}: priority {priority}, {hours_left} hours left")

Run it with python ticket_values.py in the activated environment. The output is T-1042: priority 2, 20.5 hours left.

Recap

  • Every value has a type: str, int, float, bool or NoneType. The type decides what you can do with it.
  • A variable is a name for a value. = gives the name a new value. A state table shows the value after each line.
  • / always gives a float. // and % give the whole part and the remainder.
  • Text from files must be converted with int() or float() before you calculate. Use f-strings to put values into text.
  • None means “no value”. Check it with is None.

Next, you keep many values together: a list of tickets, and a record for each ticket.

Next: Collections