The document provides an overview of expressions in Python, detailing the types of expressions such as arithmetic, relational, logical, and string expressions, along with their evaluation rules. It explains implicit type conversion, type casting, and how Python handles operations involving different data types. Additionally, it includes examples and Boolean expressions to illustrate the concepts discussed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views
Data Handling Part 5
The document provides an overview of expressions in Python, detailing the types of expressions such as arithmetic, relational, logical, and string expressions, along with their evaluation rules. It explains implicit type conversion, type casting, and how Python handles operations involving different data types. Additionally, it includes examples and Boolean expressions to illustrate the concepts discussed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29
Data Handling
Std XI – Computer Science / Informatics
Practices Part 5 Expressions An expression in Python is composed of one or more operations, with operators, literals and variables. OR A valid combination of atoms and operators forms a Python expression.
Atoms in Python : identifiers, literals values in quotes (‘
‘), parentheses, brackets ,etc, ie. strings, tuples, lists, dictionaries ,sets, etc. An atom is something that has a value. The type of operator and operands used in an expression determine the expression type. The expressions in Python can be following type: 1. Arithmetic expressions 2. String expressions 3. Relational expressions 4. Logical expressions Compound expression a+b > c**d or a*b < c*d (which involves arithmetic, logical, relational operators) 1. Arithmetic Expression – It involves numbers (integers, floating-point numbers, complex numbers) and arithmetic operators.
2. Relational Expressions – An expression having
literals and/or variables of any valid type and relational operators is a relational expression. Example x>y y<=z z!=x z==q x<y>z x==y!=z 3. Logical Expressions - An expression having literals and/or variables of any valid type and logical operators is a logical expression. Example a or b b and c a and not b not c 4. String Expressions – Python has two strings operators + and * , when combined with string operands and integers, form string expressions.
+ (concatenation operator) – The operands should be
of string type only. * (replication operator) – The operand should be one integer and one string.
Example “and” +”then” andthen
“and” * 2 andand Evaluating Arithmetic Expressions To evaluate an arithmetic expression (with operator and operands), Python follows these rules:
Determine the order of evaluation in an expression
considering the operator precedence.
Performs any implicit conversions(eg. Promoting int to
float or bool to int for arithmetic on mixed types.)
Compute its result based on the operator.
Implicit type conversion(Coercion) An implicit type conversion is a conversion performed by the compiler without programmer’s intervention. It is applied whenever differing data type are intermixed in an expression so as not to lose information. Here , if both arguments are standard numeric types, the following coercions are applied:
1. If either argument is a complex number, the other is
converted to complex.
2. Otherwise, if either argument is a floating point
number, the other is converted to floating point;
3. No conversion if both operands are integers.
Example Expression 1 will be internally evaluated as: ((ch + i) / db) ((5 + 2)) / 5.0 =(7) /5.0 [int to floating point conversion] =7.0 /5.0 A= 1.4 Expression 2 will be internally evaluated as: (((fd / db) * ch) / 2) =(((36.0 /5.0) * 5 /2) [no conversion required] =((7.2 * 5) /2) [integer to floating point conversion] =((7.2 * 5.0) /2) =(36.0 /2) [integer to floating point conversion] =36.0 /2.0 B =18.0 Output 1.4 Evaluating Relational Expressions(Comparisons) All comparison operations in Python have the same priority , which is lower than that of any arithmetic operations.
All relational expressions(comparisons) yield Boolean
values only ie. True or False. Chained expressions like a<b<c have the interpretation that is conventional in mathematics ie. comparisons in Python are chained arbitrary eg. a<b<c is internally treated as a<b and b<c
For chained comparison like x<y<=z(which is
internally equivalent to x<y and y<=z), the common expression (the middle one, y here) is evaluated only once and the third expression(z here) is not evaluated at all when first comparison (x<y here) is found to be false. Example What will be the output of following statements when the inputs are: 1) a= 10, b = 23, c = 23 2) a = 23, b= 10, c = 10 print(a < b) print(b <= c) print(a < b<=c)
Output True Output False
True True True False How would following relational expressions be internally interpreted by Python? 1) p>q<y 2) a<=N<=b
Solution 1) (p>q) and (q<y) 2)(a<=N) and (N<=b)
Evaluating Logical Expressions Logical operators and , or, not makes a logical expression. While making logical expressions. Python follows these rules: 1. The precedence of logical operators is lower than the arithmetic operators, so constituent arithmetic sub- expression(if any ) is evaluated first and then logical operators are applied. Example 25/5 or 2.0 + 20/10 5 or 4.0 5 2. The precedence of logical operators among themselves is not, and, or. So the expression a or b and not c will be evaluated as : (a or (b and (not c)))
Expression p and q or not r will be evaluated as:
((p and q) or (not r)) 3. Important While evaluating , Python minimizes internal work by following these rules:
i) In or evaluation, Python only evaluates the second
argument if the first one is falsetval. 2. In and evaluation, Python only evaluates the second argument if the first one is truetval. Example 1. (3<5) or (5<2) Since (3<5) is True , the overall result is True and the second expression will not be evaluated. 2. (5<3) or (5<2) Since (5<3) is false, the second argument is evaluated , the overall result is of the second argument. 3. (3<5) and (5<2) Since (3<4) is True , it will now evaluate the second expression (5<2) and the result of second expression is returned as the overall result. 4. (5<3) and (5<2) Since first argument (5<3) is False , the first argument’s result is returned as overall result, the second argument (5<2) is not evaluated. What will the output of following expressions? (5<10) and (10<5) or (3<18) or not 8<18 Output True ‘Divide by 0’ is an undefined term. Dividing by zero causes an error in any programming language, but when following expression is evaluated in Python, Python reported no error and returned the result as True. Could you tell why? (5<10) or (50 <100/0)
Solution In or evaluation python will test the first
expression, and if the result is True will not evaluate the second expression so in the above example the second expression is not evaluated at all. Type casting Python internally changes the data type of some operands so that all operands have same data type. This is known as implicit type conversion. Python also supports explicit type conversion.
Explicit type conversion is user-defined conversion that
forces an expression to be of specific type. This is known as Type Casting. Type casting in Python is performed by <type>() function of appropriate data type. Example <datatype>(expression) If (a = 3, b=5.0 then) int(b) will type cast the expression as int ie. 5 d= float(a) will assign value 3.0 to d Python offers some conversion functions that you can use to type cast a value in Python. Type casting issues Assigning a value to a type with a greater range (eg. From short to long) poses no problem, however, assigning a value of larger data type to a smaller data type (eg. From floating-point to integer)may result in losing some precision. Floating point type to integer conversion results in lose of fractional part. Example Which of the following expressions will yield a Boolean type value as its output? i) 10 > 2 ii) 2 != 4 iii) (2 + 5, 6 + 6) iv) 5 > 2 and not (10 >11)