Computer Science
Operators in Python
Operators in Python are symbols that perform operations on variables and values. They include arithmetic operators (+, -, *, /), comparison operators (>, <, ==), logical operators (and, or, not), and more. These operators are used to manipulate data and control the flow of a program, making them essential for writing efficient and effective code.
Written by Perlego with AI-assistance
Related key terms
1 of 5
8 Key excerpts on "Operators in Python"
- eBook - ePub
An Introduction to Python Programming: A Practical Approach
step-by-step approach to Python programming with machine learning fundamental and theoretical principles.
- Dr. Krishna Kumar Mohbey; Dr. Brijesh Bakariya(Author)
- 2021(Publication Date)
- BPB Publications(Publisher)
HAPTER 2Operators and Expressions
O perator is a particular concept in Python that is carried out from the computational point of view. Here, there are two types of the terminology used - operator and operands. The operator is a symbol that is performed with operands' help, and the operand is the value that the operator operates.Structure
In this chapter, we will cover the following topics:- Types of operators
- Writing expression in Python
- Use comments during a program
- Working with function and modules
Objective
The objective of this chapter is to introduce Python operators, expressions, functions, and modules. After completing this chapter, you should be familiar with various Python operators and their working. Also, you will be able to write Python programs using operators and expressions.What is an operator?
It is a particular type of symbol that performs some operations on operands and returns the result. Let us take an example for better understanding, 10+20 is an expression. Here, 10 and 20 are operands, and + is an operator. So, in this example, a simple arithmetic expression is evaluated, and the result will be the addition of the two values.There are various types of operators used in Python. The table 2.1 shows the hierarchy of the operators available in Python:Operators Symbols Arithmetic operators +,-, *,/, %, **,// Comparison operators >,>=,<,<=,==,! = Assignment and shortcut operators =, +=,-=,*=,/=,%=,**=,//= Unary operators +x, -x Bitwise operators &,|,^,~,<<,>> Logical operators and, or, not Membership Operators in, not Identity operators is, is not Table 2.1: Hierarchy of the operatorsArithmetic operators
It performs the basic mathematical operation on the numeric operands. The arithmetic operators return the result, and the outcome depends on which type of operands you are using in programming.- Addition (+) : The symbol + is used while using this operator. It uses between two operands and this operator adds the two value. For example, a=4, b=2 then a + b
- eBook - ePub
Data Analysis with Python
Introducing NumPy, Pandas, Matplotlib, and Essential Elements of Python Programming (English Edition)
- Rituraj Dixit(Author)
- 2022(Publication Date)
- BPB Publications(Publisher)
To perform operations, we need operators, which are the function of the operation, and operands are the input to that operation. For example, 10+6 = 16; here, in this expression, 10 and 6 are the operands, and + is the operator.Various types of Operators in Python are depicted with their implementation in Python as follows.Arithmetic operators
Arithmetic operators are required to perform arithmetic operations like addition, subtraction, multiplication, division, and so on. The following table is the list of arithmetic Operators in Python:Operator name Operator symbol Description Example addition + Add the two operands a+b subtraction - Subtract the right operands from the left operand a-b multiplication * Multiply the two operands a*b division or float division / Left operand divide by the right operand and gives the float value as a result a/b floor division // Left operand divide by the right operand and gives the floor value of division as a result a//b exponent ** Raised the left operand to the power of right a**b (3**2 means 3 to the power of 2) modules % Gives the remainder of the division of the left operand by the right operand a%b Table 3.1: Arithmetic Operators in PythonThe following are some codes where we used arithmetic operators on the variables a and b:Coding example(s)
a = 10 b = 8 # Addition (+) addition = a + b print(‘addition => a + b =’,addition) # Subtraction (-) Subtraction = a - b - eBook - ePub
Python Internals for Developers
Practice Python 3.x Fundamentals, Including Data Structures, Asymptotic Analysis, and Data Types
- Sonam Chawla Bhatia(Author)
- 2021(Publication Date)
- BPB Publications(Publisher)
operands . Python has the following type of operators:- Arithmetic operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
- Assignment operators
Arithmetic operators
Arithmetic operators perform mathematical operations on operands such as addition, subtraction, and so on. Arithmetic operators are binary operators. Binary operators operate on two operands. Table 1.3 describes the various Python arithmetic operators:Operator Name Description + Addition Adds values of two operands. - Subtraction Subtract the value of the right operand from the left operand. * Multiplication Multiplies values of two operands. / Division Divides the left operand with the right operand. % Modulus Divides the left operand with the right operand and displays the output (remainder of division). ** Exponentiation Raise left operand to the power of right operand. // Floor Division Divides left operand with right operand and display the output (the floor value). Table 1.3: Python arithmetic operatorsExample 1.4 shows few arithmetic operators and their output.Example 1.4: Arithmetic operators- 2 +3//Output 5
- 3-2 //Output 1
- 2 *3//Output 6
- 5 /2//Output 2.5
- 23 %5//Output 3
- 2 **4//Output 16
- 5 //2//Output 2
The preceding example shows different outputs obtained while using arithmetic operators. As shown in the preceding example, the modulus operator (%) is the same as the mathematical remainder operator which outputs the remainder when 23 is divided by 5 (which is 3 ). The exponentiation operator (**) is the same as the mathematical power operator which outputs the value of 24 (which is 16 ). The floor division operator (//) divides the operand’s and the output’s floor value of division like in the preceding example, 5/2=2.5 but 5//2 outputs floor value of 2.5 which is 2 .The addition and multiplication Operators in Python can be used for strings too. Python addition operator adds two strings and merges them. Python multiplication operator can take one string and one integer as input. Example 1.5 - eBook - ePub
- Michael Burch, Marco Schmid(Authors)
- 2024(Publication Date)
- River Publishers(Publisher)
Table 4.1 ). In the Python programming language, those are expressed by special symbols like +, -, *, /, and **. Moreover, we can distinguish between binary operators, that is, those that take two arguments and combine them into one, and unary operators, that is, those that just work on one value and might change the sign of a value, for example, from a positive into a negative one. The good thing with expressions in Python is that they do not only work on raw values like integers and floating point numbers, but also on variables or even function calls that create a certain value as a result.Table 4.1 A list of arithmetic operators, some examples, their meanings, and mathematical notations.Operator Example Explanation Math formula + x + y Add x and y (addition) x + y - x - y Subtract y from x (subtraction) x − y * x * y Multiply x and y (multiplication) x · y / x/y Divide x by y (division, type float) x y // x//y Divide x by y (division, type int) ⌊ x ⌋ y ** x **y x to the power of y (exponentiation) y x y % x%y Divide x by y (modulo division) x mod y - −x Negative of x (unary) −x + +x Positive of x (unary) +x As already shown in Table 4.1 , we can start building arithmetic expressions by simply using the rules of arithmetic and then step-by-step connect more and more of such subexpressions to more complex ones. An example of a more complex arithmetic expression is given in Listing 4.1 . Here, we also see the idea of evaluation precedence or priorities. The addition parentheses have the highest priority and express that the expressions contained inside the parentheses should be evaluated first. Then the multiplication and division operators have a higher priority than the additional and subtraction operators. If operators have the same priority level, the expression is evaluated from left-to-right. By the way, the arithmetic expression in Listing 4.1 - eBook - ePub
- Dimitrios Xanthidis, Christos Manolas, Ourania K. Xanthidou, Han-I Wang, Dimitrios Xanthidis, Christos Manolas, Ourania K. Xanthidou, Han-I Wang(Authors)
- 2022(Publication Date)
- Chapman and Hall/CRC(Publisher)
script usually contains a sequence of statements. When there are more than one statements, the results appear one at a time, as each statement is executed.An expression is a combination of values, variables, operators, and calls to functions resulting in a clear and unambiguous value upon execution.2.6.2 Operators
Operators are tokens/symbols that represent computations, such as addition, multiplication and division. The values an operator acts upon are called operands.Observation 2.9 – Operators/Operands: Operators are symbols representing computations like additions, multiplications, divisions. Operands are the values that the operators act upon.Let us consider the simple expression x = 3*2 . The reader should note the following:- x is a variable.
- 3 and 2 are the operands.
- * is the multiplication operator.
- 3*2 is considered an expression since it results in a specific value.
TABLE 2.3 Python Arithmetic OperatorsOperator Example Name Description + (unary) +a Unary positive a + (binary) a + b Addition Sum of a and b . The + operator adds two numbers. It can be also used to concatenate strings. If either operand is a string, the other is converted to a string too. − (unary) −a Unary negation It converts a positive value to its negative equivalent and vice versa. − (binary) a − b Subtraction b subtracted from a . * a * b Multiplication Product of a and b. / a / b Division The division of a by b . The result is always of type float . % a % b Modulo The remainder when a is divided by b . // a // b Floor division (also called integer division) The division of a by b , rounded to the next smallest integer. ** a ** b Exponentiation a raised to the power of b . Python supports many operators for combining data into expressions. These can be divided into arithmetic, comparison, logical, assignment, and bitwise:Observation 2.10 – Efficient Script Writing: Include expressions that display results inside the print function to avoid multiple instructions. Use a single statement to declare and assign values to multiple variables. - eBook - ePub
Basic Core Python Programming
A Complete Reference Book to Master Python with Practical Applications (English Edition)
- Meenu Kohli(Author)
- 2021(Publication Date)
- BPB Publications(Publisher)
HAPTER 3Numbers, Operators and In-built Functions
Introduction
Programing is all about implementing logic which is done with the help of various operators. In this chapter, you will learn about the numeric data type in Python, operators and some very important in-built functions available for Python coding.Structure
- Numbers in Python
- Boolean variables and expressions
- Conversion of Boolean to integer and vice versa
- Operators
- Arithmetic operators
- Arithmetic operators precedence in Python
- Relational/Comparison/Conditional operator
- Logical operator (Boolean expressions)
- ‘and operator
- ‘or operator
- ‘not operator
- Assignment operator
- Bitwise operators
- Binary AND
- Binary OR
- Binary XOR
- Binary ones complement
- Binary left shift
- Binary right shift
- Membership operators
- Identity operators
- Arithmetic operators
- Built-in functions for math operators
- Functions available for conversion of numbers from one type to another
- Math module
- Mathematical functions
- Trigonometric functions
- Working with random numbers
- Float representation and equality
- Operator precedence and associativity
- Associativity
- Statements
- Simple statements
- Compound statements
- Date/ Time module
Objectives
In this chapter, you will learn about:- Numerical data type
- Various operators used to carry out:
- Arithmetic operation
- Relational operation
- Logical operation
- Bitwise operation
- Membership operation
- Identity operation
- Working with three important python modules:
- Math module
- Random module
- Date/time module
3.1 Numbers in Python
You were briefly introduced to the data types in Python in the last section of the previous chapter. In this chapter, we will discuss all about numbers and what we can do with them. Python supports three types of numbers:- Integers
- Float
- Complex numbers
Figure 3.1: Python numbers typesThe integers belong to ‘int ’class. Integers can be positive or negative and do not have any decimal point they can be of unlimited size in Python. 256 is an integer and -288890000 is also an integer. The floats belong to the ‘float ’ class. These are real numbers with decimal points. Complex numbers belong to the ‘complex ’ class. They are of the form a+bj where a and b are floats and j - eBook - PDF
- Adele Kuzmiakova(Author)
- 2023(Publication Date)
- Arcler Press(Publisher)
By inputting any arithmetic phrase into the interpreter, we may quickly examine its value: >>> 3+4 7 >>> 17–6 11 >>> 2 + 3*4 14 >>> (2 + 3)*4 20 >>> 3 + 11/4 5.75 In a Python application, none of these expressions would ever be used as a full line. The samples provided here are solely for educational purposes. We will learn how to use arithmetic expressions in Python scripts very soon (Alzahrani et al., 2018; Schäfer, 2021). The four operators are given, multiplication (*), subtraction (–), and addition (+), all function in the same way they did in elementary school. Division and multiplication take precedence over subtraction and addition, as seen in the examples above, and parenthesis can be used to specify the order in which operations should be performed (Dubois et al., 1996; Tateosian, 2015). 4.4. VARIABLES IN PYTHON 4.4.1. The Idea of a Variable One of the reasons computers programmers are so effective is that they can do computations with a variety of numbers while still following a similar set Introduction to Python Programming 105 of instructions. The usage of variables is one method of accomplishing this. Instead of computing 5*5, if we could compute side*side for any value of side, we would be able to measure the area of any square rather than the area of a square with side 5. Variables are simple to utilize in Python. You may insert the name of the variable in your code whenever you wish to utilize it (Donat, 2014; Hunt, 2019). The one drawback is that when you initially make a variable, it lacks a well-defined value, so you cannot utilize it in a framework that requires one. The simplest approach to introduce a variable is to use an assignment statement, as seen below: >>> side = 5 The variable produced is called side, and the line above assigns the value 5 to it (Figure 4.4). The following is a representation of memory at this moment in time: If we go along with this statement: >>> area = side*side In memory, then our picture is as follows: Figure 4.4. - eBook - PDF
Introduction to Computing Using Python
An Application Development Focus
- Ljubomir Perkovic(Author)
- 2015(Publication Date)
- Wiley(Publisher)
15 16 Chapter 2 Python Data Types 2.1 Expressions, Variables, and Assignments Let’s start with something familiar. We use the Python IDE interactive shell as a calculator to evaluate Python expressions, starting with simple algebraic expressions. Our goal is to illustrate how Python is intuitive and usually behaves the way you would expect. Algebraic Expressions and Functions At the interactive shell prompt >>> , we type an algebraic expression, such as 3 + 7, and hit the Enter key on the keyboard to view the result of evaluating the expression: >>> 3 + 7 10 Let’s try expressions that use different algebraic operators: >>> 3 * 2 6 >>> 5 / 2 2.5 >>> 4 / 2 2.0 In the first two expressions, integers are added or multiplied and the result is an integer, which is what you expect. In the third expression, an integer is divided by another and the result is shown in decimal point notation. This is because when an integer is divided by another, the result is not necessarily an integer. The rule in Python is to return a number with a decimal point and a fractional part, even when the result is an integer. This is illustrated in the last expression, where integer 4 is divided by 2 and the result shown is 2.0 rather than 2. Values without the decimal point are said to be of type integer or simply int. Values with decimal points and fractional parts are said to be of type floating point or simply float. Let us continue evaluating expressions using values of both types: >>> 2 * 3 + 1 7 >>> (3 + 1) * 3 12 >>> 4.321 / 3 + 10 11.440333333333333 >>> 4.321 / (3 + 10) 0.3323846153846154 Multiple operators are used in these expressions, which raises the question: In what order should the operations be evaluated? The standard algebra precedence rules apply in Python: Multiplication and division take precedence over addition and subtraction and, just as in al- gebra, parentheses are used when we want to explicitly specify the order in which operations should take place.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.







