In this tutorial we will learn about the different Datatypes in Python and some important facts about them.
A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error.
A string, for example, is a data type that is used to classify text and an integer is a data type used to classify whole numbers.
Any number you enter in Python will be interpreted as a number; you are not required to declare what kind of data type you are entering. Python will consider any number written without decimals as an integer(as in 138) and any number written with decimals as a float (as in 138.0).
Like in math, integers in computer programming are whole numbers that can be positive, negative, or 0 (…, -1, 0, 1, …). An integer can also be known as an int. As with other programming languages, you should not use commas in numbers of four digits or more, so when you write 1,000 in your program, write it as 1000.
We can print out an integer in a simple way like this:
Or, we can declare a variable, which in this case is essentially a symbol of the number we are using or manipulating, like so:
We can do math with integers in Python, too:
Integers can be used in many ways within Python programs, and as you continue to learn more about the language you will have a lot of opportunities to work with integers and understand more about this data type.
Some more important facts about float data type:
Some important points about complex data type:
String is a sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ''' or """ .Python does not have a char data type , unlike C/C++ or Java. Unlike C language , Pyhton does not uses ASCII number system for character.It uses UNICODE number system.UNICODE is a number system which supports wider range of character compared to ASCII. As far as Python is concerned, it uses UNICODE supports 655536 characters with their numeric values ranging from 0 yo 65535 which covers almost every spoken language in the world like English,Greek,Germen,Korean,Chinese etc.
Whenever we display a string value directly on python's shell i.e. without using the functio print(), python's shell automatically encloses it in single quotes.
However this does not happen when we use print() function to print a string value
If a string with double quotes , it must end with double quotes only.
Similarly if it starts with single quotes , it must end with Single quotes only.
Otherwise python will generate error.
If the string contains single quotes in between then it must be enclosed in double quotes and vice versa.
For example:
To print CodeMistic Python Tutorials we would write:
msg="CodeMistic Python Tutorials"
Similarly to print Capital of "MP" is "Bhopal", we would write:msg='Capital of "MP" is "Bhopal"'
Just like a list and tuple, the slicing operator [ ] can be used with strings.Strings, however, are immutable.
List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.
Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ] .
a = [1, 2.2, 'python']
We can use the slicing operator [ ] to extract an item or a range of items from a list. The index starts from 0 in Python.
Tuple is an ordered sequence of items the same as a list. The only difference is that tuples are immutable. Tuples once created cannot be
modified.
Tuples are used to write-protect data and are usually faster than lists as they cannot change dynamically.
It is defined within parentheses () where items are separated by commas.
t = (5,'program', 1+3j)
We can use the slicing operator [] to extract items but we cannot change its value.
Some important points about data type:
Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { } . Items in a set are not ordered.
We can perform set operations like union, intersection on two sets. Sets have unique values. They eliminate duplicates.
Since set are unordered collection, indexing has no meaning. Hence, the slicing operator [] does not work.
Dictionary is an unordered collection of key-value pairs.
It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.
In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value . Key and value can be of any type.
We use a key to retrieve the respective value. But not the other way around.