Common Python Data Types¶
What is a data type?¶
A data type is basically a category in which a value belongs to.
> Meaning that data types can be divided into categories like Numeric,Sequence Type etc. > Knowing the data type helps one know the kind of operations that can be done on the value.
- Every value that is held by a variable has a data type.
In [1]:
Copied!
name = 'Tony'
students = 10
height = 10.5
name = 'Tony'
students = 10
height = 10.5
- To view the type you need to use the type() function.
In [2]:
Copied!
#View Types
type(name)
#View Types
type(name)
Out[2]:
str
Although there are numerous data types, there are certain data types which are commonly used in Python.
Common data types are categorized under primitive data types, and these include integers, strings, Booleans, and floating points.
Other primitives include the Boolean and None Types.
Numeric¶
- This is data that has numeric value
Integers¶
- An integer is a value that is a whole number (+/-).
In [3]:
Copied!
#Integer
cars = 100
type(cars)
#Integer
cars = 100
type(cars)
Out[3]:
int
In [4]:
Copied!
#Negative Value
temperature = -25
type(temperature)
#Negative Value
temperature = -25
type(temperature)
Out[4]:
int
In [5]:
Copied!
#Negative Value
temperature = -25
type(temperature)
#Negative Value
temperature = -25
type(temperature)
Out[5]:
int
Floats¶
- A Float (Floating point number) is a number that has a decimal
In [6]:
Copied!
#Float
height = 100.5
type(height)
#Float
height = 100.5
type(height)
Out[6]:
float
Sequence Type¶
In python, a sequence is the ordered collection of data types that are either similar or different.
An example of this includes strings, lists, and tuples.
String¶
- A string is a text value
In [7]:
Copied!
#String
make = 'Toyota'
type(make)
#String
make = 'Toyota'
type(make)
Out[7]:
str
In [8]:
Copied!
#Empty String
color = ''
type(color)
#Empty String
color = ''
type(color)
Out[8]:
str
In [9]:
Copied!
#Number as a string
number_of_children = '11'
type(number_of_children )
#Number as a string
number_of_children = '11'
type(number_of_children )
Out[9]:
str
Boolean¶
This data type has either True or False built in values.
> N/B: T and F have to be capitalized.
In [10]:
Copied!
y = True
type(y)
y = True
type(y)
Out[10]:
bool
In [11]:
Copied!
x = False
type(x)
x = False
type(x)
Out[11]:
bool
None-Type¶
- The data type has a single value None which points to the absence of a value.
In [12]:
Copied!
#Empty Value
number_of_people = None
#Empty Value
number_of_people = None
In [13]:
Copied!
# Check None-Type
type(number_of_people)
# Check None-Type
type(number_of_people)
Out[13]:
NoneType
- You can specify a specific Data Type
In [14]:
Copied!
#Specify String
count = str(11)
type(count)
#Specify String
count = str(11)
type(count)
Out[14]:
str
In [15]:
Copied!
#Specify Integer
height = int(10.5)
type(height)
#Specify Integer
height = int(10.5)
type(height)
Out[15]:
int
In [16]:
Copied!
# Operation on a string and an integer
Bus_one = '10'
Bus_two = 11
total = Bus_one + Bus_two
# Operation on a string and an integer
Bus_one = '10'
Bus_two = 11
total = Bus_one + Bus_two
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_2004/785225065.py in <module> 4 Bus_two = 11 5 ----> 6 total = Bus_one + Bus_two TypeError: can only concatenate str (not "int") to str
In [17]:
Copied!
# Correct Operation on a string and an integer
Bus_one = int('10')
Bus_two = 11
total = Bus_one + Bus_two
# Correct Operation on a string and an integer
Bus_one = int('10')
Bus_two = 11
total = Bus_one + Bus_two
In [18]:
Copied!
total
total
Out[18]:
21
In [19]:
Copied!
#Assignment Concatenate a str to a str
Bus_one = '10'
Bus_two = str(11)
total = Bus_one + Bus_two
#Assignment Concatenate a str to a str
Bus_one = '10'
Bus_two = str(11)
total = Bus_one + Bus_two