Data Types In Python and It’s Example

1
1845
Data Type's In Python and It's Example

Introduction to Python Data Types

Python is a general-purpose programming language that is becoming ever more popular for data science, to learn a python we need to start by knowing what are the data types in python. Before digging into deep we need to know…

What is the data type?

Variables are used to hold values of different data types. Python is called dynamically typed language hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value
with its type. Python enables us to see the sort of variable utilized in the program. Python provides us the sort () function which returns the type of the variable passed.

Python provides various standard data types that outline the storage method on each of them.

Given below are the Data Types in Python.

List, String, Tuple, Number, Dictionary are Data Types In Python.

Data types in Python.

  1. Numbers (Integer)
  2. String
  3. List
  4. Tuple
  5. Dictionary

Number(Integer)

A number is used to store numeric values. Python creates Number objects when variety is assigned to a variable.

For example:

a = 3 ,
b = 5 # here a and b are number objects in which we pass 3 and 4

Python supports 4 types of numeric data.

Int

(signed integers like 13, 2, 29, etc.)

Long

(long integers used for a higher range of values like 90809080L, -0x192929L, etc.)

Float

(float is used for floating-point numbers like 1.9, 9.902, 15.2, etc.)

complex

(complex numbers like 2.14j, 2.0 + 2.3j, etc.)

Important note

  • Python allows us to use a lower-case L to be used with long integers.
  • However, we should use an upper-case L to avoid confusion.
  • A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts respectively).

String

The string can be defined as the sequence of characters represented in the quotation(“ ”) marks. We will use single, double, or triple quotes to define a string in Python.

Single Quote string:

String handling in python is a straight forward task since there are various inbuilt functions and operators provided.

For example:

• A= ‘Fireblaze’
Here A is having a single word Fireblaze so here we pass only single Quote.
• B=”Kartik’s friend”
Here B is having the ‘s after a Kartik if we use a single quote the word will phrase will end there for we use double quote.
• C= ”’His friend’s,”laptop is good””’
Here we use a double quote inside the statement for that we have to pass a triple quote in such situation.

The following example shows the string handling in python.

str1 =’DataScience’
 str2 = ‘Python’
 print (str1[0:2]) #printing first two characters using the slice operator
 print (str1[4]) #printing 4th character of the string
 print (str1*2) #printing the string twice
 print (str1 + str2) #printing the concatenation of str1 and str2
 
Widget not in any sidebars

Output:

Da
 S
 Data science data science
 Data Science Python

List

Lists are similar to arrays in C. However; the list can contain data of various types. The items stored in the list are separated with a comma (,) and enclosed within square brackets []. We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.

Consider the following example:

list = [1, 2] Here we create a list of two numbers 1,2
 print (l) here we print the variable list
 print (l + l) here we add the same variable.

Output:

[1,2]
 [1,2,1,2]

Tuple

A tuple is similar to the list in many ways. Like lists, tuples also contain the gathering of the things of various data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().
A tuple may be a read-only arrangement as we will not modify the dimensions and value of the things of a tuple.

example of the tuple.

Tuple1 = ('SQL', 'Python', 100, 200);
 Tuple2 = (1, 2, 3, 4);
 print ("tuple1[0]: ", tuple1[0])
 print ("tuple2[1:4]: ", tuple2[1:4])

Output:

tuple1[0]: SQL
 tuple2[1:5]:  [2, 3, 4]

Dictionary

Dictionary is an ordered set of a key-value pair of things. It is like an associative array or a hash table where each key stores a selected value. Key can hold any primitive data type and value is an arbitrary Python object.
The items within the dictionary are separated with the comma and enclosed within the curly braces {}.

 
Widget not in any sidebars

For example:

dict1 = {1: 'a', 2: 'b'}
 dict2 = {'c': 3, 'd': 4}
 dict3 = {'N': 'Nagpur', 'M': 'Mumbai'}

Here are three dictionaries and if I want to call the second dictionary.

Write in a cell the name of the dictionary is:

dict1

o/p

{1: ‘a’, 2: ‘b’}

Conclusion

In this blog, we covered all the information about python data types and its uses which helps you to build a block in your python career.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here