Data Types in Python
In Python, every value has a data type that determines its nature and the operations that can be performed on it. Python has several built-in data types, including:
- Integer (
int
): Represents whole numbers, both positive and negative. - Float (
float
): Represents real numbers with decimal points. - String (
str
): Represents sequences of characters. - Boolean (
bool
): Represents eitherTrue
orFalse
.
Let's take a look at an example that demonstrates the use of the type()
function to determine the data types of variables:
PYTHON
1# Python code to demonstrate
2# type() built-in function
3
4# initializing variables
5a = 5
6b = 2.7
7message = 'Hello, world!'
8
9# printing the types
10print(type(a))
11print(type(b))
12print(type(message))
The above code will output:
SNIPPET
1<class 'int'>
2<class 'float'>
3<class 'str'>
This shows that a
is of type int
, b
is of type float
, and message
is of type str
.
Understanding the data types in Python is essential as it allows you to perform appropriate operations and manipulate data effectively.
xxxxxxxxxx
12
# Python code to demonstrate
# type() built-in function
# initializing variables
a = 5
b = 2.7
message = 'Hello, world!'
# printing the types
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(message)) # <class 'str'>
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment