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 eitherTrueorFalse.
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.
xxxxxxxxxx12
# Python code to demonstrate# type() built-in function# initializing variablesa = 5b = 2.7message = 'Hello, world!'# printing the typesprint(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


