Skip to main content

Operators in Python

Operators

Operator is a symbol that is used to perform specific operations like mathematical, relational or logical operations. 

There are different types of operators. Below are some the most common operators

  • Mathematical Operators.
  • Assignment Operators.
  • Relational Operators.
  • Logical Operators.

Apart from these, there are couple of more types. 

  • Identity Operators.
  • Membership Operators.

Let's have a look at each of these. 

Mathematical Operators

Mathematical operators are used to perform mathematical operations (as name states) with numeric values. 

These are operators like '+' Addition, '-' Subtraction, '*' Multiplication, '/' Division, '%' Modulus, '//' Floor Division and '**' Exponentiation.

a = 10

b = 5

c = a + b 
# Addition of two variables, Result would be c = 15

c = a - b 
# Subtraction of second variable from first variable, Result would be c = 5

c = a * b 
# Multiplication of two variables, Result would be c = 50

c = a / b 
# Division, Divides first variable with second variable, Result would be c = 2.0. Result for the division operation would always be float.

c = a % b 
# Modulus, Returns the reminder after dividing first variable with second variable. Result would be c = 0. If a = 11, b = 5, then Result would be c = 1

c = a // b 
# Floor Division, Divides first variable (a) with second variable (b) and returns the whole number only. Result would be c = 2

c = a ** b 
# Exponentiation, Calculating the first variable (a) with the power of second variable (b), Result would be c = 10000.

Assignment Operators

Assignment Operators are used to assign value to a variable. 

One of the assignment operators is '=' (equal to) to assign a value to a variable. In python, a variable is declared when a value is assigned. 

a = 10 
# This defines a integer variable 'a' and assigns a value '10'

Below are the few other assignment operators. 

*Considering the current value of 'a' as '10' for all of these examples.

'+=' Adds the value on the right side to the current value of the variable. 

a += 5 
# adds 5 to the current value of 'a' (i.e., 10) and assigns it to 'a'. This is same as 'a = a + 5'.

'-=' Subtracts the value on the right side from the current value of the variable. 

a -= 5 
# subtracts 5 from the current value of 'a' (i.e., 10) and assigns it to 'a'. This is same as 'a = a - 5'.
  
'*=' Multiplies the value on the right side with the current value of the variable. 

a *= 5 
# multiplies 5 with the current value of 'a' (i.e., 10) and assigns it to 'a'. This is same as 'a = a * 5'.

'/=' Divides the current value of the variable with the value on the right side. 

a /= 5
# divides the current value of 'a' (i.e., 10) with 5 and assigns to 'a'. This is same as 'a = a / 5'.

'%=' Divides the current value of the variable with the value on the right side to calculate the remainder. 

a %= 5 
# divides the current value of 'a' (i.e., 10) with 5 and assigns the remainder to 'a'. This is same as 'a = a % 5'.

'//=' Divides the current value of the variable with the value on the right side and assign the resulting whole number

a //= 5 
# (Floor) divide the current value of 'a' (i.e., 10) with '5' and assigns it to 'a'. This is same as 'a = a // 5'.

'**=' Calculate the exponent of the variable to the power of value on the right side. 

a **= 5 
# Calculate the current value of 'a' (i.e., 10) to the power of 5 and assigns it to 'a'. This is same as 'a = a ** 5'.

Relational Operators

Relational Operators are used to compare the relation between two variables (e.g. greater than, less than, equal to...).

Below are the Relational operators. 
  • Equal To (==), used to check if the variables on either sides are equal (a == b). Returns True if equal and False if not equal.
  • Not Equal To (!=), used to check if the variables on either sides are not equal (a != b). Returns True if not equal and False if equal.
  • Greater than (>), used to check if the left side variable is greater than the right side variable (a > b). Returns True if greater and False if not greater. 
  • Greater than or Equal to (>=), used to check if the left side variable is either greater or equal to right side variable (a >= b). Returns True if greater than or equal and False if not.
  • Less than (<), used to check if the left side variable is less than the right side variable (a < b). Returns True if less and False if not less. 
  • Less than or Equal to (<=), used to check if the left side variable is either less or equal to right side variable (a <= b). Returns True if less than or equal and False if not.

Logical Operators

Logical operators are used to combine multiple conditions and returns if the result is True or False. 

  • 'and', returns True if conditions on either sides are True and returns False if at least one of them is False.
(a > b) and (c > d)
# if both conditions (a > b) and (c > d) are True, then the result would be True. If at least one of them is False, then result would be False.

  • 'or', returns True if one of the condition is True and returns False if both of them are False. 
(a > b) or (c > d)
# if at least one of the condition (a > b) or (c > d) are True, then result would be True. If both conditions are False, then result would be False. 

  • 'not', returns the opposite value of the current condition. If the condition is True, not would return False and vice versa.
not (a > b) 
# if (a > b) is True, then the result would False. if (a > b) is False, then result would be True.

Identity Operators

Identity Operators are used to identify if a value belongs to particular class or type. 

  • 'is', returns True if the type (or value) on the left side matches with the type (or value) mentioned on the right side. 
E.g.:

a = 10
print(type(a) is int) # prints True, as the data type of 'a' is int and the condition is satisfied. 

a = 10
b = 11 
print(a is b) # prints False, as the values of 'a' and 'b' aren't same.

  • 'is not', returns False if the type (or value) on the left side doesn't match with the type (or value) mentioned on the right side.
E.g.:

a = 10
print(type(a) is not int) # prints False, as the data type of 'a' is int and the condition is not satisfied. 

a = 10
b = 11 
print(a is not b) # prints True, as the values of 'a' and 'b' aren't same.

Membership Operators

Membership operators are used to check if the value on the left side is part of the sequence (list or tuples). 

  • 'in', returns True if the left side value is part of sequence mentioned on the right side. returns False if not part of sequence mentioned on the right side.
E.g.:

a = [1, 2, 3]
b = 2

print(b in a) # prints True as '2' is part of list 'a'

  • 'not in', returns False if the left side value is part of sequence mentioned on the right side. returns True if not part of sequence mentioned on the right side.
E.g.:

a = [1, 2, 3]
b = 2

print(b not in a) # prints False as '2' is part of list 'a' 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Comments

  1. Merkur Progress 500 Progress 500-001 - DECCASINO
    Whoa, kadangpintar whoa whoa! Welcome to Merkur septcasino Progress 500 Progress 500-001. Today you will deccasino know about it. The Progress 500 is a 570 year old. The

    ReplyDelete

Post a Comment

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i