0% found this document useful (0 votes)
2 views

Data Handling Part 3.2

The document covers negative number arithmetic, augmented assignment operators, and relational operators in Python, explaining their usage and behavior. It highlights the importance of understanding how relational operators compare different data types, including strings and floating point numbers, and discusses the distinction between equality (==) and identity (is) operators. Additionally, it provides examples and principles for using these operators effectively in programming.

Uploaded by

ashugamerz1988
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Handling Part 3.2

The document covers negative number arithmetic, augmented assignment operators, and relational operators in Python, explaining their usage and behavior. It highlights the importance of understanding how relational operators compare different data types, including strings and floating point numbers, and discusses the distinction between equality (==) and identity (is) operators. Additionally, it provides examples and principles for using these operators effectively in programming.

Uploaded by

ashugamerz1988
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Data Handling

Std XI – Computer Science / Informatics


Practices
Part III
Negative Number Arithmetic in
Python
Example  -5+3 = -2 -5-3 = -8 -5*3 = -15 -
5**3= -125
>>>5//-3 >>>-5//3 >>>-
7/4
-2 -2 -1.75
>>>-7//4 >>>-7%4 >>>7%-4
>>>7//-4
-2 1 -1 -2
Augmented Assignment
Operators
Example  a=a+b can be written as a+=b
Relational Operators
Relational refers to the relationships that values or
operands can have with one another.

Python provides 6 relational operators for comparing


values ,thus called as comparison operators.

If comparison is true it results into Boolean value True.

If comparison is false it results into Boolean value


False.
The six relational operators are:

< less than <= less than or equal to ==


equal to

> greater than >= greater than equal to !


= not equal to

 Relational operators work with nearly all types of data in


Python, such as numbers, strings, lists, tuples etc.
Relational operators work on the following principles:

1) For numeric types, the values are compared after removing trailing
zeros after decimal point from a floating point number.

Example  4 and 4.0 will be treated as equal.

2)Strings are compared on the basis of lexicographical ordering


(ordering in dictionary)

 Capital letters are considered lesser than small letters eg. ‘A’ is less
than ‘a’

Example ‘Python’ is not equal to ‘python’


‘book’ is not equal to ‘books’
Lexicographical ordering is implemented via the
corresponding codes or ordinal values (eg, ASCII code
or Unicode code) of characters being compared.

That is the reason ‘A’ is less than ‘a’ because ASCII


value of ‘A’(65) is less than ‘a’(97)

To check the ordinal code of a character use


ord(<character>) eg. ord(‘A’)

Nonprinting characters like spaces are real


characters and they have ASCII code 32
Two strings may appear same but they might
produce different results if they have spaces in
them.

Example  ‘Apple’ > ‘ Apple’  True


 ‘Apple’ == ‘ Apple’  False

Second string contains a space in the beginning


and space’s ordinal value (32) is less than A’s
ordinal value (65)
Two lists, two tuples are equal if they have same
elements in the same order.

Boolean True is equivalent to 1(numeric one) and Boolean


False to 0 (numeric zero) for comparison purposes.
Characters Ordinal/
ASCII
code
A-Z 65-90
a-z 97-122
0-9 48-57
Character Ordin
s al/
ASCII
code
A-Z 65-90
a-z 97-122
0-9 48-57
Relational Operators in Python
Check point 8.4 page 220

False
Important  You should not use == to compare two
floating point values, the reason is that floating point
numbers are stored in some precision limit and which may
result in some rounding off. All this will not yield the
correct result.

Floating point numbers are approximately presented in


memory in binary form up to allowed precision (15 digit
precision )in Python.
This approximation may yield unexpected results if you
are comparing floating – point numbers especially for
equality(==)
Numbers such as 1/3 cannot be fully represented in
binary as it yields 0.333….. And to represent it in binary
some approximation is done internally.
Relational Operators with
Arithmetic Operators
The relational operators have a lower precedence than
the arithmetic operators.
Example  a+5 > c-2 expression 1
 (a+5) > (c-2) expression 2
 a+(5>c) -2 expression 3
Here expression 1 and 2 are the same but not expression
3.

Check point 8.5 page 221


Tips of relational operators
Value1==3
(Compares Value1 with 3, will give result True
or False)

Value1 = 3 are different (Assigns 3 to Value1)


Identity operators
is , is not  Are used to check if both the
operands reference the same object memory ie.
The identity operators compare the memory
locations of two objects and returns True or False
accordingly.
Most of the times we just need to check whether
the two objects refer to the same value or not –

In this case the equality operator (==) is


sufficient for this test.

However in advanced programs or in your


projects , you may need to check whether they
refer to same memory address or not –
In this case you can use the is operator.
The is not operator is opposite of the is operator. It returns True
when both its operands are not referring to same memory address.
Equality (==)and identity (is) –
Important Relation
When the is operator returns True for two variables, it
implicitly means that the equality operator will also return
True.
Expression a is b is True means that a==b will also be
True
Example 
>>> print(a,b)
235 235
>>> a is b
True
But it is not always true other way round. That
means there are some cases where you will find that the
two objects are having just the same value ie. ==
operator returns True for them but the is operator returns
False.
l

l
The reason behind this behaviour is that there are
a few cases where Python creates two different
objects even though both store the same value.
These are:

1. input of strings from the console.


2. Writing integer literals with many digits (very
big integers)
3. Writing floating point and complex literals

You might also like