01.02 Lesson 2 of 10
30 minValues, 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
ExerciseBuild a small value parser
Write a function that receives raw text values for an hour and a temperature and returns typed values.
- Convert the hour to an integer.
- Convert the temperature to a float.
- Check that the hour is between 0 and 23.
- Return a clear error for invalid input.
- Print the parsed values and their types.
Valid input produces an int hour and float temperature; invalid input does not fail silently.
Free references
Study resources
- 01
Check your understanding
Self-test
-
Question 1
What type does
input()return?A string (str). Convert it explicitly when a numeric or other typed value is required. -
Question 2
What is the difference between
==andis?==compares values for equality.ischecks whether two names refer to the same object.
Completion check
Move on when
You can inspect, convert and validate basic values without relying on implicit assumptions.