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.                                      ...

C tutorial for complete beginners(intro to C language)

     A ny programming language has four important aspects viz * The way it stores data * The way it operates upon this data * The way it accomplishes input and output * The way lets u control the sequence of execution of program.  What is C?  C was developed by Ritchie in 1972 at AT and T's Bell laboratories, USA. Interestingly no one pushed it and there was no advertisement made but still programmers of the time preferred it over the older ones like FORTRAN. It was popular due to its simplicity, reliability and it's ease.  Inventor of C language,   Dennis Ritchie Why Still C in 2019?  As per today's opinion that C has been superseded by C++, C# and java one must still start his programming journey with C due to following reasons: 1. C++and C# use object oriented programming and basic programming skills         of C.  2. Mobile devices are today's rage and device like watches, ...

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...