Operators, Booleans, Tuples in Python
Tuples are the lists like sequences that store objects and numbers. The users can view the tuples as arrays or lists which are very ordered. However, unlike the lists or arrays, the values stored in them cannot be changed. Python language uses the tuples to group related entities, such as courses, students, and teachers.
These are created by merely putting all the comma-separated values in the parentheses. The values can be of any type, such as int, float, boolean, and string. Although python allows the programmers to create tuples without using parentheses, it is better to place them inside the parentheses to make the code look compact and neat.
Tup1 = (1, 2, ,3 ,”Hello”, “World”); Tup2 = 1, 2, 3, “Hello” , “World”;
Furthermore, even though the users can’t change the tuples’ values, they can create new tuples using the older ones.
Tup3 = Tup1 + Tup2
Accessing the Tuples
As the tuples store number of values in them, the user can access the specific indices, as shown in the table below. Like lists, the first index starts from ‘0,’ but unlike the lists, tuples are like a list with connected ends. If the user tries to access the negative index, the counting starts from the end. For example, -1 refers to the tuple’s last value, and -2 refers to the second last value of the tuple.
print(Tup1); //output: 1 print(Tup1[-1]); //output: World
In programming languages such as python, the operators are used to carry out logical and arithmetic computations. There is no other way for programmers to tell the program to perform these operations without the appropriate operators.
The programming languages use general symbols as operators irrespective of the programming language, such as arithmetic operators. These operators include ”+,” “-,” “*” and ”/” that performs addition, subtraction, multiplication, and division, respectively. For instance,
X = 2+3; A = 2; B = 3; X = A+ B; //same as X= 2+3, output = X =5
Here 2 and 3 are operands of the operator ‘+.’
These operators assign values to the variable. For instance, “X+=4” adds X to 4 and then assigns the X’s resulting value.
X = 3; X+=4; // X = X+4 // X = 3+4 , X = 7 X -=6, // X = X -6 , X = 7-6 X /= 4 // X= X /4 , X = 1/6
Logical operators are words and symbols which perform logical operations such as ‘and,’ ‘or’ and ‘not’ and return Boolean values. For instance, ‘and’ returns true if both operands are “True” or the same and return “False” Similarly, ‘or’ returns true if either of the operands is true and returns false if both operands are false.
A = True B = False Print ('A and B:', A and B) //output: A and B: False
These operators perform bit by bit operations on the operands. These operators treat the operands as the set of binary digits. For instance, in performing bitwise ‘AND’ or ‘&,’ the operands such as 5 and 3 are treated as ‘101’ and ‘11’
X = 10 //0000 1010 Y = 4 // 0000 0100 X >> 2 //right shift output: 2 (0000 0010) X << 2 //left shift output: 40 (0010 1000) X | Y //bitwise or output: 14 (0000 1110)
The programming languages use the comparison operators to compare the values of the operands. It also returns the true and false based on the comparison. For instance, the greater than operator ‘>’ returns true if the first operand is greater than the second one.
Comparison operators include :
- Greater than ">"
- Lesser than "<"
- Greater than or equal to "=>"
- Lesser than or equal to "<="
- Equal to "=="
- Not equal to "!="
A = 2 B = 5 Print(‘A >= B: ’ , A>=B); //output: A>=B: False
In programming languages, Boolean is a data type that assigns either true or false values to the variables. The values 0 and 1 are also booleans as they refer to “false” and “true,” respectively. The programmers use logical operators such as AND and OR operators to perform Boolean arithmetic. Moreover, the functions that have a boolean return type only return true or false. The programmers use Boolean type in such functions where the result could be only either of two values. For instance, a number can be even or odd and nothing in between.
In python, the users can use the Boolean type as shown below.
Value1 = True; Value2 = False; Value3 = Value1 and Value2 //Value3 = False
Other useful articles:
- OOP in Python
- Python v2 vs Python v3
- Variables, Data Types, and Syntaxes in Python
- Operators, Booleans, and Tuples
- Loops and Statements in Python
- Python Functions and Modules
- Regular Expressions in Python
- Python Interfaces
- JSON Data and Python
- Pip and its Uses in Python
- File Handling in Python
- Searching and Sorting Algorithms in Python
- System Programming (Pipes &Threads etc.)
- Database Programming in Python
- Debugging with Assertion in Python
- Sockets in Python
- InterOp in Python
- Exception Handling in Python
- Environments in Python
- Foundation of Data Science
- Reinforcement Learning
- Python for AI
- Applied Text Mining in Python
- Python Iterations using Libraries
- NumPy vs SciPy
- Python Array Indexing and Slicing
- Python & Libraries