Python for Data Science : Data Types
Data Types
Data types are the classification or categorization of data items.
It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python
programming, data types are actually classes and variables are instance
(object) of these classes.
- A data type is a classification that allows specifying which type of value a variable contains.
-
It also allows us to determine what type of mathematical operations can be applied to any given data.
-
There are different types of python data types some of which are integer, float, string, lists, dictionaries, etc.
-
The integer value is a positive or negative whole number.
-
The float value is a real number with floating-point representation.
-
The string value is a sequence of characters.
-
A list can contain data of different data types.
-
A dictionary is an ordered set of a key-value pair of items.
-
Other data types include tuples and sets.
Example 1:
# Dynamically-inferred typesx = 20print(type(x))x = '20'print(type(x))x = 20.0print(type(x))
Output:
<class 'int'> <class 'str'> <class 'float'>
Example 2:
# Manual type-conversion (string to int)
x = 20
y = '5'
print(x + int(y))
# output
# 25
Example 3:
# Automatic type-conversion (int to float)
x = 10
print(type(x))
x += 5.0
print(x),
print(type(x))
Output:
<class 'int'> 15.0 <class 'float'>
Example 4:
# Dividing Integers
a = 20
b = 5
print(a/b)
print(b/a)
Output:
4.0 0.25
Example 5:
# String "arithmetic" (actually concatenation)
a = 'Sani '
b = 'Kamal'
print(a + b)
Output:
Sani Kamal
Leave a Comment