+ 1

Oddities in Python

How do you explain these anomalies... https://code.sololearn.com/cswL7cDCh5Ux/?ref=app

18th Mar 2021, 12:19 PM
Sanjay Kamath
Sanjay Kamath - avatar
25 odpowiedzi
+ 2
Sanjay Kamath if some source claims that "b=(4) defines a tuple const" in Python then that source needs correction. Would you elaborate on your question of portability? I would expect Python code to be portable from platform to platform, as long as it is within the same version of the language. I am not a Python expert, but I believe the syntax for tuples has not changed amongst Python versions.
19th Mar 2021, 2:39 PM
Brian
Brian - avatar
+ 3
well, it seems that for that google calculator ^ is used as exponentation operator (in python this is **)... and anything raised to power of 0 will result to 1 ;) you couldn't assume that all operators works same everywhere ^^
18th Mar 2021, 2:08 PM
visph
visph - avatar
+ 2
^ is binary xor operator 0^0 result to 0 (all numbers) then you print this result as string (str(ee))...
18th Mar 2021, 2:00 PM
visph
visph - avatar
+ 2
visph In Asia ^^ In the United States šŸ‡ŗšŸ‡ø it's šŸ“€šŸ˜‡ 0=1 šŸ¤£šŸ¤£šŸ¤£
18th Mar 2021, 3:15 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
by 'everywhere' I don't think to geographic place, but in any language and/or application... all around the world: anything raised to power of 0 will result to 1 (general mathematics are same everywhere) anything xor 0 will result to anything (binary mathematics are same everywhere) what could change is the symbol(s) used to do these operations...
18th Mar 2021, 3:20 PM
visph
visph - avatar
+ 2
??? if this is really supposed to be a python parser, then there's a bug... or you've entered 0**0, it show result as 0^0 (exponentiation in both cases): look at the result field... it show 0^0= 1, while the input field show 0**0...
18th Mar 2021, 3:50 PM
visph
visph - avatar
+ 2
Brian thanks! I didn't noticed this change made to the code, so I didn't noticed this error ;)
18th Mar 2021, 4:47 PM
visph
visph - avatar
+ 2
empty_tuple = () one_item_tuple = ('item',) two_items_tuple = ('item1','item2') two_items_tuple_bis = ('item1','item2',)
19th Mar 2021, 2:49 PM
visph
visph - avatar
+ 1
(and you could concatenate tuples as you concatenate lists)
18th Mar 2021, 12:29 PM
visph
visph - avatar
18th Mar 2021, 2:03 PM
Sanjay Kamath
Sanjay Kamath - avatar
18th Mar 2021, 3:42 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
try to put 0^0 in input field in python mode, it should result as 0 (don't know wich expression will be shown in result field)
18th Mar 2021, 3:51 PM
visph
visph - avatar
+ 1
-1 and (-1) is the same value...
18th Mar 2021, 6:12 PM
visph
visph - avatar
+ 1
Sanjay Kamath in program var1.0 I am guessing that you intended to create a single-value tuple by using af=(-1) and expected to find a mismatch between the tuple and the single integer -1. And you would be right. Only the syntax is wrong. The correct syntax to make af into a tuple would be af=(-1,).
18th Mar 2021, 6:31 PM
Brian
Brian - avatar
+ 1
I have already explained how tuples works in the first answer of this thread ^^
18th Mar 2021, 6:33 PM
visph
visph - avatar
+ 1
notice that only empty tuple doesn't have comma inside it ^^
19th Mar 2021, 2:50 PM
visph
visph - avatar
0
there's no anomalies here, only expected output ^^ what are you expected? what are you calling 'anomalies'? printing list output square brackets notation adding lists result to lists concatenation parenthesis, unless having at least one comma are mathematical parenthesis, so number inside parenthesis is just number. printing string doesn't default output quotes to wrap text (to do so you must use repr instead of str, or be in the python command line and just type your variable, without using print function), so number to string output number as number...
18th Mar 2021, 12:27 PM
visph
visph - avatar
0
(parenthesis with at least one comma are tuples -- imutable lists -- so output is done enclosed in parenthesis)
18th Mar 2021, 12:28 PM
visph
visph - avatar
0
Instead of: da=0**0 print (str(ee)) Try: da=0**0 print (str(da)) (print the correct variable)
18th Mar 2021, 4:44 PM
Brian
Brian - avatar