Data Types in Python

In this article, I will describe five main data types that we can see in the Python language. They are

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Let me give you a brief description about those data types.

Numbers

This data type can be divided into four categories. All of them are used to store numeric values.

  • int

This data type is used to store Signed Integer Values. That means positive numbers, negative numbers or zero without the decimal point.
eg : 10, 0, -10

  • long

Used to store Long Integers. (This data type is not available in Python 3. So we can use int instead of long in Python 3). And also this data type can be used to represent octal (with prefix – 0) and hexadecimal (with prefix – 0x) numbers.
eg  : 2553663L , -2553663L, 0227L, -0227L, 0xAB1FL, -0xAB1FL.
(Don’t forget the suffix – L. Although we can use both capital (L) and simple (l) to represent long, generally we use capital letter. The reason is, if we use simple, it might lead to a confusion with number 1).

  • float

This data type is used to represent Floating  Point Values.
eg : 2.5, 0.0, -2.5

  • complex

Used to represent Complex Numbers. To do that we use the suffix (J). If you like, you can use lowercase (j) too.
eg : 2.13e+7J, 3.14J

All the number types above are Immutable. That means they cannot be changed after they are created.

Two examples for assigning number values for variables have been given in the below.

mark = 75 #int
pi = 3.14 #float

String

We can describe a String as a sequence of characters within quotes. Both single quotes and double quotes can be used. Assignment of string value to a variable can be shown as below.

name = 'James White'
name = "James White"

String also is an Immutable data type.

List

List is a collection of values (or items). These items are separated by commas. Lists are enclosed by square brackets – [ ]. See the following examples.

nameList = ['Bobby','John','Ana','Summer','Robin']
cities = ['London','Leeds','Edinburgh']
mix = ['Tommy',5,'Snow',7]

Lists are Mutable. That means we can change them after they are created.

Tuple

Tuples  are also like lists. They also can have collection of values (or items) which are separated by commas. But there are two differences than  Lists. They are…

  • Tuples are enclosed by parenthesis – ( )
  • Tuples are Immutable (Cannot be changed after they are created)

See the following examples.

nameList = ('Bobby','John','Ana','Summer','Robin')
mix = ('Tommy',5,'Snow',7)

Dictionary

Interesting data type in python. In a dictionary, we can see key-value pairs. Dictionaries are enclosed by curly braces – { }.

See the following example.

nameList = {'name':'Mike','age':27,'height':'175cm'}

Dictionaries are Mutable, They  can be changed after they are created.

That’s all for today and the next week python tutorial will be on the topic of “Python Operators“. Stay with Aiten Tutorials.

I really appreciate your Comments. So don’t hesitate to put a comment. And If you think this tutorial is valuable, please share this with your friends.

Leave a Reply

Your email address will not be published. Required fields are marked *