Python Has A NEW Crazy Operator? #python #programming #learnpython

preview_player
Показать описание
Python has a new crazy operator? #python #programming #learnpython
Рекомендации по теме
Комментарии
Автор

another way to make your code unmaintainable

paperxplane
Автор

So what's happening is this:
I'll use signed 8-bit integers so anyone can understand
- We take 0 and invert it (thus using the ~ operator), the inversion of 0 is 1111 1111 (-1 in signed 8-bit integer)
- Now we invert it's signal (using the - operator), so -(-1) = 1

You can do this in any programming language that has the bitwise operators, it's not exclusive to python. Heck, you can even do this on Windows' calculator.

marcosdanielamaral
Автор

Ah. Two's complement. (Boolean algebra)

hardi_stones
Автор

Bitwise "NOT" (~) Operator:
The ~ operator in Python is called the bitwise NOT operator. It works on integers by flipping all the bits in their binary representation. Simply put, for each bit:

A 0 becomes 1.
A 1 becomes 0.

However, because Python uses two's complement notation for representing negative numbers, the result of this operation is -(x + 1).

Example:
Let’s take the number 5. The binary representation of 5 is
1. When the ~ operator is applied to 5, all the bits are flipped:
2. The binary value represents -6 in two's complement notation.

In other words:
~5 is equivalent to -(5 + 1), which gives -6.
Similarly, ~x for any integer x will result in -(x + 1).

Another Example:
x = 10
print(~x) # Output: -11

The binary representation of 10 is 00001010.
Flipping all the bits (bitwise NOT) gives 11110101, which is -11 in two's complement notation.
So, when you use ~ on an integer, the result is the negative of that number plus one.

meysamgor
Автор

A neat trick is this code:

def median(nums):
nums.sort()
mid = len // 2
return (nums[mid] + nums[~mid]) / 2

It makes use of negative indexing, but with "~" rather than "-". 🤓

trwn
Автор

This combination of operations has the same effect in many languages.

robertmenteer
Автор

I have another crazy one for you ! It also takes 2 characters to type… + 1

kiand
Автор

One of the proper ways to use tilde is if you do not want to alternate between positive and negative indexes to get the first and last element at your own convenience.

~0 == -1
~1 == -2

and so forth.

getting the first and the last elements:

elements = “abcd”
for index in range(len(elements) -1):
print(elements[index]+ elements[~index])

SpacemanYK
Автор

floats are gonna have a wild ride with this one

lelaleasl
Автор

It’s not an operator. It’s two operators. Unary minus then (implicitly unary) tilde. Nothing to see here.

tubero
Автор

Hey Indently!
Can you make a video explaining @property and how to make custom properties in classes? If so, thanks very much!

wedgeguyfr
Автор

There is also decrement operator ~- with ~-2 = 1 and ~-1 = 0, ...

saadzahem
Автор

That works if your integer uses the 2's complement for negation (which signed types generally do). 2's complement is basically negation + 1, so this method is simply adding two negations into adding 1.

sciencetube
Автор

You’re basically just negating a number twice and then adding 1. Most languages use 2’s complement for negative numbers. In this number system, -x = ~x + 1.
Therefore, -~x = ~~x + 1 = x + 1.

brandon.m
Автор

Its principle of duality( boolean algebra)

shrifashionworld
Автор

I’m going to implement this in every open source project I see

progames
Автор

Hmmm didn't know about this, could be quite useful when iterating through a list's range using the for loop though 😮 thanks for this 🤟

jamieharper
Автор

In Django, when using the ORM to query data, you also have the Q function that overloads the ~ operator. Something in the lines of Model.objects.filter(Q(some_query_that_filters_true, ~Q(some_query_that_filters_false))

eduferreyraok
Автор

I like to call them "off-by-one" operators. They can be quite useful.

Also in some languages, you can use "--" or "~~" in front of a string to convert it to an integer.

trwn
Автор

Some really cool code is to use python, ro recreate python, and use binary to recreate numbers, then, do 1+1!

Pxsl_YT
visit shbcf.ru