Topics

Python Data Types

 Python Data Types 

In any programming language data type is very important concept. Variables can store data of different types and these different types can do different things. In this article you will learn python built-in data types.    

Python has the following built-in data types.

            Numeric               : int, float, complex
            Text                      : str
            Sequence Type     : list, tuple, range
            Mapping               : Dictionary
            Boolean                : bool
            Set                        : set, frozonset
            Binary                  : bytes, bytearray

Python Data types at a glance.

Python Data Types
Python Data Types

Getting the Data Type 

you can get the data type of any object by using type() function
Example : 
print the data type of variable x 

Python Data Types



Setting the Data Type

In Python the data type is set automatically when you assign a value to a variable 
Examples 
Python Data Types






You can check the data type of following variables by yourself. 

lst = ["apple", "banana", "cherry"]  ==> Data type list  

tup = ("apple", "banana", "cherry") ==> Data type tuple       

rng = range(6)==> Data Type range       

dict = {"name" : "John", "age" : 36} ==>Data type dict 

s1 = {"apple", "banana", "cherry"} ==> Data type set  

f = frozenset({"apple", "banana", "cherry"}) ==>Data type frozenset

b = True==>Data type bool

by = b"Hello" ==>Data type bytes       

by1 = bytearray(5)==> Data type bytearray


Numeric Data Type

In python numeric data type represent the data which has numeric value. Numeric value can be integer, float and complex numbers. In python it can be defined as int, float and complex class.

Integers : This value is represented by int class , it can be positive or negative whole numbers. 
For example- 4.
Float : This value is represented by float class, it is real number with floating point representation.
For example-4.0
Complex Numbers: This is represented by complex class. it is represented as real part + imaginary part. 
For example-    3+4j

To determine the type of data type type() function is used in python.

Numeric value demonstration in python
a = 4
type(a)
<class 'int'>
You can try the same on your machine.
Python Data Types





Float value demonstration in python
a = 4.1
type(a)
<class 'float>t
Try it in your system




Complex Number demonstration in python.

c = 1 + 4j
type(c)
<class 'complex'>

check it by yourself





Sequence Data Type in Python

Sequence is the ordered collection of similar or different data types in python. Sequence allowed to stored multiples values in an organized and efficient manner. 
There are following sequence types in python:
  • String
  • List
  • Tuple
String: A string is a collection of  one or more characters put in single or double quotes. In python string is represented by str class.

In python string is created using single quote , double quotes or some time it can be created using triple quotes.
Example 1: Creating string using single quote 

String Creation in Python Using Single Quote

Example 2: Creating string using double quotes 

String Creation in Python Using Double Quotes


Example 3: Creating string using triple quotes


String Creation in Python Using Triple Quotes

List 

In python list are used to store multiple items in single variable. List is very flexible it can as the items in the lists do not necessary to be of the same type.

Syntax to creating empty/blank list in python 

Creating and printing a empty list in python

Syntax to create list with multiple values


Creating List with multiple values.




 



Access List Items
List items are indexed and can be access them by referring to the index number. index number always start with 0. 

Accessing element from list

Negative Indexing 
Negative indexing means staring from the last.-1 refer the last item and -2 refer the second last item and so on.

Negative indexing of the list

Tuple

Tuple is also an ordered collection of Python objects. Tuples are immutable means tuples cannot be modified like list once it is created. It is represented by tuple class.

Syntax to create tuple in python
Creating an empty tuple
Syntax to create tuple with multiple values
Creating tuple with multiple values
Access Tuple items 
Tuples items can also be accessed with their index number

Access tuple elements

Negative indexing of tuple
Accessing tuple elements from last

Access tuple elements from last

Mapping Data Type in Python

Python has one of the important data type called dictionary which can store multiple data values.
Dictionaries are used to store data values in key:value pairs.

key:value is provided in the dictionary to make it more optimized. Each key:value pair in a dictionary is separated by a colon: whereas each key is separated by a comma,.


Syntax to create empty dictionary 

Creating Empty Dictionary

Syntax to create dictionary with keys

Creating Dictionary with keys
Accessing Dictionaries Elements
To access the elements of a dictionary, refer to its key name. Key can be used inside square brackets. get() method can also be used to access the dictionary items.

Example 1 : Try to run below code in your console 

# accessing an element from a Dictionary Creating a Dictionary  

Dict = {'name': 'Tom', 'Roll': 5, 'salary': 2000} 
# accessing dictionary element using key 
print("Accessing an element using key:") 
print(Dict['name']) 
print(Dict['salary']) 

Output:
Accessing an element using key:
Tom
2000

#Accessing an element from Dictionary using get() method
print("Accessing an element using get method:"
print(Dict.get('name'))


Output:
Accessing an element using get method:
Tom

Boolean Data Type in Python

The Boolean data type have one of the two built-in values True or False.  It is denoted by class. 
Example : 
#python script to explain bool data type
print(type(True))
print(type(False))
Output
<class 'bool'>
<class 'bool'>

Note : First Character of "True" and "False" always should in capital else it will throw an error. 

Set Data Type in Python

In python Set is an unordered collection of data type that is mutable, iterable and has no duplicate values. Set elements are undefined but it may consist of various elements. 

Creating Set : Sets can be created with following ways : 

1. By using built-in set() function with iterable objects 

2. A sequence by placing the sequence inside middle bracket({}) separated by comma(,).

Example1 : Create and print blank set

# Creating a Set 
set1 = set() 
print("blank Set: ") 
print(set1) 

Output:
blank Set:
set()

Example2: Creating set with the string


# Creating a Set with the String
set1 = set("thisisaboy")
print("\nSet with the String: ")
print(set1)


Output :

Set with the String:
{'y', 'o', 'b', 't', 'h', 'a', 's', 'i'}



Click below link to get your Dream Job


https://optimhire.com?ref_code=jk125735