Data Science with Python: Python Variables and Operators

Operators

In this article, we will understand Python variables and Python operators in detail.

Python Variables

In a programming language, variables are fundamental for storing and managing data within a program.  Variables act as containers to store values and allow the user to extract and manipulate values as per the requirements of the programmer. Variables store data in storage locations in the computer’s memory.

How to Create Variables?

In Python, you create a variable by assigning a value to a name using the assignment operator =.
Python automatically determines the data type of the variable based on the value assigned.

num = 5

Variable Naming Rules

  1. Variable names must start with a letter (a-z, A-Z) or an underscore (_).
  2. Variables cannot start with a number.
  3. Variables can only contain alphanumeric characters (letters, numbers) and underscores.
  4. Variable names are case-sensitive (e.g., num, Num, and NUM are considered different variables).
  5. Avoid using Python keywords (like from, print, for, while, if, class, etc.) as variable names.
  6. Any special character is not allowed.
  7. Space in a variable name is not allowed.

Python Operators

Operators are special symbols in Python that are designed to perform special operations and computations. The value that the operator operates on is called the operand. Some operators require two operands, and some require only one operand to operate. There are seven broad categories of operators used in Python.

Operators

1. Arithmetic Operators

Arithmetic OperatorOperator NameFunction
+Addition OperatorUsed to add two numbers or two strings.
Subtraction OperatorUsed to subtract two numbers.
*Multiplication OperatorUsed to multiply two numbers.
/Division OperatorUsed to divide two numbers and return the quotient.
%Modulus OperatorUsed to divide two numbers and return the remainder.
**Exponent OperatorGives the value left operand raised to the power of the right operand.
//Floordivision OperatorUsed to divide two numbers and return the round-off quotient to the nearest integer.

2. Relational Operators

Relational OperatorOperator NameFunction
<Less thanReturns true if the left operand is less than the right.
>Greater thanReturns true if the left operand is greater than the right.
<=Less than or equal toReturns true if the left operand is less than or equal to the right.
>=Greater than or equal toReturns true if the left operand is greater than or equal to the right.
==Equal toReturns true if the left operand is equal to the right.
!=Not Equal toReturns true if the left operand is not equal to the right.

3. Logical Operators

Logical OperatorOperator NameFunction
andLogical ANDReturns true if both operands are true.
orLogical ORReturns true if one of the operands is true.
notLogical NOTReturns true if operand is false and returns false when operand is true.

4. Assignment Operators

Assignment OperatorOperator NameFunction
=AssignmentAssigns a value to the variable on the left.
+=Assignment SumAdds the value on the right to the existing value of the variable on the left.
-=Assignment DifferenceSubtracts the value on the right from the existing value of the variable on the left.
*=Assignment MultiplicationMultiply the value on the right by the existing value of the variable on the left.
/=Assignment DivisionDivide the existing value of the variable by the value on the right and return the quotient.
%=Assignment ModulusDivide the existing value of the variable by the value on the right and return the remainder.
**=Assignment ExponentGives the value left operand raised to the power right operand.
//=Assignment FloordivisionDivide the existing value of the variable by the value on the right and return the rounded-off quotient to a near integer.

5. Bitwise Operators

Bitwise OperatorOperator NameFunction
&Bitwise ANDFollows the truth table of the AND logic gate.
|Bitwise ORFollows the truth table of the OR logic gate.
~Bitwise NOTFollows the truth table of NOT logic gate.
^Bitwise XORFollows the truth table of the XOR logic gate.

6. Membership Operators

Membership OperatorOperator NameFunction
‘in’‘in’ OperatorReturns true if the value is found in the sequence(string, list, tuple, set, or dictionary)
‘not in’‘not in’ OperatorReturns true if the value is not found in the sequence(string, list, tuple, set, or dictionary)

7. Identity Operators

Identity OperatorOperator NameFunction
‘is’‘is’ OperatorReturns true if both operands are identical and saved in the same memory location.
‘is not’‘is not’ OperatorReturns true if both operands are not identical and not saved in the same memory location.

Conclusion

In this article, we have covered Python variables and Python data operators in detail. Hands-on practice is a must to have a good command of a programming language.

Stay Tuned!!

Learn the Data Types and Data Structures of Python, click on the link below:

Data Science with Python: Basic Data Types

Data Science with Python: Basic Data Structures

Keep learning and keep implementing!!

Leave a Comment

Your email address will not be published. Required fields are marked *