Sitemap

Built-in Data Types in Python

What is a Data Type in python ?

--

Python Data Types are used to define the type of a variable. It defines what type of data we are going to store in a variable. The data stored in memory can be of many types.

For example, a person’s age is stored as a numeric value and his or her address is stored as alphanumeric characters.

The following are the standard or built-in Data type in python.

  • Numeric
  • Sequence Type
  • Boolean
  • Set
  • Dictionary
Press enter or click to view image in full size

What is the type() function in python ?

The type() function is the built-in function in python, it takes the value or variable as input and gives the datatype of it.

1. Numeric Data type in python:

The numeric data type in Python represents the data that has a numeric value. A numeric value can be an integer, a floating number, or even a complex number.

I) Integer : This value represent by the int class. It contains positive as well as negative whole numbers. In Python, there is no limit to how long an integer value can be.

II) Float : This value is represented by the float class. It is a real number with a floating-point representation. It is specified by a decimal point.

Press enter or click to view image in full size

here, the type() function is used to determine the type of data type .

2. Sequence Data type in python:

sequence Data type in python is order collection of similar or different datatype.Sequences allow storing of multiple values in an organized and efficient fashion.

I) String : A string is a collection of one or more characters put in a single quote and double-quote. In python there is no character data type, a character is a string of length one. It is represented by str class.

Strings in Python can be created using single quotes or double quotes .

  • Syntax:

String_name = "value"

  • Example:
  • String1 = "welcome"
Press enter or click to view image in full size

here, we can create a String1 is a variable which is holding as string type.

We discussed built-in functions in our past article. You can refer to the article using the following link.

II) List :

List is the Data type in python. We can use the any type of element in list which means it can be int, float, str, tuple, list and nested list.

The list is a mutable in nature shich means we can add, remove and modify the list.list support a indexing as well as slicing.

  • Syntax:

variable_name = [] This will create empty list

variable_name = list() This will also create empty list

  • Ex:

a = [1,2,3.3, [4,5]]

a is list type of variable which holds int, float and list type of element. Each value can be called as element.

Lists in Python can be created by just placing the sequence inside the square brackets[ ]. We can access the element in the list using indexing & slicing.

Press enter or click to view image in full size

We will discuss List built-in functions in our article. You can refer to the article using the following link.

https://medium.com/@wvrushali27/built-in-list-functions-methods-in-python-a761342452dd

III) Tuple:-

Tuple is a built-in datatype in python. It can hold any kind of value which means int, float, str, list and tuple. Tuple supporting the index as well slicing.

It is also an ordered collection of Python objects. The only difference between a tuple and a list is that tuples are immutable i.e. tuples can not be modified after it is created. It is represented by a tuple class.

  • Syntax:
  • variable_name = () This will create empty tuple
  • variable_name = tuple() This willl also create empty tuple

Example :

  • b = (1,55,True, [100,200])

here ‘b’ declare the variable as a tuple which is holding int, bool and list type of values.

Press enter or click to view image in full size

In above example, we create a a1 is tuple type of variable holding the value as int, float, string, nested list etc.

Get Vrushali Walekar’s stories in your inbox

Join Medium for free to get updates from this writer.

Tuple supports the slicing as well as indexing, how to use the indexing and slicing in a1 tuple type of variable.

Press enter or click to view image in full size

To print all built-in methods in tuple we can use dir().
In Tuple their are only two built-in methods:

  1. Index()
  2. count()

Here, we will discuss one by one built-in methods in tuple.

  1. count():

We use the count() method in tuple to count the number of occurrences of value. count() method take a input or arguement as a value and return a number of occurances of value.

In, following example t1 is tuple variable which is holding a value as a int and nested tuple, intir nested tuple holding a string type of value.

here we use a count() i.e In[12] it will gives output as 3 wich means the occurrences of 1 is 3 time.

Press enter or click to view image in full size

2.index():

We use the index() method in tuple to print the index of value. index() method take a input as value returns the first index of value.

In following example, we take a t1 as tuple type of variable which is holding the int and nested tuple as value.

Here, we use the index() as method in In[16]: it will take a 2 as value and return the output as 1 which means the index of the value , it is showing of the Out[16]:

Press enter or click to view image in full size

In index() method if we put the value is not present in tuple it will raise the ValueError .

Press enter or click to view image in full size

3. Boolean Data Type in python:

True and False are the two default values for the Boolean type. False can be represented by the 0 or the letter “F,” while true can be represented by any value that is not zero.

Press enter or click to view image in full size

4. Dictionary Data Type in Python :

Dictionary or dict is datatype in python. Dictionaries are written with curly brackets, and have keys and values. We are going to store key and value pair of element in dict.

Here, in Dict key cannot be duplicated, however the value can be duplicated . in dict key act like as index. we can not using indexing and slicing in dict, however we have key in dict.

syntax:

  • variable = {}
  • variable = dict()

Example :

  • a1 = {“a”: 1, “b”: 2, “c”: 3}
  • here the keys are a,b,c and the values are 1,2,3
Press enter or click to view image in full size

We can not use the duplicate key in dictionary, whenever we using the duplicate key in dict then it will always pickup a latest one . whereas we can use the dublicate value.

Press enter or click to view image in full size

We discussed Dict built-in functions in our another article. You can refer to my article using the following link.

https://medium.com/@wvrushali27/built-in-dictionary-method-in-python-576d1aeb85ac

5. Set Datatype in Python:

Set is the one of the datatype in python. set is nothing but the collection of unique value. We can not use the duplicate value inside the set, even if try it it will just store a unique value. We can modify set number but we can create a new set from one. Set is unorder collection of data.

Syntax :

variable_name = {values}

or

variable_name = set()

Example :

s1 = {1, 2, 3, 5, 4, 3, 7, 8, 2, 9}

In above example s1 is a set type of a variable it’s stored integer as value, and set do not store a dublicate value, it only store a unique value.

Press enter or click to view image in full size

Here, s4 is set . s4 will contain a only three elements & the elements are 1,2,3. It will discard all the duplicate elements.

We discussed Set built-in functions in my another article. You can refer to my article using the following link.

https://medium.com/@wvrushali27/built-in-set-function-in-python-6c0117422a0e

--

--