Data Types In Python
Variables can store data of different types. As a beginner, it is important to know these data types as they are very important in programming. Therefore, this article will teach you the different data types.
In Python, data types can be grouped into a few categories which are:
Text Type: In Python, it is called string and written as
str
.They are sequences of characters such as letters, numbers and symbols. In Python, we enclose the string in either a set of single quotes or double-quotes. Examples are:a = 'John' x = "H12nhgv"
Numeric Types: These are for numbers and are divided into three which are:
- Integer (
int
): This is for a whole number that can be positive or negative. In Python 3, there is no limit to the length of an integer value. Examples are:root = 25 t = -23456787654324567890
- Float: This is for a number, positive or negative that contains one or more decimals. Examples are:
sqr = 2.34 b = -0.23456
Floats can also be scientific numbers with an "e" to show the power of 10.
q = 35e3 t = -72.7e100
- Complex: This is for complex numbers. They are written with 'j' as the imaginary part. Examples are:
a = 2+j c = -4j
- Integer (
Sequence Types: There are 3 types which are:
- Lists: They are used to store multiple items in a single variable. List items are ordered, changeable, and can be duplicate values. List items are also indexed, the first has an index of [0], the second with [1] and so on. Lists are created with square brackets. Example:
cars = [ 'Audi','Ford','Chevrolet' ]
- Tuples: Like lists, these are also used to store multiple items in a single variable. Tuple items are also indexed, ordered and can be duplicated values. However, unlike Lists, Tuples are unchangeable. They are written with round brackets. Example:
cars = ('Audi','Ford','Chevrolet')
- Lists: They are used to store multiple items in a single variable. List items are ordered, changeable, and can be duplicate values. List items are also indexed, the first has an index of [0], the second with [1] and so on. Lists are created with square brackets. Example:
Mapping Type: This data type refers to dictionaries,
dict
. A dictionary is a collection that is ordered, changeable but does not allow duplicates. Dictionaries store data inkey:value
pairs. Example:clothing = { "brand": "Mavi", "style": "Jeans", "price": 200000 }
Boolean Types: Boolean is used to represent the truth values of an expression. It can also only be
True
orFalse
. For example, 23== 0 is True , whereas 2<1 is False.
Checking the type of a variable
In python, we can check the data type of an object using the built-in function
type()
.
a = 'John'
b= 234
print(type(a))
print(type(b))
When you run this program, Python prints the type of variables, a and b.
I hope you find this article helpful. lf you do, please click like and leave a comment.
Zainab Lawal