Data Types in Python with Examples.
What is Python?
Python is a general-purpose high level programming language, which has support of object-oriented programming.
What is Data Type?
Data Type is a crucial concept in programming.
Variables are used to store different data types. Data types are nothing but categories of values.
There are various data types in python. Some of the important types are listed below.
Data Types In Python
1. Numeric
2. String
3. Tuple
4. List
5. Dictionary
6. Set
Note: type() function is used to determine the type of data type.
Numeric
Numeric data type represents the data which has numeric values. Numeric values can be integer, floating-point numbers or complex numbers.
· Integer: Integer are used to represent positive or negative whole number values. (Without fraction or decimal)
· Float: It is used to represent decimal point values.
· Complex: Complex numbers are used to represent imaginary values. Imaginary values are denoted with “j” in python.
They are stored in form of a+bj, where a is the real part and j is the imaginary part.
A string is a sequence of characters enclosed in quotation. We can use single (‘‘), double(“ “) or triple(‘ ‘ ‘ ‘ ‘ ‘ ) quotes to define a string in python.
The characters present in a string can be any digit, letter, symbols or space. String in python is an immutable data type i.e once a value is assigned it cannot be changed later.
Syntax : stringname= ” abcd1234@#$%^ “
Example:
A tuple is a collection of homogeneous and heterogeneous values enclosed in parenthesis (). The items of tuple are separated with a comma (,). Tuple is also an immutable data type.
Syntax: tuplename= (item1, item2, item3, item4)
Examples:
A list is a collection of homogeneous and heterogeneous values enclosed in square brackets and the items of list are separated by comma (,).
A list is similar to tuple is many ways, but list is a mutable data type.
Syntax: listname= [item1, item2, item3, item4]
Example:
Set
Set is an unordered collection of data type, that is mutable and has no duplicate elements. To declare a Set in python we use curly brackets.
Syntax: setname= {item1, item2}
Examples:
Dictionary
A dictionary is a collection of key-value pairs enclosed in curly brackets.
like the set data type, a dictionary data type is unordered.
Syntax: dictionaryname= {key:Value}
Examples:
Note: In dictionary, all key-value must be unique.
(In the above example, a:30 overwrites/updates a:20.)
Conclusion
Python has a lot of data types.
In this article, we have covered few major data types that will help you understand python better.