Python supports various data types, these data types define the operations possible on the variables and the storage method. Below is the list of standard data types available in Python:
- Numeric:
Just as expected Numeric data types store numeric values. They are immutable data types, this means that you cannot change its value. Python supports three different Numeric datatypes.
- Integer type:
It holds all the integer values i.e. all the positive and negative whole numbers.
- Float type:
It holds the real numbers and are represented by decimal and sometimes even scientific notations with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250), .
- Complex type:
These are of the form a + bj, where a and b are floats and J represents the square root of -1 (which is an imaginary number), example — 10+6j.
- List:
You can consider the Lists as Arrays in C, but in List, you can store elements of different types, but in Array, all the elements should of the same type.
A list is the most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
- Tuples:
A Tuple is a sequence of immutable Python objects. Tuples are sequences, just like Lists. The differences between tuples and lists are:
Tuples cannot be changed unlike lists
Tuples use parentheses, whereas lists use square brackets.
So the simple answer would be, Tuples are faster than Lists. If you are defining a constant set of values which you just want to iterate, then use Tuple instead of a List.
Guys, all Tuple operations are similar to Lists, but you cannot update, delete or add an element to a Tuple.
- Strings:
Strings are amongst the most popular data types in Python. We can create them simply by enclosing characters in quotes. Python treats single and double quotes in exactly the same fashion.
- Set:
A Set is an unordered collection of items. Every element is unique.
A Set is created by placing all the items (elements) inside curly braces {}, separated by a comma.
- Union:
Union of A and B is a set of all the elements from both sets. Union is performed using | operator.
- Intersection:
Intersection of A and B is a set of elements that are common in both sets. Intersection is performed using & operator.
- Difference:
The difference of A and B (A - B) is a set of elements that are only in A but not in B. Similarly, B - A is a set of the element in B but not in A.
- Symmetric Difference:
Symmetric Difference of A and B is a set of elements in both A and B except those that are common in both. Symmetric difference is performed using ^ operator.
- Dictionary:
Now let me explain you Dictionaries with an example.
I am guessing you guys know about Adhaar Card. For those of you who do not know what it is, it is nothing but a unique ID which has been given to all Indian citizen. So for every Adhaar number, there is a name and a few other details attached.
Now you can consider the Adhaar number as a Key and the persons detail as the Value attached to that Key.
Dictionaries contain these Key Value pairs enclosed within curly braces and Keys and values are separated with ‘:’.
- Operators in Python:
Operators are the constructs which can manipulate the values of the operands. Consider the expression 2 + 3 = 5, here 2 and 3 are operands and + is called operator.
Python supports the following types of Operators:
- Arithmetic Operators:
These Operators are used to perform mathematical operations like addition, subtraction etc.
- Comparison Operators:
These Operators compare the values on either sides of them and decide the relation among them.
- Assignment Operators:
Assignment Operator is an operator used to assign a new value to a variable.
- Bitwise Operators:
These operations directly manipulate bits. In all computers, numbers are represented with bits, a series of zeros and ones. In fact, pretty much everything in a computer is represented by bits.
- Logical Operators:
1. AND
2. OR
3. NOT
- Membership Operators:
These Operators are used to test whether a value or a variable is found in a sequence (Lists, Tuples, Sets, Strings, Dictionaries) or not. The following are the Membership Operators:
1. In
2. In Not
- Identity Operators:
These Operators are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.
- Loops in Python:
In Python, there are three loops:
1. While
2. For
3. Nested
- While Loop:
Here, first the condition is checked and if it’s true, control will move inside the loop and execute the statements inside the loop until the condition becomes false. We use this loop when we are not sure how many times we need to execute a group of statements or you can say that when we are unsure about the number of iterations.
- For Loop:
Like the While loop, the For loop also allows a code block to be repeated a certain number of times. The difference is, in For loop we know the number of iterations required unlike While loop, where iterations depend on the condition.
- Nested Loops:
It basically means a loop inside a loop. It can be a For loop inside a While loop and vice-versa. Even a For loop can be inside a For loop or a While loop inside a While loop.