What is an operator? The role of Python operators and the meaning of each python operator

Tomorin
Release: 2018-08-13 16:10:27
Original
3693 people have browsed it

What is an operator?

This chapter mainly explains Python’s operators. Let’s take a simple example: 4 5 = 9. In the example, 4 and 5 are called operands, and " " is called the operator.

Python language supports the following types of operators:

1. Arithmetic operators

2. Comparison (relational) operators

3. Assignment operations Operator

4. Logical operator

5. Bit operator

6. Member operator

7. Identity operator

8.Operator precedence

Next let’s learn Python’s operators one by one.

Python arithmetic operators

The following assume variables: a=10, b=20:

or less The example demonstrates the operation of all arithmetic operators in Python: The output result of the above example is: 1 - The value of c is: 312 - The value of c is: 11
Operator
Description
Instance

Add - Add two objects a b Output result 30
- Subtract - get a negative number or one number minus another number a - b Output result- 10
* Multiply - Multiply two numbers or return a string repeated several times a * b Output result 200
/ Divide - x divided by y b / a Output result 2
%
Modulo - Returns the remainder of division b % a Output result 0
**
Power - Returns the y power of Integer division - Returns the integer part of the quotient (rounded down) 9//2 The output result is 4, 9.0//2.0 The output result is 4.0
#!/usr/bin/python# 
-*- coding: UTF-8 -*-
 a = 21
 b = 10
 c = 0
 c = a + b
 print "1 - c 的值为:", c
 
 c = a - b
 print "2 - c 的值为:", c 
 
 c = a * b
 print "3 - c 的值为:", c 
 
 c = a / b
 print "4 - c 的值为:", c 
 
 c = a % b
 print "5 - c 的值为:", c
 
 # 修改变量 a 、b 、c
 a = 2
 b = 3
 c = a**b
 print "6 - c 的值为:", c
 
 a = 10
 b = 5
 c = a//b 
 print "7 - c 的值为:", c
Copy after login

3 - The value of c is: 210

4 - The value of c is: 2

5 - The value of c is: 1

6 - The value of c is: 8

7 - The value of c is: 2

The above is the detailed content of What is an operator? The role of Python operators and the meaning of each python operator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template