Basic Datatypes in Python 3
You might have heard that This is the era of Data in the IT Industry.
Today's internet and business world are full of data. Your data is being collected, processed, and used on a vast scale every day. But we need to understand What is Data? and how we can use it in our python program.
If you are interested in watching a video tutorial in Hindi, you can watch it here.
What is Data?
Before starting with DataTypes, we need to understand what is Data.
-
Data is the information that we store on our computers. It can be anything from a number to a text.
-
An example of data might be
- username
- password
- date of birth
- girlfriend's name
- 12th-grade marks
-
This information is accessible for us as humans to understand, but the computer has a specific way of handling different data types.
What are DataTypes?
The data we discussed above can be in various forms. For example, if there is a number
, we expect to be able to do mathematical operations on it.
So for the computer to know that the information is of which type and which operations we can perform on that data, we need to define a datatype.
What are the datatypes in Python?
Python is a vast and powerful programming language. It has an enormous number of data types to handle all the different kinds of data.
- int - Integer
- float - Floating point number
- bool - Boolean
- str - String
- list - List
- tuple - Tuple
- set - Set
- dict - Dictionary
Let's discuss each of these data types briefly.
All the data types are case sensitive. They need to be written in the exact same case.
int - Integer
It is used to store whole numbers. It can be positive or negative. You can perform mathematical operations on it like addition, subtraction, multiplication, division, modulus, etc.
It can be used to store values like your age
or number of students
etc.
Example of integer:
float - Floating point number
It is used to store decimal numbers. Like int
, it can be positive or negative, and you can perform mathematical operations on it.
It can be used to store values like your GPA
or your height
etc.
Example of float:
bool - Boolean
bool or Boolean is an important data type. It is used to store True
or False
values. It is highly used in conditional and iterative statements.
It is used to specify if a given condition is true or false. Like if the given password is correct or not.
Example of bool:
str - String
The string is a very important data type. It is used to store text. It is used to store words, sentences, or any other kind of text. It is written in 'single-quotes' or "double-quotes".
It can be used to store information like your name
or your address
etc.
Example of str:
list - List
list is a complex datatype. It is used to store multiple values in a single variable. List is defined by square brackets. A comma separates each element of the list. These values can be accessed by index.
The first element of the list is at index 0
. Then the second element is at index 1
and so on. If you want to access the last element of the list, you can use the negative index. For example, if you want to access the last element of the list, you can use -1
as the index.
Lists can be used to store values like your your hobbies
or your friends
etc.
The values in a list can be of any datatype
Example of list:
tuple - Tuple
Tuple is similar to list datatype. The difference is that it is Immutable. It cannot be changed. It is defined by round brackets.
Example of tuple:
set - Set
Set is a data type used to store unique values. It is defined by curly brackets. It is similar to the sets in mathematics. You can use mathematical operations on it like union, intersection, difference, etc. These are unordered and unindexed.
Sets can be used to store unique values like roll numbers
or email ids
etc.
Example of set:
dict - Dictionary
dict is a data type used to store key-value pairs. It is defined by curly brackets.
syntx:
dict can be used to store structured data like details of students
or details of employees
etc.
Example of dict:
Additional Resources
Here are some additional videos for each of the data types:
- #5 Data Types in Python & Arithmetic Operators on int - Python for Absolute Beginners 2022
- #6 Strings in Python - Python for Absolute Beginners 2022
- #7 Lists in Python - Python for Absolute Beginners 2022
- #8 Dictionary in Python - Python for Absolute Beginners 2022
- #9 Tuple, Set & Boolean in Python - Python for Absolute Beginners 2022
Conclusion
Thank you for reading this article. I hope you find it helpful, and please share it with your friends.