Data Types¶
Python is dynamically typed, which means you do not need to declare variable types explicitly.
Integer¶
Integers represent whole numbers.
age = 18
print(age)
print(type(age))
Output¶
18
<class 'int'>
Float¶
Floats represent decimal numbers.
price = 19.99
print(price)
print(type(price))
Output¶
19.99
<class 'float'>
String¶
Strings represent text.
name = "Alice"
print(name)
print(type(name))
Output¶
Alice
<class 'str'>
String Formatting¶
name = "Alice"
age = 20
print(f"{name} is {age} years old")
Output¶
Alice is 20 years old
Boolean¶
Booleans represent truth values.
is_active = True
print(is_active)
print(type(is_active))
Output¶
True
<class 'bool'>
None¶
None represents the absence of a value.
result = None
print(result)
print(type(result))
Output¶
None
<class 'NoneType'>
Type Conversion¶
Convert values between different types.
age = "18"
print(int(age))
print(float(age))
Output¶
18
18.0
Check a Type¶
Use type() to inspect a value's type.
name = "Alice"
print(type(name))
Output¶
<class 'str'>
Common Built-in Types¶
| Type | Description | Example |
|---|---|---|
| int | Integer | 18 |
| float | Decimal number | 19.99 |
| str | Text | "Hello" |
| bool | Boolean value | True |
| NoneType | Empty value | None |
Summary¶
intstores whole numbersfloatstores decimal numbersstrstores textboolstoresTrueorFalseNonerepresents no value- Use
type()to inspect a type - Use functions such as
int(),float(), andstr()for type conversion