# Operators Data types Literals Constants Identifiers Naming Conventions with examples Basic programming Examples by unitdiploma

### Operators & Operand

•Operators are special symbols which represents computation.

•They are applied on operand(s), which can be values or variables. Operators when applied on operands form an expression.

•Operators are categorized as Arithmetic, Relational, Logical and Assignment.

•Value and variables when used with operator are known as operands.

### Mathematical/Arithmetic Operators

•Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.

Assume a=5 and b=3

Operator | Meaning | Example | Result |

+ | Addition Operator | a+b | 8 |

– | Subtraction Operator | a-b | 2 |

* | Multiplication Operator | a*b | 15 |

/ | Division Operator | a/b | 1.666 |

% | Modulus Operator | a%b | 2 |

** | Exponent Operator | a**b | 125 |

// | Integer Divison | a//b | 1 |

### Assignment Operators

•These operator are useful to store the right side value into a left side variable

Assume a=20, y=10 and z=5

Operator | Meaning | Example | Result |

= | Assignment Operator | ||

+= | Addition Assignment Operator | z+=x | z=25 |

-= | Subtraction Assignment Operator | z-=x | z=-15 |

*= | Multiplication Assignment Operator | z*=x | z=100 |

/= | Division Assignment Operator | z/=x | z=0.25 |

%= | Modulus Assignment Operator | z%=x | z=5 |

**= | Exponentiation Assignment Operator | Z**=y | z=9765625 |

### Relational Operators

• Relational operators compares the values.

• It either returns True or False according to the condition.

### Logical Operators

•Logical operators perform Logical AND, Logical OR and Logical NOT operations.

Example:

x = True

y = False

print(‘x and y is’, x and y) # Output: x and y is False

print(‘x or y is’, x or y) # Output: x or y is True

print(‘not x is’, not x) # Output: not x is False

### Special Operator:

##### Identity operators:

•is and is not are the identity operators in Python.

•They are used to check if two values (or variables) are located on the same part of the memory.

• Two variables that are equal does not imply that they are identical.

##### Membership operators:

•in and not in are the membership operators in Python.

•They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).

### Identity Operator:

Example:

x1 = 5

y1 = 5

x2 = ‘Hello’

y2 = ‘Hello’

x3 = [1,2,3]

y3 = [1,2,3]

print(x1 is not y1) # Output: False

print(x2 is y2) # Output: True

x1 and y1 are integers of same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings).

But x3 and y3 are list. They are equal but not identical. It is because interpreter locates them separately in memory although they are equal.

### Membership Operator:

•x = ‘Hello world’

•y = {1:’a’,2:’b’}

• print(‘H’ in x) # Output: True

• print(‘hello’ not in x) # Output: True

• print(1 in y) # Output: True

• print(‘a’ in y) # Output: False

‘H’ is in x but ‘hello’ is not present in x. (Python is case sensitive). Similarly, 1 is key and ‘a’ is the value in dictionary y. Hence, ‘a’ in y returns False.

### Bitwise Operator:

•Bitwise operators acts on bits and performs bit by bit operation.

•For example, 2 is 10 in binary and 7 is 111

### Data Types in Python

A data-types represents the type of data stored into a variable or memory.

•None type

•Numeric type

•Strings

•Sequences

•Sets

•Mapping

### Numeric Types

Integers, floating point numbers and complex numbers falls under Python numbers category. They are defined as int, float and complex class in Python.

•int

•float

•complex

We can use the type() function to know which class a variable or a value belongs to and the isinstance() function to check if an object belongs to a particular class.

A data-types represents the type of data stored into a variable or memory.

•None type

•Numeric type

•Strings

•Sequences

•Sets

•Mapping

### Examples Numeric Types

a = 2

print(a, “is of type”, type(a))

a = 2.0

print(a, “is of type”, type(a))

a = 1+2j

print(a, “is complex number?”, isinstance(1+2j,int))

•Integers can be of any length, it is only limited by the memory available.

•A floating point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points. 1 is integer, 1.0 is floating point number.

•Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.

### Converting the Datatypes

Sometimes, we want to convert one datatype into another. This is called type conversion or coercion. For this purpose mention the datatype with parenthesis

Ex: int(x) is used to convert it is in integer type

x=15.56

int(x) # will display 15

float(num) is used to convert it is in float type

num=15

float(num) #will display 15.0

complex(n) is used to convert it is in complex type

n=10

complex(n) #will display (10+0j)

### bool Datatypes

The bool datatype in python represents boolean values. There are only two boolean value True or False that can be represented by this datatype. A blank string like “” is also represented as False.

Ex:

a=10>5

print(a) # display True

a=6>10

print(a) # display False