Posts

Showing posts from 2020

Python 4.4 - Points to remember

  Points to remember ·          Operators are symbols used to perform some operations on operands. For example, in statement z=x+y, ‘+’ and ‘=’ are operators and ‘x’, ‘y’ and ‘z’ are operands. Addition operator is used here to add two operands ‘x’ and ‘y’ and assignment ‘=’ operator is used to assign the result of addition to operand ‘z’. ·          Operators which operate upon single operand are called Unary operators such as unary ‘+’ and unary ‘-’. For example, -5, -6.8 ·          Operators which operate upon two operands are called Binary operators such as addition, subtraction etc. For example, 5+7, 45-23, x+y, x/y ·          Arithmetic operators perform mathematical calculations such as addition, subtraction, multiplication, division. Modulus, floor division and exponentiation. ·          Assignment operator ‘=’ is used to assign a value to the left side variable. ·          There are compound assignment operators such as ‘+=’, ‘-=’, ‘*=’, ‘/=’, ‘//=’, ‘%=’ and ‘**=’

Python 4.3 - Operator precedence and associativity

  Operator precedence and associativity We can easily solve a simple statement like x=5+2 where only one or two operators (‘=’ and ‘+’) are used. Because we know that, first we will add 5 and 2 i.e. would use ‘ +’ operator and then store the result 7 into ‘x’ i.e. use ‘ =’ operator. So when a statement or expression contains one or two operators then it becomes easy for us to solve such statement but how would you solve if a statement contains many operators together? For example:   x=35+6/3-(2+2)*(5-3)**4//2+2.2   There are many operators used in a single statement. You might be confused by seeing the statement. You would think that just execute the statement as it is written i.e. first add 35 and 6 then divide the result by 3 then add 2 and 2 and so on. But it does not work like this. When multiple operators are used in a single statement then it must be known which operator will be executed first, which will be executed next and so on. The process of deciding that whic

Python 4.2 - Other operators

Other operators Python provides two other operators called Membership and Identity operators which are used for some different purposes. Let’s understand these operators one by one. 1.    Membership operators Membership operators are used to check the membership of a value or object in a sequence such as list, string, tuple, dictionary. We can check, if a particular value is available in a sequence or not. For example, we can check whether number 4 is available in the list [1,2,3,4,5]. This checking is called membership i.e. we can check whether number 4 is a member of the sequence. There are two types of membership operators – ·          in ·          not in in operator ‘in’ operator returns True if the value or element is a member of a sequence i.e. it returns True if an element is found in a sequence such as list, string etc. ‘ in’ operator is mostly used with for loop . Syntax to use ‘ in’ operator is – value in sequence The above syntax represents that we want t

Python 4.1 - Operators

We all know some basic mathematics like addition, subtraction, multiplication and division. These all are operations which are also performed on data in a program. A program requires data to perform operations like addition, subtraction etc. And we know that data is stored into variables. So we can perform operations on those data or variables. So how do we perform operations on data? Operations are performed on data by using operators. For example, in statement ‘x + y’, we are performing an addition operation on variables ‘x’ and ‘y’. Here symbol ‘+’ is called an operator which is used to perform an addition operation. And ‘x’ and ‘y’ are called variables or operands. So operators are symbols (such as +, -, *, % etc.) which are used to perform operations on operands.   z = x + y – x Here z, x and y are operands or variables and ‘+’, ‘-’ and ‘=’ are operators. There are many types of operators available in Python which are used to perform different types of operations on data. We