Python: Operator & Strings
Alright, buckle up, because we're about to dive deeper into the fascinating world of Python operators and strings! You've laid a fantastic foundation, like setting up all the pieces on a chessboard. Now, let's make some strategic moves and see how these concepts play out in the real game of coding.
OPERATORS: What comes to mind when you talk about operators? I can assume that one thing will be common to all that it performs: operators are something which perform some fixed operation or they’re special keywords which are defined to perform some task. So, in python we have seven different kinds of operators which are defined to perform different kinds of tasks which makes programming easier than ever. So, let me introduce to our first operator;
1. Arithmetic Operator: Arithmetic operator deals with all the arithmetic operations related to the numbers. These are the workhorses of python which performs all the mathematical operations. There exists several different kinds of operations;
a. Addition operator(+): This performs addition of two numbers.
b. Subtraction operator(-): This performs subtraction of two numbers.
c. Multiplication operator(*): This performs multiplication of two numbers.
d. Division operator: In division operator we have in total three different kinds of operator.
● Modulo Operator(%): This returns the remainder of division between two numbers.
● Floor Division Operator(//): This returns the integer answer(approx value) of the result of division between two numbers.
● Division Operator(/): This returns the return of division(exact value) irrespective of whether it is a float or int value. e. Exponential Operator(**): This returns the first operand raise to the second operand(Calculation power).
2. Relational Operator: Relational operator is used to find relations between two numbers. It is used to decide whether the numbers are equal or unequal to each other, or whether they're greater than or lesser than to each other. It also has different kinds of operator;
a. Equal To(==): To check whether the numbers are equal to each other or not.
b. Not Equal To(!=): To check whether the numbers are not equal to each other.
c. Greater than/Lesser than(>,<): To check which of the numbers is greater or lesser than the other number.
d. Greater than or equal to / Lesser than or equal to: I guess you can yourself understand this, as it is the combination of above operators.
3. Assignment Operator: Assignment operator by its name is clear, it is used to assign values to the variables. There are two different ways to declare values,First, a = 10; here you are assigning a value of 10 to variable a. But what if you have many variables to assign the value so we can use another way to assigning values to the variables, Second, a,b,c = 5,10,15; here we are assigning values to three different variables a,b and c and the values is assigned in order in which variables are declared. And there are other ways of declaration too like x += 1, which means x = x + 1 and there exists similar like x -= 1, x *= 1, x /=2 etc. etc. They all mean the same.
4. Logical Operator: Logical operator by name it's clear that it deals with the logic within the Algorithm, whenever writing a algorithm you come across points where you need to use decisive statements like if, if-else and it is not necessary that you have to deal with only one statement sometimes you need to use multiple statement that’s where we use our Logical operator, there are mainly three different kind of operator;
a. AND operator (and): AND operator is the operator which only becomes true when all the conditions are true even if one statement is false then the AND will result in false value. Example: if(a > 5 and a < 9): If both the conditions are true only then will this statement be executed.
b. OR Operator (or): OR operator is the operator which becomes true when any one of the conditions is true. Example: if(a > 5 or a < 9): If any one of the condition is true then the if block will get executed.
c. NOT Operator (~): NOT operator is the operator which returns the complement of the operand. Example: ~true => false
5. Bitwise Operator: Bitwise operator deals with the binary bits of number as you know that a computer only understands binary language(0 and 1) so bitwise operations are regularly performed within the computer.
6. Membership Operator: Membership operator is used to check whether a particular character or substring is part of a larger entity. We use two different statements “in” and “not in”; ‘IN’ results true when the character is the part of the larger Entity and ‘NOT IN’ results true when the character is not the part of the larger entity.
7. Identity Operator: We will discuss this in the next section of the article.
STRINGS AND IT’S MEMORY MANAGEMENT:
Strings: Strings are one of the data types and data structures of python. Wait What? I Guess everyone has the same question in their mind. So, let me introduce you to strings by definition strings are defined as a continuous chain of characters which are stored within single(‘’) or double quotes(“”). If you look at how a string is saved in the memory you will notice that it is similar to a list, as each character of the string has a particular index for it. Python also offers many inbuilt properties so that’s why we consider string a data structure in python and as its implementation internally is like a data structure. In python strings behave as immutable objects which means that you cannot make modifications in the object if you did any modification a new object will be created.
Memory Management: Whenever we create a string like str = “Welcome to Coding!” in memory a space will be occupied by the string “Welcome to Coding!” and the variable str will point towards the string. This is how strings are stored in memory at a specific address. But what if we create a variable str1 having the same string, in that case python won’t allocate a completely new space for that it will just make the str1 variable point to the same string. And to check if the variable points to the same address or not we have an operator named “IDENTITY OPERATOR” this operator checks if the variables are pointing to the same address or not.
#chaicode