This post was released as part of the Data Science Blogathon
Introduction
In this post, you will learn about Python data types and their applications while we write Python programs in an optimized way. You will learn from what they are, its syntax and you will also see the examples to use them in Programs. Python is a language that needs no introduction. It is incredibly powerful, versatile, quick and easy to discover.
Python is one of the languages that is experiencing incredible growth and accreditation year after year. Python is an interpreted programming language that is object oriented and used for general purpose programming. In this post, we will learn about different types of data in the Python language.
What are data types?
The types of data are the classification or categorization of knowledge items. Represents the useful type that indicates what operations are often performed on specific data. Since everything is an object in Python programming, data types are classes and variables are instances (object) of those classes.
Data types are an important concept within the Python programming language.. Each value has its own Python data type, in the Python programming language. Classifying knowledge items or placing the information value in some type of data category is called Data Types. It is useful to know which silent operations are often performed with a value.
Python has six standard data types: –
- Numeric
- Rope
- List
- Double
- Put on
- Dictionary
Image source: Link
Let's analyze the above data types one by one: –
Numeric data type: –
In Python, the numeric data type represents data that has a numeric value. Numeric value can be a whole number, floating or even a complex number. These values are defined as int classes, float and complex in Python.
Integers – This data type is represented with the help of int class. These are positive or negative integers (no fraction or decimal). In Python, there is no limit to the length of the integer values.
Example:-
a = 2 print(a, "is of type", type(a)) Output: 2 is of type
Float – This type is represented by the float class. It is a real number with floating point representation. It is specified with a decimal point. Optionally, the character e or E followed by a positive or negative integer could even be added to specify scientific notation.
Example:-
b = 1.5 print(b, "is of type", type(b)) Output: 1.5 is of type
Complex numbers – Complex numbers are represented by complex classes. It is specified as (real part) + (imaginary part) j, as an example – 4 + 5j.
Example:-
c = 8+3j print(c, "is a type", type(c)) Output: (8+3j) is a type
String data type: –
The string is a sequence of Unicode characters. A string can be a collection of 1 or more characters in quotes, double quotes or triple quotes. It can be represented through a class str.
Example:-
string1= “Hello World” print(string1) output: Hello World
We can do various operations on strings like concatenation, cut and repeat.
Concatenation: Includes the operation of joining two or more chains.
Example:-
String1 = "Hello" String2 ="World" print(String1+String2) Output: Hello World
Slice: Cutting is a technique to extract different parts of a rope.
Example:-
String1 = "Hello" print(String1[2:4]) Output: cry
Repetition:
It means repeating a sequence of instructions a certain number of times.
Example:-
Print(String1*5) Output: HelloHelloHelloHelloHello
List data type: –
It forms (or create) a list placing all the items (items) in brackets [ ], separated by commas.
It can have any number of items and they may or may not be of different types (whole, floating, chain, etc.).
A list is mutable, which suggests we will modify the list.
Example:
List1 = [3,8,7.2,"Hello"] print("List1[2] = ", List[2]) Output: List1[2] = 7.2 print("List1[1:3] = ", List[1:3]) Output: List1[1:3] = [8, 7.2]
Updating the list: – we can update the list.
List1[3] = "World" #If we print the whole list, we can see the updated list. print(List1) Output: [3, 8, 7.2, ‘World’]
Tuple data type: –
A tuple is set as an ordered collection of Python objects. The only difference between tuple and list is that tuples are immutable, In other words, tuples cannot be modified after creation. It is represented by the tuple class. we can represent tuples using parentheses ().
Example:
Tuple = (25,10,12.5,"Hello") print("Tuple[1] = ", Tuple[1]) Output: Tuple[1] = 10 print("Tuple[0:3] =", Tuple[0:3]) Output: Tuple[0:3] = (25,10,12.5)
Determine data type: –
A set is an unordered collection of items. Each element of the set is exclusive (no duplicates) and it must be immutable (It can not be changed).
Example:
Set = {4,3,6.6,"Hello"} print(Set) Output: {‘Hello’, 3, 4, 6.6}
Since the set is a messy collection, indexing won't make sense. Hence the cutter operator [ ] it does not work.
Set[1] = 12 Output: TypeError
Dictionary data type: –
In Python, the dictionary is an unordered collection of data values, which is used to save data values as a map, that, unlike other data types that contain only one value as an element, a dictionary consists of a key-value pair. The key-value is provided inside the dictionary for optimization. In the representation of a dictionary data type, each key-value pair during a Dictionary is separated by a colon, while each key is separated by a “coma”.
Syntax: Key:value
Example:
Dict1 = {1:'Hello',2:5.5, 3:'World'} print(Dict1) Output: {1: ‘Hello’, 2: 5.5, 3: ‘World’}
We can retrieve the value using the following method:
Example:
print(Dict[2]) Output: 5.5
We can also update the dictionary through the following methods:
Example:
Dict[3] = 'World' print(Dict) Output: {1: ‘Hello’, 2: 5.5, 3: ‘World’}
Conclution
If you are reading this text, you are probably learning Python or trying to become a Python developer. Learning Python or another programming language begins with understanding the concepts that are a basic part of its foundation..
Hope you have already understood the various classifications of Python data types, from this text.
About the Author
Prashant Sharma
Today, I am pursuing my Bachelor of Technology (B.Tech) from the Vellore Institute of Technology. I am very excited about programming and its actual applications, including software development, machine learning and data science.
I hope you like the article. If you intend to connect with me, you can connect in:
or for any other questions, you can also send me an email
The media shown in this post is not the property of DataPeaker and is used at the author's discretion.