xxxxxxxxxx
the ^ operator in python is xor logical operation
if you dont know what xor is
basicly its a bitwise operator
which is some logic that is done on each bit of the data
side note:(if you dont know any thing about binary or logic on them
i suggest you learn about the topic its not easy to explain here)
the xor have 2 parameters a and b
here is what happens on each bit of a and b
a|b|result
0|0|0
1|0|1
0|1|1
1|1|0
xxxxxxxxxx
>>> s1 = {"a", "b", "c"}
>>> s2 = {"d", "e", "f"}
>>> # OR, |
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1 # `s1` is unchanged
{'a', 'b', 'c'}
>>> # In-place OR, |=
>>> s1 |= s2
>>> s1 # `s1` is reassigned
{'a', 'b', 'c', 'd', 'e', 'f'}
xxxxxxxxxx
** for Exponentiation or repeated multiplication of the same number
if you dont know what this is
i guess this page from wikipedia is gonna be helpful :
https://en.wikipedia.org/wiki/Exponentiation
xxxxxxxxxx
# usual form
a and b == c
# sometimes usefull to make up expressions
import operator
from itertools import reduce
# equals to `a and b and c`
reduce(
operator.and_,
[a, b, c]
)
xxxxxxxxxx
#One thing to note- since Python treats all numbers as floats (decimal numbers),
#you can use the double division sign // to get an integer.
#This is useful for finding indexes.
string1 = "abcde"
middle = string1[len(string1) // 2] # 4//2
print(middle)
# c
There is new syntax := that assigns values to variables as part of a larger expression. It is affectionately known as “the walrus operator” due to its resemblance to the eyes and tusks of a walrus.
xxxxxxxxxx
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")