**Data Handling in Python**

**Topics are discussed:**

**Operators:**arithmetic operators, relational operators, logical operators, assignment operator, augmented assignment operators, identity operators (is, is not), membership operators (in, not in), precedence of operators**Expressions, statement, type conversion**: expression, evaluation of an expression, python statement, type conversion (explicit & implicit conversion)**Debugging / Errors**: syntax errors, logical errors, runtime errors

**Operators **

An operator is used to perform specific mathematical or logical operation on values.

**Operand **

The values that the operator works on are called operands.

**For example,**

>>> 10 + num,

- 10 & num are operands and
- the + (plus) sign is an operator.

**Arithmetic Operator**

Operator | Description |

+ (unary) | Explicitly express a +ve number. Example : +3, +98.36 |

– (unary) | Explicitly express a –ve number. Example : -3, -369.36 |

+ (binary) | Add two numbers, Example : 9 + 6 is 15 |

– (binary) | Subtract one value from another number. Example: 9 – 3 is 6 |

* | Product of two numbers. Example 9 * 6 is 54 |

/ | Gives quotient, when one value is divided by the other. Example 9/ 6 is 1.5 |

// (floor division) | Gives integer part of quotient, when one value is divided by the other. Example 9//6 is 1. |

% (Remainder) | Gives remainder when one value is divided by the other. Example 9 % 6 is 3 |

** (Exponent) | To raise a number to the power of another number. Example, 3**2 is 9 |

**Arithmetic Operators | www.mycstutorial.in**

**Assignment Operator**

Operator | Description |

= | Called Assignment operator. Assign a value to the variable. Example, x = 20. |

+= | Add and Assign, Add the R-Value to the L-Value and assign result to L-value. Example x=20, x += 30 it means x = x + 30 so the new value of x is 50. |

-= | Subtract and Assign, Subtract the R-Value from L-Value and assign result to L-value. Example x=50, x -= 30 it means x = x – 30 so the new value of x is 20. |

*= | Multiply and Assign, Multiply the R-Value with L-Value and assign product to L-value. Example x=50, x *= 3 it means x = x * 3 so the new value of x is 20. |

/= | Divide and Assign Quotient, Divide the L-value with R-Value and assign quotient to L-value. Example, x = 50, x /= 6 its means x = x / 6, and the new value of x is 8.33333…… |

//= | Floor and assign, Divide the L-value with R-Value and assign integer quotient (floor division) to L-value. Example, x = 50, x //= 6 its means x = x // 6, and the new value of x is 8. |

%= | Divide and Assign Remainder, Divide the L-value with R-Value and assign remainder to L-value. Example, x = 50, x %= 6 its means x = x % 6, and the new value of x is 2. |

**= | Exponent and Assign, Calculate (L-Value)^{R-Value} and assign the result to L-value. Example, x = 3, x**=2 its means x = x^{2 }and the result of x is 9 |

**Assignment Operator | mycututorial.in**

**Relational Operator**

Operator | Description |

== (Equality) | Compares two values for equality, Returns True if they are equal, otherwise returns False. |

!= (Not equal) | Compares two values for inequality, Returns True if they are not equal, otherwise returns False. |

< (Less than) | Compares two values, Returns True if first value is less than the second, otherwise returns False. |

> (greater than) | Compares two values, Returns True if first value is greater than the second, otherwise returns False. |

<= (Less than or equal to) | Compares two values, Returns True if first value is less than or equal to the second, otherwise returns False. |

>= (greater than or equal to) | Compares two values, Returns True if first value is greater than or equal to the second, otherwise returns False. |

**Relational Operator | mycstutorial.in**

**Logical Operator**

Operator | Description |

not | Negate a condition and Returns True if the condition is false otherwise returns False. |

and | Combines two conditions and returns True if both the conditions are true, otherwise returns False. |

or | Combines two conditions and returns True if at least one of the condition is true, otherwise returns False. |

**Logical Operator | mycstutorial.in**

**Bitwise Operator**

Operator | Description |

& (bitwise and) | The AND (&) operator compares two bits and generates a result of 1 if both bits are 1 otherwise, it returns 0 |

| (bitwise or) | The OR (|) operator compares tow bits and generate a result 1 if the bits are complementary, otherwise it returns 0. |

^ (bitwise xor) | The EXCLUSIVE-OR (XOR) operator compares two bits and return 1 if either of the bits are 1 and it gives 0 if both bits are 0 or 1 |

~ (bitwise complement) | The COMPLEMENT operator is used to invert all of the bits of the operand. |

**Bitwise Operator | mycstutorial.in**

**Note: bin( ) returns binary representation number.**

**Membership Operator**

Operator | Description |

in | Returns True if the given value is available in the sequence (like sting, tuples, lists, dictionary, etc.) otherwise returns False. |

not in | Returns True if the given value is not available in the sequence (like sting, tuples, lists, dictionary, etc.) otherwise returns False. |

**Membership Operators | www.mycstutorial.in**

**Identity Operator**

Operator | Description |

is | Returns True, if both operand have same identity i.e. memory reference , otherwise return False |

