Overclock.net - An Overclocking Community - View Single Post - What is this? Not a string..

View Single Post
post #30 of (permalink) Old 11-04-2018, 11:28 AM
New to Overclock.net
deepor's Avatar
Join Date: Feb 2013
Posts: 4,736
Rep: 474 (Unique: 320)
Yeah, that 'animal' is coming out of nowhere. It's starting to exist in the line with the 'for' loop. It's getting assigned values there. To be annoyingly clear about what's happening, this part here:

for animal in animals:
    print animal
First translates into this:

for animal in ["ant", "bat", "cat"]:
    print animal
And it then translates into:

animal = "ant"
print animal

animal = "bat"
print animal

animal = "cat"
print animal
About that thing with the 4 == "cheeseburger" test: the answer for Python is, it will always return False when you compare a number and a string. It does not matter what the values are or the length of the string or whatever. Python's rules are, a number and a string are not the same so it always says False.

In other languages, other things happen. There's languages where you don't get True or False, instead you get an error. The language says you made a mistake trying to compare a number and a string and wants you to change your code before it accepts it.

There's also languages like JavaScript or Perl where it will automatically try to convert the string into a number (or the other way around, the number into a string). In those languages the following happens (this is a JavaScript console prompt):

js> 4 == "4"
js> 4 == "cheeseburger"
Here's what Python does, like I mentioned earlier it just always says a number compared to a string is False:

>>> 4 == "cheeseburger"
>>> 4 == "4"
deepor is offline