Variable Data Types #
In Python, all variables have a specific type, which defines their properties and how they can be used. The type of a variable is determined by the value assigned to it, and it dictates the kind of operations that can be performed on the variable.
Python Data Types #
Python primarily uses four data types:
Data Type | Description | Example | Abbreviation |
---|---|---|---|
String | Text enclosed in quotes | \"Hello, World!\" |
str |
Integer | Whole numbers without decimal points | 10 |
int |
Float | Real numbers with decimal points | 3.14 |
float |
Boolean | Logical values representing True or False
|
True , False
|
bool |
Variable Types #
Unlike some programming languages like C or C++, Python automatically assigns a data type to a variable based on the value assigned to it:
Variable Type | Description | Example | Abbreviation |
---|---|---|---|
String | Text enclosed in quotes | \"Finance\" |
str |
Integer | Whole numbers without decimal points | 100 |
int |
Float | Real numbers with decimal points | 10.75 |
float |
Boolean | Logical values representing True or False
|
True , False
|
bool |
Determining the Data Type of a Variable #
It\’s often important to know the data type of a variable. You can determine this using the type()
function. The type()
function takes a variable as an argument and returns its data type. For example:
pe_ratio = 15
print(type(pe_ratio)) # Output: <class \'int\'>
In this example, pe_ratio
is an integer (int
).
Booleans #
The Boolean data type is often the result of a conditional test involving comparison or logical operators. For example:
is_equal = (5 == 5)
print(is_equal) # Output: True
print(type(is_equal)) # Output: <class \'bool\'>
Here, the double equal sign (==
) tests if the two values are equal, resulting in True
. The data type of is_equal
is Boolean (bool
).
Boolean Expression | Description | Example Expression | Result |
---|---|---|---|
Equality | Checks if two values are equal | 5 == 5 |
True |
Inequality | Checks if two values are not equal | 10 != 5 |
True |
Greater Than | Checks if one value is greater than another | 7 > 3 |
True |
Less Than or Equal | Checks if one value is less than or equal to another | 4 <= 8 |
True |
Variable Manipulations #
Understanding the data types of variables is crucial because different data types behave differently with various operators. Let\’s look at how strings and integers interact with the multiplication operator:
x = 5
print(x * 3) # Output: 15
y = \'stock\'
print(y * 3) # Output: stockstockstock
When multiplying an integer by 3, you get a numerical result. When multiplying a string by 3, the string is repeated three times. However, when you try to add an integer to a string directly, it results in an error:
print(5 + 3) # Output: 8
print(\'stock\' + 3) # Error: TypeError
You cannot directly combine an integer with a string.
Changing Variable Types #
To combine strings with integers or floats, you need to convert the number to a string using the str()
function. For example:
pi = 3.14
print(type(pi)) # Output: <class \'float\'>
pi_string = str(pi)
print(type(pi_string)) # Output: <class \'str\'>
print(\"The value of pi is \" + pi_string) # Output: The value of pi is 3.14
By converting pi
to a string, you can now concatenate it with other strings using the addition operator.
Understanding variable data types in Python is essential for effective programming. Each data type has specific properties and rules, influencing how variables can be manipulated and combined. By using functions like type()
and str()
, you can determine and change the data types of variables to suit your needs in various programming contexts, especially in financial applications where precise data manipulation is crucial.