Skip to main content

Operators | Python

  • Operators | Python

Sumário dos operadores

Arithmetic OperatorsAssignment OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsIdentity OperatorsMembership Operators
+ Addition= Assign== Equal toand Logical AND& Bitwise ANDisin
- Subtraction+= Add and assign!= Not equal toor Logical OR`` Bitwise ORis not
* Multiplication-= Subtract and assign> Greater thannot Logical NOT^ Bitwise XOR
/ Division*= Multiply and assign< Less than~ Bitwise NOT
// Floor Division/= Divide and assign>= Greater than or equal to<< Bitwise left shift
% Modulus//= Floor divide and assign<= Less than or equal to>> Bitwise right shift
** Exponentiation%= Modulus and assign
**= Exponentiate and assign
&= Bitwise AND and assign
|= Bitwise OR and assign
^= Bitwise XOR and assign
>>= Right shift and assign
<<= Left shift and assign

Aritméticos

# + Addition
# - Subtract
# * Multiply
# / Division
# // Floor Division -> x // y
# % Modulus -> x % y
# ** Exponentiation -> x ** y

Assignment operators

# = Assign
x = 5

# += Add and assign
x += 3 # Equivalent to x = x + 3

# -= Subtract and assign
x -= 2 # Equivalent to x = x - 2

# *= Multiply and assign
x *= 4 # Equivalent to x = x * 4

# /= Divide and assign
x /= 2 # Equivalent to x = x / 2

# //= Floor divide and assign
x //= 2 # Equivalent to x = x // 2

# %= Modulus and assign
x %= 3 # Equivalent to x = x % 3

# **= Exponentiate and assign
x **= 2 # Equivalent to x = x ** 2

# &= Bitwise AND and assign
x &= 3 # Equivalent to x = x & 3

# |= Bitwise OR and assign
x |= 2 # Equivalent to x = x | 2

# ^= Bitwise XOR and assign
x ^= 1 # Equivalent to x = x ^ 1

# >>= Right shift and assign
x >>= 1 # Equivalent to x = x >> 1

# <<= Left shift and assign
x <<= 2 # Equivalent to x = x << 2

Relational operators

# == Equal to
x == y # Returns True if x is equal to y

# != Not equal to
x != y # Returns True if x is not equal to y

# > Greater than
x > y # Returns True if x is greater than y

# < Less than
x < y # Returns True if x is less than y

# >= Greater than or equal to
x >= y # Returns True if x is greater than or equal to y

# <= Less than or equal to
x <= y # Returns True if x is less than or equal to y

Logical operators | Python

# and Logical AND
x and y # Returns True if both x and y are True

# or Logical OR
x or y # Returns True if either x or y is True

# not Logical NOT
not x # Returns True if x is False

Bitwise operators | Python

Os operadores bitwise (ou operadores de bits) são usados para realizar operações em nível de bit em números inteiros. Eles manipulam diretamente os bits individuais de um número binário. Aqui está uma breve descrição de cada operador bitwise:

  • & (AND bit a bit): Realiza a operação AND bit a bit. Cada bit do resultado é 1 se os bits correspondentes de ambos os operandos forem 1, caso contrário, é 0.
  • | (OR bit a bit): Realiza a operação OR bit a bit. Cada bit do resultado é 1 se pelo menos um dos bits correspondentes dos operandos for 1.
  • ^ (XOR bit a bit): Realiza a operação XOR bit a bit. Cada bit do resultado é 1 se os bits correspondentes dos operandos forem diferentes.
  • ~ (NOT bit a bit): Realiza a operação NOT bit a bit. Inverte todos os bits do operando.
  • << (deslocamento à esquerda): Desloca os bits do operando à esquerda por um número especificado de posições. Bits deslocados para fora à esquerda são descartados, e zeros são inseridos à direita.
  • >> (deslocamento à direita): Desloca os bits do operando à direita por um número especificado de posições. Bits deslocados para fora à direita são descartados. Para números positivos, zeros são inseridos à esquerda; para números negativos, uns são inseridos à esquerda (deslocamento aritmético).

Esses operadores são úteis em várias aplicações, como criptografia, compressão de dados, manipulação de gráficos e otimização de desempenho.

# & Bitwise AND
x & y # Performs bitwise AND on x and y

# | Bitwise OR
x | y # Performs bitwise OR on x and y

# ^ Bitwise XOR
x ^ y # Performs bitwise XOR on x and y

# ~ Bitwise NOT
~x # Performs bitwise NOT on x

# << Bitwise left shift
x << 2 # Shifts bits of x to the left by 2 positions

# >> Bitwise right shift
x >> 2 # Shifts bits of x to the right by 2 positions

Identity operators

Os operadores de identidade são usados para comparar objetos e verificar se eles são o mesmo objeto na memória.

# is
x is y # Returns True if x and y are the same object

# is not
x is not y # Returns True if x and y are not the same object

Membership operators

Os operadores de associação são usados para testar se uma sequência contém um valor específico.

# in
x in y # Returns True if x is found in y

# not in
x not in y # Returns True if x is not found in y