7 stages2 sections22 units

01.02 Lesson 2 of 10

30 min

Values, Types & Expressions

Work confidently with numbers, strings, booleans, `None` and explicit type conversion.

By the end of this lesson

You will be able to

  • Identify common built-in types.
  • Convert text and numbers deliberately.
  • Use comparisons and boolean expressions to produce decisions.

A variable is a name bound to a value

Python names can refer to integers, floating-point values, strings, booleans and the special value None. Use type(value) while exploring unfamiliar data, but do not confuse a printed representation with a data contract.

count = 12
price = 4.5
city = "Delhi"
is_open = True
result = None

print(type(count))  # <class 'int'>
print(type(price))  # <class 'float'>

Convert at the boundary

Input read from a text file or terminal may arrive as a string. Convert it when you know what the value should mean, and handle invalid input explicitly rather than assuming every string contains a valid number.

raw_hour = "08"
hour = int(raw_hour)

raw_temperature = "23.5"
temperature = float(raw_temperature)

label = str(hour)
is_morning = hour < 12

Apply what you learned

Exercise

Build a small value parser

Write a function that receives raw text values for an hour and a temperature and returns typed values.

  1. Convert the hour to an integer.
  2. Convert the temperature to a float.
  3. Check that the hour is between 0 and 23.
  4. Return a clear error for invalid input.
  5. Print the parsed values and their types.
Done when

Valid input produces an int hour and float temperature; invalid input does not fail silently.

Free references

Study resources

1 links
  1. 01
    Built-in types Article

    Focus on numeric types, text sequences, booleans, None, and type conversion.

Check your understanding

Self-test

2 questions
  1. Question 1

    What type does input() return?

  2. Question 2

    What is the difference between == and is?

Completion check

Move on when

You can inspect, convert and validate basic values without relying on implicit assumptions.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels