Python Data Types
1. Overview of Data Types:
Python is a dynamically typed language, meaning you don't need to declare the type of a variable explicitly. Here's a summary of some commonly used data types in Python:
- Integer (
int
): Whole numbers without any decimal point, e.g.,5
,-10
,100
. - Float (
float
): Numbers with a decimal point or numbers written in exponential form, e.g.,3.14
,-0.001
,2.5e2
. - String (
str
): Ordered sequence of characters enclosed within single, double, or triple quotes, e.g.,"hello"
,'world'
,'''multiline string'''
. In most cases, single quotes (' '
) and double quotes (" "
) can be used interchangeably - Boolean (
bool
): Represents truth values,True
orFalse
. Notice that the first letter is always capitalized while the rest remains lowercase.
We can get the type of a variable or an object using function type()
as shown below.
It's easy to mix up variable names with strings. Let's clarify the difference between the two with an example:
2. Data Type conversion
Converting between different data types is a common task in programming. Python provides built-in functions to convert data from one type to another. In this section, we'll explore how to convert float, string, and boolean , integers between each other.
Converting Float, String, Boolean to Integer:
You can convert a floating-point number to an integer using the int()
function. This function truncates the decimal part of the float, effectively rounding towards zero. After applying this function to variables, the variable type becomes int
.
To convert a string to an integer, you can use the int()
function as well. This function parses the string and returns an integer if the string represents a valid integer literal. If the string contains non-numeric characters, it will raise a ValueError
. A ValueError
arises when a function receives an argument that doesn't match its expected input type, suggesting that the function is unable to proceed.
Booleans in Python (True
and False
) can be converted to integers where True
is equivalent to 1
, and False
is equivalent to 0
. You can use the int()
function or directly perform arithmetic operations to achieve this conversion.
Below is a summary of the content above:
- Use
int()
to convert float, string, and boolean values to integers. - When converting from float to int, the decimal part is truncated.
- For strings, ensure that the string represents a valid integer literal; otherwise, a
ValueError
will be raised. - Booleans are converted to integers as 1 (
True
) or 0 (False
).
Converting Integer, String, Boolean to Float:
Similarly, we can convert integers
, strings
, and boolean
to float
using the function float()
.
- When converting from
int
tofloat
, the decimal part is added. - For
strings
, ensure that the string represents a valid integer literal; otherwise, aValueError
will be raised. Booleans
are converted tointegers
as 1.0 (True
) or 0.0 (False
).- After applying this function to variables, the variable type becomes
<class 'float'>
.
Converting Integer, Float, Boolean to String:
Similarly, we can convert integers
, float
, and boolean
to strings
using the function str()
. The function basically adds two quotes around the variable value and converts the variable type to <class 'str'>.
Converting Integer, String, Boolean to Float:
Converting data to boolean values in Python follows simple rules:
- For numerical types (floats and integers), non-zero values are
True
, while zero isFalse
. - For strings, empty strings are False, while non-empty strings are
True
. - For collections and None, empty values are False, while non-empty or
None
values areTrue
. - The function converts the variable type to
<class 'float'>