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
deepor
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:

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

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

Code:
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):

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

Code:
>>> 4 == "cheeseburger"
False
>>> 4 == "4"
False
deepor is offline