Is not | Returns True, if both operand have not same identity i.e. memory reference , otherwise return False |

**Identity Operators | www.mycstutorial.in**

**Operator Precedence : **

Execution of operator depends on the precedence of operator.

Order | Operator | Description |

Highest | () | Parentheses |

↑ | ** | Exponentiation |

| | ~ | Bitwise nor |

| | +, – | Unary Plus and Minus |

| | *, /, //, % | Multiplication, Division, floor division , remainder |

| | +, – | Addition, Subtraction |

| | & | Bitwise and |

| | ^ | Bitwise XOR |

| | | | Bitwise OR |

| | <, <=, >, >=, <>, !=, ==, is, is not | Relational Operators, Identity Operators |

| | not | Boolean NOT |

↓ | and | Boolean AND |

Lowest | or | Boolean OR |

**Expression**

In Python, An expression is a valid combination of operators, literals and variables.

There are three types of Expression in Python, These are-

•**Arithmetic Expressions :-**** ** combination of integers, floating-point numbers, complex number, and arithmetic operators. Example ; 2+9, 9.2/3

•**Relational Expression :-** combination of literals and/or variables of any valid type and relational operators . Example; x > y, 5 < 8, z == y

•**Logical Expression:-** combination of literals, variables and logical operators. Example; t and q, p or g

•**String Expression :-**** **combination of string literals and string operators ( + -> concatenate, and * -> replicate). Example;

“Hello” + “Anjeev” = “Hello Anjeev”

“Like” * 3 = “LikeLikeLike”

**Types Casting / Explicit Type Conversion**

The conversion of one type to specific type explicitly, called Type Casting / Explicit Type Conversion.

With explicit type conversion, there is a risk of loss of information since we are forcing an expression to be of a specific type.

Syntax: <type>( )

Example x = int ( “123”)

y = float(“5369.36”)

z = str(598698.36)

Functions in Python that are used for explicitly converting an expression or a variable to a different types are –

- int(x) – Converts x to an integer
- float(x) – Converts x to a floating-point number
- str(x) – Converts x to a string representation
- chr(x) – Converts x to a character
- unichr(x) – Converts x to a Unicode character

**Program : Explicit type conversion from int to float**

num1 = 10 num2 = 20 num3 = num1 + num2 print(num3) print(type(num3)) num4 = float(num1 + num2) print(num4) print(type(num4))

**Output:**

30 <class 'int'> 30.0 <class 'float'>

**Program : Explicit type conversion from float to int**

num1 = 10.2 num2 = 20.6 num3 = (num1 + num2) print(num3) print(type(num3)) num4 = int(num1 + num2) print(num4) print(type(num4))

**Output:**

<class 'float'> 30.8 <class 'int'> 30

**Implicit Conversion**

Implicit conversion, also known as coercion, happens when data type conversion is done automatically by Python and is not instructed by the programmer. **Program to show implicit conversion from int to float.**

num1 = 10 # num1 is an integer num2 = 20.0 # num2 is a float sum1 = num1 + num2 # sum1 is sum of a float and an integer print(sum1) print(type(sum1))

**Output:**

30.0 <class 'float'>

**Que : Why was the float value not converted to an integer instead? **

**Ans :** This is due to type promotion that allows performing operations (whenever possible) by converting data into a wider-sized data type without any loss of information.

**Debugging (Process of Identifying and Removing Errors)**

**Bug / Errors **– The mistake (error) and problem found in the program is called Bug.

**Debug** – Removing the bug / error from the program is called Debug.

**Debugging **– The process of Identifying and removing errors from the program is called debugging.

**Types of Errors **

Errors occurring in programs can be categorised as:

i) Syntax errors

ii) Logical errors

iii) Runtime errors

**Syntax Errors:**

Python has its own rules that determine its syntax. The interpreter interprets the statements only if it is syntactically (as per the rules of Python) correct. If any syntax error is present, the interpreter shows error message(s) and stops the execution.

For Example : 10 = X

**Logical Errors**

A logical error is a bug in the program that causes it to behave incorrectly. A logical error produces an undesired output but without abrupt termination of the execution of the program.

For Example : Instead of +, you writing – operator.

Total = X – Y

Runtime Error

A runtime error causes abnormal termination of program while it is executing. Runtime error is when the statement is correct syntactically, but the interpreter cannot execute it. Runtime errors do not appear until after the program starts running or executing.

x = int(input(“enter number”))

You entered here 25.69. It is a run time error.

Divide by a zero is also called runtime error.

- Computer System: Overview – Notes
- Number Systems & Encoding Scheme – Notes
- Boolean Logics – Notes
- Emerging Trends – Notes
- Introduction to Problem Solving – Notes
- Getting Started with Python – Notes
- Python Fundamentals – Notes
- Data Handling – Notes
- Flow of Control (Selection Statement, Iterative Statement and Jump Statement) – Notes
- Strings Manipulation in Python
- List Manipulation – Notes
- Tuples Manipulation in Python Notes
- Dictionary Handling in Python – Notes
- Sorting Techniques in Python – Notes
- Introduction to Python Module – Notes
- Society, Law, and Ethics: Societal Impacts – Notes