Python Data Types

The python programming language is not a typed language like Java, C/C++, etc. However, python supports all the primitive data types like other languages.

Advertisements

Here are the list of data types supported by python programming language.

  1. integer
  2. string
  3. float
  4. boolean
  5. list
  6. dictionaries
  7. set
  8. tuple
  9. files

The first four types are primitive types and the remaining five types are user-defined types. We will not discuss each one of them in detail.

Primitive Types

The primitive types of data is common to all programming languages. Though python is not a typed language it understand the primitive types.

Integer Type

This type contains all the number or integers that include both negative and positive numbers.

For example

my_Integer = 244
print(type(my_Integer))

In the code above, we assigned an integer to variable called my_Integer. To know the type of variable, use the type() function which prints the following results.

<class 'int'>

Each type has a class in python and variables are instances of these classes.

String Type

The string type is a group of character that is enclosed within a double or single quotes. It can be a single word or a sentence or sometimes numbers are displayed as strings.

my_string = "hello"
print(type(my_string))

In the code above, we have declared and assign “hello” to the variable called my_string. You can use the type() function to verify the type of the variable.

<class 'str'>

Float Type

Python does not have a C style ‘float‘, but it has a C style ‘double‘. Any number that is a decimal number with a fractional part in python is a double precision number. You can control the fraction part in a number of ways. Here we used the format() function to control the fraction of a decimal number.

Consider the following example.

my_float = 23.4667445778967
# Method 1
# prints total 5 numbers only including fraction
print(format(my_float,'.5g') 
# Method 2
# prints 2 digits in the fraction part
print(format(my_float,'.2f') 
# check the type of my_float
print(type(my_float))

The output of the program is given below.

23.467 
23.47
<class 'float'>

The method 1 gives a result of 23.467 which has exactly 5 digits. The method2 results in 23.47 because we only allowed 2 digits in the fractional part.

Boolean Type

The python Boolean type only holds two values – True or False. The numeric value of True is 1 and 0 for False.

Advertisements

Consider the following example

my_Bool = True
print(type(my_Bool))

The output of the above program gives the type of the variable.

<class 'bool'>

User-Defined Type in Python

The user-defined type contains primitive types and it is a named type constructed by the user. We already mentioned earlier that list, dictionaries, set, tuple and files are the five user-defined types in python. Let us now discuss them in detail.

List Type

A list type is equal to an array in JavaScript which contains multiple data type. In programming language such as Java or C++ , the array is typed and therefore, contain only one type of element. The python list is flexible and supports multiple data type. Consider the following example.

my_list = ['Apple',"Oranges", 25, 55.77]
print(type(my_list))

The output of the above code, will print the data type of my_list which is list.

<class 'list'>

Dictionaries in Python

Dictionary in python is an object with name/value pair. It is similar to JavaScript declaration of object literals. Here is an example of dictionary.

# dictionary defining an employee
employee = {
  'firstName': 'Jon',
  'lastName' : 'Rambo',
  'age' : 45,
  'designation' : 'Manager',
  'salary' : 35000
}
# print the employee 
print(employee)
print(type(employee))

The output of the above program is given below.

{'firstName': 'Jon', 'lastName': 'Rambo', 'age': 45, 'designation': 'Manager', 'salary': 35000}
<class 'dict'>

Set in Python

The set is similar to a mathematical set in python. You can use a set in various ways. Here is a way to declare a set.

my_set = {"apple","oranges",33.55,99}
print(type(my_set))

The result of the above code is as follows. It prints the data type of the variable my_set.

<class 'set'>

Tuple Type in Python

Not only sets, but you can also create tuple in python. The tuple in python is unchangeable or immutable. To change the value of a tuple, first change it to list and then back to tuple.

my_tuple = ("Dog","Cat","Mice")
print(type(my_tuple))

The output is the type of the variable called my_tuple.

<class 'tuple'>

Files in Python

The python programming language allows you to manipulate files like other programming languages. Here file is treated as an object with properties and methods.

file = open("dummy.txt")
print(type(file))

The output is as follows.

================ RESTART: C:/Python_projects/file-handling.py ================
<class '_io.TextIOWrapper'>

The ‘io’ is a module in python and it helps to deal with various type of IO that the python program performs. An object from this module is called a file object or a stream object. The common types of IO performed by Python are: text IO, binary IO and raw IO. In the above program we are using a Text IO (_io.TextIOWrapper).

Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version