I believe this is correct:
x = a or b
Proof
This is how "||" works in JavaScript:
> 'test' || 'again'
"test"
> false || 'again'
"again"
> false || 0
0
> 1 || 0
1
This is how "or" works in Python:
>>> 'test' or 'again'
'test'
>>> False or 'again'
'again'
>>> False or 0
0
>>> 1 or 0
1
Answer from Tadeck on Stack OverflowI believe this is correct:
x = a or b
Proof
This is how "||" works in JavaScript:
> 'test' || 'again'
"test"
> false || 'again'
"again"
> false || 0
0
> 1 || 0
1
This is how "or" works in Python:
>>> 'test' or 'again'
'test'
>>> False or 'again'
'again'
>>> False or 0
0
>>> 1 or 0
1
In python you can use something like this
result = a or b
which may give you result=a if a is not False (ie not None, not empty, not 0 length), else you will get result=b
I'm making a syntax cheatsheet for Javascript vs. Python
Is there a Javascript equivalent to Python's in conditional operator? - Stack Overflow
Do JavaScript arrays have an equivalent of Python’s “if a in list”? - Stack Overflow
Javascript bitwise operator "<<", ">>>" to Python - Stack Overflow
I've been recently watching some Python videos and had the idea of making a simple cheatsheet comparing how to do the same basic things between two languages.
The cheatsheet assumes you are experienced in one of the languages and are looking to switch to the other and just need a quick reference. It doesn't try to teach you how to write either language because that's not the point.
https://www.theyurig.com/blog/javascript-python-syntax
I'm mainly just looking for someone with decent Python experience and who has the bare minimum Javascript experience (this is what I'm strong with) to help me build it and make it more robust. Because I want to offer a way to help with the least friction possible, you can just make a comment on the related issue on my repo and I'll add it.
Just be aware that I'm not gonna push change by change, but in bulks, so if you suggest something and it doesn't go live shortly afterward, it's most likely because I'm waiting to see if no more feedback is coming before I commit my current changes.
Post there since r/python is private due to protesting. Also, let me know if this specific request breaks rule #3 because I feel like it doesn't (since I'm creating a public resource) but I might just be wrong.
You can use .includes:
const good = ['beer', 'pizza', 'sushi']
const thing = 'pizza';
if (good.includes(thing)) console.log('hurray');
Note that this is entirely separate from Javascript's in operator, which checks for if something is a property of an object:
const obj = { foo: 'bar'}
const thing = 'foo';
if (thing in obj) console.log('yup');
Note that includes is from ES6 - make sure to include a polyfill. If you can't do that, you can use the old, less semantic indexOf:
const good = ['beer', 'pizza', 'sushi']
const thing = 'pizza';
if (good.indexOf(thing) !== -1) console.log('hurray');
If you're using JavaScript ES6, I believe that Array.includes is what you're looking for. If you need support for older versions of JS, then you can use Array.indexOf (or use a polyfill for Array.contains).
// Array.includes()
if (yourArray.includes("some value")) {
// Do stuff
}
// Array.indexOf()
if (yourArray.indexOf("some value") > -1) {
// Do stuff
}
Since ES7, it is recommended to use includes() instead of the clunky indexOf().
var my_array = ['a', 'b', 'c'];
my_array.includes('a'); // true
my_array.includes('dd'); // false
var my_array = ['a', 'b', 'c'];
alert(my_array.indexOf('b'));
alert(my_array.indexOf('dd'));
if element not found, you will receive -1
There is no zero filled right shift operator >>> in python and we can not use short hand assignment operator in expressions (like c -= 8). So it can be written like this
(a >> (c - 8)) % 256
a = (a << 6) + f
Yes, there is. Python Bitwise Operators.
From the Docs:
The Operators:
x << y
Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.
x >> y
Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.
x & y
Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.
x | y
Does a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1.
~ x
Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1.
x ^ y
Does a "bitwise exclusive or". Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1.
The way you have done it is the most concise way that I'm aware of in JavaScript. You might want to supply a default value for your reduce to protect against an empty input array:
console.log([1,2,3,4,5].reduce((a,b) => a + b, 0))
// throws a TypeError...
console.log([].reduce((a,b) => a + b))
Javascript is a low level language : no way until you define it.
See Array.prototype.reduce
const add = (acc, item) => {
return acc = acc + item;
});
console.log([1, 2, 3, 4, 5].reduce(add, 0));