Python programmers often have
In this article, we will show 5 data type in python. Python has five standard Data Types.
The Python data types are: numbers, strings, list, tuple, and dictionary.
This is something that every programmer need to know. It
After reading this article, you can prevent
First, let’s talk about numbers
Numbers
“The most used number data type is integer and floating. But that is not the only number data type in python. Python has another number data type. There is complex, decimals and fractions.
Integer
Integer is the whole number without fractional number. It includes positive, negative and zero number. But it does not include 1.5, because that is a fractional number. 50% is not an integer because it is a fractional number. Example of integer is -60, 31, 0, 50 38. and so on.
If we assign integer number to a variable, python automatically defines its number and variable as an integer.
In Python programming language, declaration data as
This is an example. Var1 = 23. This
>>> var = 34
>>> type(var)
<type 'int'>
Float Point Number
Float point number is
In python, to define that some variable as float type, we just assign float point number to a variable. For example, we have variable C and data float point, 1.3.
>>> c= 1.3 # dot in this number is sign of decimal number after dot.
>>> type(c)
<type 'float'>
It is possible in python to make mathematic
>>> c= 1.3
>>> type(c)
<type 'float'>
>>> d = 29
>>> type(d)
<type 'int'>
>>> w = c+d
>>> w
30.3
>>> type(w)
<type 'float'>
Python support operand type(s) for float and integer.
Complex Number
>>> 3+6j
(3+6j)"
Strings
strings are sequences of alphabet, text, or number data. It is character data. Number as character data different than gives
To make python understand, that some data is strings is by add quote or double quote
This is example number as character data.
>>> x = '9' #
>>> type(x)
<type 'str'>
>>> x *5
'99999'
The example show that ‘9’ * 5 (‘9’ multiply 5) is not 45, but ‘99999’. Because, the ‘9’ is not integer, but string.
>>> x = '8'
>>> x*4
'8888'
>>> x +5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>>
List
A variable can have more than one data or lists. Python
Tuple is also collection of values. Unique feature of list compare to tuple is mutable. We change the member of list easily.
your_list = [2,3,4,5,6]
my_list = ["we', "are", "champion"]
empty_list = []
Python give index of every member of list. The index is start with 0. There are 0,1,2,3 and so on. So we can also retrieve the list by its index number. For example.
>>> ere = ["yes", 90, "wow"]
>>> ere[0]
'yes'
>>> ere[2]
'wow'
List can be member of another list.
>>> ere = ["yes", 90, "wow"]
>>> newlist = [ere, [3,5,"5"]]
>>> newlist
[['yes', 90, 'wow'], [3, 5, '5']]
>>>
We can retrieve list in the list by two bracket square.
>>> newlist
[['yes', 90, 'wow'], [3, 5, '5']]
>>> newlist[0][1]
90
>>>
Tuple
Tuple is also collection of value. Different than list data type, tuple data type immutable. We can not change its member easily.
It is simple to create a tuple. We can type
>>> mytuple=(1,2,"three")
>>> type(mytuple)
<type 'tuple'>
>>>
Dictionary
Dictionary is also a collection of value. But different than list and tuple, dictionary, the values of
eng2g = {"one": "eins", "two": "zwei", "three": "drei"}
To declare a variable as dictionary is simple. If list have bracket square [], tuple have parenthesis (), dictionary have {}. The pair of value have :
>>> gg ={}
>>> type(gg)
<type 'dict'>
>>>