Home Backend Development Python Tutorial Python3基础之基本运算符概述

Python3基础之基本运算符概述

Jun 16, 2016 am 08:42 AM
python3 Basic Base operator

本文所述为Python3的基本运算符,是学习Python必须掌握的,共享给大家参考一下。具体如下:

首先Python中的运算符大部分与C语言的类似,但也有很多不同的地方。这里就大概地罗列一下Python 3中的运算符。

一、算术运算符

注意:

双斜杠 // 除法总是向下取整。
从符点数到整数的转换可能会舍入也可能截断,建议使用math.floor()和math.ceil()明确定义的转换。
Python定义pow(0, 0)和0 ** 0等于1。

二、比较运算符

运算符 描述
小于
小于或等于
> 大于
>= 大于或等于
== 等于
!= 不等于
is 判断两个标识符是不是引用自一个对象
is not 判断两个标识符是不是引用自不同对象

注意:

八个比较运算符优先级相同。

Python允许x 复数不能进行大小比较,只能比较是否相等。

三、逻辑运算符

运算符 描述 备注
x or y if x is false, then y, elsex  
x andy if x is false, then x, elsey  
not x if x is false, then True,elseFalse  

注意:

 or是个短路运算符,它只有在第一个运算数为False时才会计算第二个运算数的值。
and也是个短路运算符,它只有在第一个运算数为True时才会计算第二个运算数的值。
not的优先级比其他类型的运算符低,所以not a == b相当于not (a == b),而 a == not b是错误的。

四、位运算符

运算符 描述 备注
x | y 按位或运算符  
x ^ y 按位异或运算符  
x & y 按位与运算符  
x  n 左移动运算符  
x >> n 右移动运算符  
~x 按位取反运算符  

五、赋值运算符

复合赋值运算符与算术运算符是一一对应的:

 

六、成员运算符

Python提供了成员运算符,测试一个元素是否在一个序列(Sequence)中。

运算符 描述
in 如果在指定的序列中找到值返回True,否则返回False。
not in 如果在指定的序列中没有找到值返回True,否则返回False。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the root operator in C language? What is the root operator in C language? Mar 06, 2023 pm 02:39 PM

In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values ​​nor output imaginary results.

What does % mean in Java What does % mean in Java Mar 06, 2023 pm 04:48 PM

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

Golang error: 'invalid use of ... operator' How to solve it? Golang error: 'invalid use of ... operator' How to solve it? Jun 24, 2023 pm 05:54 PM

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

What is the meaning of '==' symbol in php What is the meaning of '==' symbol in php Mar 14, 2023 pm 07:05 PM

In PHP, the "==" symbol is a comparison operator that can compare whether two operands are equal. The syntax is "operand 1 == operand 2". The "==" operator compares and tests whether the variable on the left (expression or constant) has the same value as the variable on the right (expression or constant); it only compares the values ​​of the variables, not the data types. If the two values ​​are the same, it returns a true value; if the two values ​​are not the same, it returns a false value.

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Analysis of the meaning and usage of += operator in C language Analysis of the meaning and usage of += operator in C language Apr 03, 2024 pm 02:27 PM

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

PHP Basics Tutorial: From Beginner to Master PHP Basics Tutorial: From Beginner to Master Jun 18, 2023 am 09:43 AM

PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

Magic methods in Python Magic methods in Python Apr 13, 2023 am 10:25 AM

Magic methods in Python are special methods that allow you to add "magic" to a class. They are often named surrounded by two underscores. Python's magic method, also known as the dunder (double underline) method. Most of the time, we use them for simple things like constructors (init), string representations (str, repr) or arithmetic operators (add/mul). In fact, there are many methods that you may not have heard of but are very useful. In this article, we will sort out these magic methods! We all know the size of the iterator __len__ method, which can be used in container classes Implement the len() function on. However, if you want to get the length of a class object that implements iterator

See all articles