EduFlow

Section 1 · Python Essentials for Analysts

Values, Types and Variables

16 min3 min readPython exercise

Lesson at a glance

Store values in variables, tell integers, floats, strings, booleans and None apart, and convert text like " $129.00" into a number you can calculate with.

  1. 1Values and variables
  2. 2Every value has a type
  3. 3Text is a sequence of characters
  4. 4Converting between types
  5. 5Booleans and comparisons

No video here — just the real thing: a written lesson, runnable examples, a quiz and practice.

An order line arrives from a messy export: the unit price reads $129.00, the quantity reads 2, and a 15% coupon was applied. A human reads that and calculates the line revenue in their head. Python cannot, yet: to Python, "$129.00" is a piece of text, no more a number than the word "skillet". Knowing what kind of value you are holding is the first skill of data work.

Values and variables

A value is a piece of data: 2, 129.0, "Cast Iron Skillet", True. A variable is a name you attach to a value with = so you can use it later.

Python
product = "Cast Iron Skillet 26cm"
unit_price = 39
quantity = 2
line_total = unit_price * quantity
print(product, line_total)

Read line_total = unit_price * quantity from right to left: Python computes the right side first, then points the name on the left at the result. Variable names use lowercase words joined by underscores (unit_price, not UnitPrice or unitprice); that convention is called snake_case and every Python codebase you will read follows it.

Every value has a type

The type decides what you can do with a value. type() reveals it:

Python
print(type(2))            # int: whole numbers
print(type(129.0))        # float: numbers with a decimal point
print(type("129.00"))     # str: text, in single or double quotes
print(type(True))         # bool: True or False
print(type(None))         # NoneType: "no value here"

Notice that "129.00" is a str. Quotes make text, even when the characters inside look like digits. That single fact explains a whole family of data bugs: a column that looks numeric but was read as text sorts "100" before "20" and refuses to sum.

Arithmetic

Numbers support the operators you expect, plus three that matter in analysis:

Python
print(10 / 4)    # 2.5  true division, always a float
print(10 // 4)   # 2    floor division: whole boxes of 4
print(10 % 4)    # 2    remainder: items left over
print(2 ** 3)    # 8    power
print(0.1 + 0.2) # 0.30000000000000004

The last line is not a Python bug. Computers store decimals in binary, so most decimal fractions are stored as a tiny approximation. For money you round at the end with round(value, 2), and you never compare two floats with == after a calculation unless you rounded both. Section 4 returns to this when totals do not quite match.

One more surprise hides in round() itself. Python rounds exact halves to the nearest even digit, so round(0.5) is 0 and round(2.5) is 2, and a value like 2.675 is stored as slightly less than it looks, so round(2.675, 2) gives 2.67. For analysis and reporting that never matters. For invoices, where every cent must follow an accounting rule, Python’s decimal module exists; you will not need it in this course, but now you know where to look when a finance colleague asks.

Text is a sequence of characters

Strings come with methods: functions attached to the value, called with a dot. These are the ones you will use constantly:

Python
raw = "  united KINGDOM "
print(raw.strip())                 # remove spaces at both ends
print(raw.strip().title())         # "United Kingdom"
print(raw.upper())                 # all capitals
print("$129.00".replace("$", ""))  # swap one substring for another
print(len("Cartwheel"))            # number of characters: 9

Every one of these returns a new string. Strings are immutable: raw.strip() does not change raw. If you want to keep the cleaned version, assign it: country = raw.strip().title().

Converting between types

int(), float() and str() convert values, but only when the conversion makes sense:

Python
quantity = int("2")
price = float("129.00")
label = "Qty: " + str(quantity)
print(quantity * price, label)

float("$129.00") fails with ValueError: could not convert string to float: '$129.00'. The fix is to clean first, then convert. You chain methods left to right, each one working on the result of the previous one:

Python
price_text = " $129.00"
price = float(price_text.strip().replace("$", ""))
discount = 0.15
line_revenue = round(2 * price * (1 - discount), 2)
print(price, line_revenue)

That is the revenue formula Cartwheel uses for every order line: quantity times unit price times one minus the discount. You will apply it to thousands of rows at once in Section 2; here you see it for one.

Booleans and comparisons

Comparisons produce True or False, and these answer yes/no questions about your data:

Python
line_revenue = 219.3
print(line_revenue >= 200)       # True
print(line_revenue == 219.3)     # True: equal values
print("web" != "mobile_app")     # True: not equal

One = assigns; two == compares. Mixing them up is the most common typo for new programmers, and Python helps by rejecting if x = 3: as a syntax error.

None is a separate value that means “nothing here”. A missing coupon code or an unknown satisfaction score would be None in plain Python. pandas uses its own markers for missing data, and Section 3 is about handling them carefully.

The exercise below turns one messy export row into real numbers. Next, you store many values at once in lists and dictionaries.

Key takeaways

  • A variable is a name pointing at a value; = assigns, == compares.
  • Every value has a type, and type() tells you which one; most bugs in data work are a value having the type you did not expect.
  • / always returns a float, // drops the remainder, and % gives the remainder.
  • String methods such as strip() and replace() return a new string; they never change the original.
  • Convert text to numbers with int() and float() only after removing symbols like $ and spaces.

Further reading

Check your understanding

4 questions

Question 1 of 4

What does this print?

Python
price = "39"
print(price * 2)
Question 2 of 4

Which line turns "$1,250.00" into the float 1250.0?

Question 3 of 4

What is the value of 10 / 4 and 10 // 4?

Question 4 of 4

A customer’s coupon code is unknown. Which value best represents that in Python?

Practice

Python exercise

Turn a messy row into numbers

A row from the messy export gives you price_text = " $129.00", quantity_text = "2" and discount = 0.15. Create price (a float), quantity (an int), line_revenue (quantity × price × (1 − discount), rounded to 2 decimals) and is_large (a boolean: True when line_revenue is 200 or more). Calculate from the variables; don’t type the answers in by hand.

# One row from Cartwheel's messy export, exactly as it arrived
price_text = " $129.00"
quantity_text = "2"
discount = 0.15

# 1. Turn price_text into a float
price = None

# 2. Turn quantity_text into an int
quantity = None

# 3. Line revenue = quantity * price * (1 - discount), rounded to 2 decimals
line_revenue = None

# 4. True when line_revenue is 200 or more
is_large = None
Loading the editor…

Ctrl/⌘ + Enter to run · Runs in a sandbox in your browser. Nothing leaves your device.

(no output)

Checks

  • price is the float 129.0
  • quantity is the integer 2
  • line_revenue is 219.3
  • is_large is True