datatype :-
Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
There are various data types in Python.
Python Numbers
Number data types store numeric values. Number objects are created when you assign a value to them.
Integers, floating point numbers and complex numbers fall under Python numbers category. They are defined as int, float and complex classes in Python.
Note : We can use the type() function to know which class a variable or a value belongs to. Similarly, the isinstance() function is used to check if an object belongs to a particular class.
Integers can be of any length, it is only limited by the memory available. A floating-point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points. 1 is an integer, 1.0 is a floating-point number. Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.
Python Complex Number:-
Python supports complex numbers and has a datatype called complex. A complex number contains a real part and imaginary part.
We can initialize a complex number in two ways. The first process is using complex() function. The second is initializing the variable with the actual complex number.
output :-
(5+6j)complex() function takes real part and imaginary part as arguments respectively. While assigning the actual complex number to a variable, we have to mention j next to the imaginary part to tell Python that this is the imaginary part.
Python Complex Number – type()
Python List
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.
A list contains items separated by commas and enclosed within square brackets [ ].
List Example
Note :
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.
Python Tuples
Tuple is an ordered sequence of items 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.
A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
We can use the slicing operator [] to extract items but we cannot change its value.
Python Strings
Python String is an immutable 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 """
In string handling, the + operator is used to concatenate two strings as the operation "sana"+" mahtab" returns "sana mahtab".
To define a string literal, you can use single quotes, double quotes or triple quoted.
Triple quoted strings can define multiple line strings.
Python str type
Convert to String
To convert objects of other datatype to string, you can use str() function.
Python Set
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.
Python Set is the unordered collection of the data type. It is iterable, mutable(can modify after creation), and has unique elements. In set, the order of the elements is undefined; it may return the changed sequence of the element. The set is created by using a built-in function set(), or a sequence of elements is passed in the curly braces and separated by the comma. It can contain various types of values.
Example
Python Dictionary
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.
Key can hold any primitive data type, whereas value is an arbitrary Python object. The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}.
Trending Tutorials