Programs work on values, and each value has a type that decides what can be done with it. Python provides a few basic types, and it can change a value from one type to another when asked.
Basic Data Types
The basic types are int for whole numbers of any size, float for numbers with a decimal point, complex for numbers such as 3 + 4j, bool for True and False, str for text, and NoneType with the single value None. The type of any value is found with type(). A variable is created when a value is assigned to it, and its type can change when a new value is assigned.
Type Conversion
Conversion can be implicit, done automatically by Python, as when an int and a float are added and the result is a float. It can also be explicit, done by the programmer with functions such as int(), float(), str() and bool(). Converting a float to an int cuts off the decimal part. A string can be converted to a number only if it looks like one, otherwise a ValueError is raised.
Definition: Type conversion. Changing a value from one data type to another, either automatically (implicit) or by calling a function such as int() or float() (explicit).
Statements and Expressions
An expression is a combination of values, variables and operators that produces a value, such as a + 2 b. A statement* is an instruction that the interpreter executes, such as an assignment or a print call. Every expression can be used in a statement, but not every statement is an expression. Several statements can be put on one line using a semicolon, and a long statement can be continued with a backslash.
A variable in Python is better thought of as a name that refers to a value stored in memory. When the statement x = 10 is run, the name x is attached to the value 10. A later statement y = x makes y refer to the same value. Python also allows several variables to be assigned together, as in a, b = 3, 4, and two values to be swapped without a temporary variable using a, b = b, a. The function id() shows the identity of the value that a name refers to, and isinstance() tests whether a value belongs to a given type.
Table 1.3: Basic data types
| Type | Example | Use |
|---|---|---|
| int | 25 | Whole numbers |
| float | 3.14 | Decimal numbers |
| complex | 2 + 3j | Complex numbers |
| bool | True | Logical values |
| str | "BCA" | Text |
Example 1.3: Types and conversions
Problem: Show the type of a few values and the effect of explicit conversion.
Solution:
- Use type() on an int, a float, a string and a bool.
- Convert 7.9 to int, "25" to int, and 5 to str.
Result: int(7.9) gives 7, not 8, because the decimal part is dropped.
for v in (25, 3.14, "BCA", True, 2 + 3j):
print(v, type(v).__name__)
print(int(7.9), int("25") + 5, str(5) + "5")
print(float("2.5"), bool(0), bool("a"))
print(5 + 2.0, type(5 + 2.0))
Output
25 int
3.14 float
BCA str
True bool
(2+3j) complex
7 30 55
2.5 False True
7.0 <class 'float'>
Common mistakes
- Adding a string and a number directly, such as "5" + 5, which raises a TypeError.
- Expecting int(7.9) to round to 8.
- Converting a string such as "abc" with int(), which raises ValueError.
Key takeaways
- Basic types are int, float, complex, bool and str.
- type() gives the type of a value.
- int() drops the decimal part.
- Mixing int and float gives a float.
Practice questions
- What does int(7.9) return?
- Which function gives the type of a value?
- What is the difference between implicit and explicit conversion?
Answers to the practice questions
-
- type().
- Implicit is done automatically by Python, explicit is done by the programmer using a function.
Sources
- Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit I
