Skip to main content

Python tutorial - #4 (Operators in Python)

OPERATORS IN PYTHON

The general purpose of a program is to accept data and perform some operations on the data. The data in the program is stored in variables. The next question is how to perform operations on the data. For example, when a programmer wants to add two numbers, he simply type '+' symbol. This symbol performs addition operation. Such symbols are called operators.
Operator = An operator is a symbol that performs an operation. 

When an operator acts on some variables then it is called operands. Example :- a + b in the given equation '+' operator acts on two operands 'a' and 'b'. If an operator acts on single operands then it is called unary operator. And when an operator acts on two operands then it is called binary operator. And when an operator acts on more than two operands (variable) It is called Tertiary operator.

➠Classifications of Operator --

a) Arthimatic operators
b) Assignment operators
c) Unary minus operators
d) Relational operators
e) Logical operators
f) Boolean operators
g) Bitwise operators
h) Membership operators 
i) Identity operators

Arthimatic operators

➣There are seven arthimatic operators in python: -

a) Addition (+)

b) Subtraction (-)

c) Multiplication (*)

d) Division (/)

e) Modulus (%) = This operators gives the remainder of division.

f) Exponent (**) = This operator calculates the exponential power value. 
⇨ a**b means it gives the value of a to the power of b.

g) Integer division or Floor division (//) = This operator gives the quotient of division.

When their is an expression which contains several arthimatic operators. Then we should know that which operator will be work first. Let's see that

step 1)) At first parenthesis () are evaluated.
step 2)) Then Exponentiation.
step 3)) Then Multiplication, division, Modulus, and floor division. 
             Then Addition and subtraction 
step 4)) And then at last Assignment operation is performed.

➢Let's take an example to understand this clearly -- 
⇒5+(4-2)+8*1000/2%9**2//5

⇒5+2+8*1000/2%9**2//5
⇒5+2+8*1000/2%81//5
⇒5+2+8*500%81//5
⇒5+2+8*14//5
⇒5+2+900//5 (8**14 gives huge number so we assume it as 900)
⇒5+2+180
⇒187


Assignment Operators

This operator helps to store (or assign) a value into a variable.
➢There are eight types of assignment operators:-

a) Assignment operator (=)

b) Addition assignment operator (+=)
   Here, a += 10 means 
  means  a = a+10
c) Subtraction assignment operator (-=)
Here, a -= 30
means  a = a - 30
d) Multiplication assignment operator (*=)
Here, a *= 3
means  a = a *3
e) Division assignment operator (/=)
Here, a /= 50
means  a = a/50
f) Modulus assignment operator (%=)
Here, a %= 7
means   a = a % 7
g) Exponentiation assignment operator (**=)
Here, a **= 3
means  a = a **3
h) Floor division assignment operator (//=)
Here, a //= 3
means  a = a // 3

Unary Minus Operator

You can understand this by the following example :-

Relational operator

This operator are use to compare two or more objects.

➢There are six types of Relational operator :-

a) Greater than operator (>)

b) Greater than or equal to operator (>=)

c) Lesser than operator (<)
d) Lesser than or equal to operator (<=)

e) Equals operator (==)

f) Not equals operator (!=)

Boolean Operator

a) AND operator

  Example = num1 and num2
  ➣Here, When num1 and num2 both condition becomes True then the statements related to it execute.
If only one of them becomes True and other becomes False then the statements related to it not execute.
Program - 
b) OR operator

 Example = n1 or n2
➣ Here, When n1 or n2, only one condition becomes true then the Statements related to it execute. Otherwise when both of the statements becomes true or false then the statements related to it does not execute.
Program -
c) NOT operator

Example = n1 Not n2
➣Here, When n1 and n2 return False then condition related to it is executed then it executed the statements related to it. When one of them condition return True then the statements related to it don't executed.
***----------------------------------------------------***
I will discuss next types of Operators in next Post..

You can Comment your quiery in the comment Box I will Definitely Solve your quiery and gives reply to your quiry.




Comments

Popular posts from this blog

Python tutorial - #5 (Operators in Python)

Membership Operators This operator helps us to check an element is found in a sequence or not. It gives there result in Boolean value (True or False). ➥There are two types of Membership operator :- a) in b) not in ➢in membership operator This membership operator returns True if an element is found in a sequence and it returns  False When an element is not found in a sequence. Program - #1 ➢not in membership operator This membership operator returns True if an element is not found in a sequence and it returns False if an element is found in a sequence. Program = #2 Identity Operators This operator helps us to check whether two element are same or not. Actually, this operator compares the memory location of two object, if the memory locations are same then objects are also same otherwise the objects are not same.                                      ...

Python tutorial - #3 (Docstrings and Data-Type)

Docstrings = If we write string inside triple single quote (‘’’----‘’’) or triple double quote (“””----“””) and if these strings are written as first statements in a module, function, class or a method. Then these strings are called Documentation strings or simply Docstrings. These strings are useful to create an A.P.I documentation file from python program, An A.P.I (Application Programming Interface) documentation file is a text file or HTML file that contains the description of all features of software or product. When software is created it is the duty of programmer to create docstrings file in which they explain each and every class, Module, function etc. properly. It helps the user of the product (software) to use this smoothly. Representing Binary, Octal and Hexadecimal number. ➠In python, Binary number are represented by adding 0 (zero) B or 0 (zero) b as prefix to each binary number. Example: - 0B11001, 0b00101 etc.       ...

C tutorial #lecture 3

Here you will be introduced to the comments in C and some basic arithmetic operations on variables and you will learn various shorthand operators. Let's get started- Comments in C : Comments in C/C++ and in any other programming languages serve the purpose of improvement of readability of the code.  Comments are enclosed within /*-----------*/. This  symbol is used for multiline comments. For single line comments we use  // symbol  ( two backslashes) The rules of programming language aren't applicable within comments. The text doesn't appear in the output of the code but it appears in the source code. It just makes the program readable for the coder. This is a C code to illustrate comments.  Add caption       Output.  Nesting of comments : Comments written with /*----/*---*/--------*/  can't be nested and are invalid but a comment with  /*------//------*/ can be nested.  The f...