Home >Backend Development >Python Tutorial >How to use operators in Python? (code example)
#How do beginners use operators in Python? Among the operators that can be said to be essential knowledge in programming, the following php Chinese website will lead Would you like to explain how to use operators in Python? [Recommended tutorial: Python tutorial]
1: What are Python operators
Operator Is an operation symbol used for calculations, such as the four arithmetic operations and comparisons when performing programming. There are several types of Python operators.
1. Algebraic operators
Algebraic operators are used for operations represented by arithmetic operations. It is similar in name to the assignment operator, but it is an algebraic operator.
, -,*,/,%,**, //
2. Bitwise operators
The bitwise operators perform bitwise operations on integers. Operators
&, |, ^, >
3. Assignment operator
The assignment operator is used to assign a value to Operators for variables, arrays, etc.
It is similar in name to the algebraic operator, but it is an assignment operator.
=, =, - =, * =, / =%=, = **, // =, &=, | =, ^ = >
4. Comparison operators
Comparison operators are operators used to compare lvalues and rvalues.
==,! =, , =, , ===
5. Logical operators
Logical operators are also called Boolean An operator that returns a conditional expression that is primarily true or false and will be used with the condition of an if statement.
If the result of the value of the logical operation of the logical operator is correct, true is expressed as true.
When the result of the value of the logical operation of the logical operator is correct, false is expressed as true.
6. String operations
String operators manipulate strings. The number of characters in combination is calculated from 0 characters, and operations must start from the end of the characters.
2: How to write Python operators
Using the assignment operator, you can perform arithmetic operations.
can be added using b
a-b can be subtracted using
* b can be multiplied and can be divided by
a/b. You can calculate the remainder by dividing a by b divided by
%b. You can multiply a times b times
**. Can be rounded down
// b.
1. Example of assignment operator
a = 10.0 b = 3 print(a+b) print(a-b) print(a*b) print(a/b) print(a%b) print(a**b) print(a//b)
Display result:
Parsing:
in the first line And in the second line, the floating point number 10.0 is assigned to variable a, the integer 3 is assigned to variable b, and in the print statement, the calculation result is displayed.
2. Comparison operator example
if 0 == 1: print("相同") else: print("不同")
Analysis: The if statement is used to determine whether the numbers 0 and 1 are the same or different. In order to distinguish, we use the comparison operator ==.
The above is about how to use operators in Python? (Code examples) full introduction, if you want to know more, please pay attention to the php Chinese website.
The above is the detailed content of How to use operators in Python? (code example). For more information, please follow other related articles on the PHP Chinese website!