Expressions By High School Technology Services myhsts.org Objective

● Unary and Binary Operations ● Comparison and Boolean Operations ● Conditional Expressions ● Lambda Expressions ● and Operator Evaluation ● Expression Lists ● Assignment Operations Expressions in Python

Most statements (logical lines) that you write will contain expressions. A simple example of an expression is 2 + 3. An expression can be broken down into operators and .

Operators are functionality that do something and can be represented by symbols such as + or by special keywords. Operators require some data to operate on and such data is called operands. In this case, 2 and 3 are the operands Python Arithmetic Operations

Assume that we have 2 variables in python a=2 and b=4. Below are the basic arithmetic operations that can be performed. ● (+) - Adds values of either side of the operator a + b =6 ● Subtraction (-) - Subtracts right hand from left hand operand b - a =2 ● Multiplication (*) - Multiplies values on either side of the operator a * b = 8 ● Division(/) - Divides left hand operand by right hand operand b/a = 2 ● Modulus(%) - Divides left hand operand by right hand operand and returns remainder b%a = 0 ● Exponent(**) - Performs exponential (power) calculation on operators a**b= 16 As can be seen above, these operations are performed on 2 operands hence these are referred to as binary operations Python Arithmetic Operations Contd.

Unary operators in Python uses just one operand and returns the result. + and - binary operators can also be used as unary operators when is performed on just one operand. Below are few examples of performing unary operations. >>>a=3 >>>print(a) 3 >>>a=+a #Performs + >>>print(a) 3 >>>a=-a #Performs unary operation - >>>print(a) -3 Boolean Operations

A boolean expression (or logical expression) evaluates to one of two states true or false. Python provides the boolean type that can be either set to False or True. Many functions and operations returns boolean objects. The not operator can be used to invert the boolean type i.e convert true to false or vice versa Every object has a boolean value. The following elements are false: ● None ● False ● 0 (whatever type from integer, float to complex) ● Empty collections: “”, (), [], {} ● Objects from classes that have the special method __nonzero__ ● Objects from classes that implements __len__ to return False or zero All the other objects are true. Comparison Operations

These operators compare the values on either sides of them and return boolean type i.e true or false. Assume variable a=2 and b=4. Below are few comparison operators: ● == compares values of one operator with another and returns either true if the values are equal. (a == b) returns false ● != returns true of the values are not equal. (a != b) returns true ● > If the value of left operand is greater than the value of right operand, then condition becomes true. ● < If the value of left operand is less than the value of right operand, then condition becomes true. ● >= Returns true if left operand is greater than or equal to the value of right operand ● <= Returns true if left operand is less than or equal to the value of right operand Conditional Expressions

Conditional expressions in Python are used to perform different computations or actions depending on whether a condition evaluates to True or False. The condition usually uses comparisons and arithmetic expressions with variables. These expressions are evaluated to the Boolean values True or False. if condition_1: Statement_block_1 # If condition_1 is true, execute Statement_block_1 elif condition_2: Statement_block_2 # If condition_2 is true, perform Statement_block_1 else: Statement_block_3 # Else perform Statement_block_3 Lambda Expressions

The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created. The general syntax of a lambda function is : lambda argument_list: expression The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name. eg. The following example of a lambda function returns the sum of its two arguments: >>> f = lambda x, y : x + y >>> f(1,1) 2 Order of Operations and Operator Evaluation

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. Python follows the same precedence rules for its mathematical operators that mathematics does.

● Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. ● Exponentiation has the next highest precedence, so 2**1+1 is 3 and not 4, and 3*1**3 is 3 and not 27. ● Multiplication and both division operators have the same precedence, which is higher than addition and subtraction, which also have the same precedence. So 2*3-1 yields 5 rather than 4, and 5-2*2 is 1, not 6.

. Order of Operations and Operator Evaluation Contd.

● Operators with the same precedence (except for **) are evaluated from left-to-right. In algebra we say they are left-associative. So in the expression 6-3+2, the subtraction happens first, yielding 3. We then add 2 to get the result 5. If the operations had been evaluated from right to left, the result would have been 6-(3+2), which is 1 Assignment Operator in Python

Assignment operators are used in Python to assign values to variables. a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.

There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5. Assignment Operator in Python contd. Assignment

Write a single python program which uses all types of operators discussed in this chapter.