At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
http://www.asciitable.com/ Basically when you compare a char to an int, the char is interpreted as its integer ascii representation number.
char '4' = int 52 char 'a' = int 97 so if you were to substitute them in 97 < 3 = false 4 < 52 = true
Char is the smallest data type available in C. It consists of 8 bits. However it also has a special property to it it in that it can directly reference ASCII characters. For instance this is legal. char a =1, int b =1. This is also legal, char a = 'a'. However this isnt legal, int = 'a'. You will get an error that says, "cant implicitly convert type char into type int.".
also char a = '1', char b = 1, this isnt a true statement a == b. 1 typed in from your keyboard and that is actually being displayed on your screen right now. Is not being represented by 1 under the hood. Any time you are typing in a calculator a number from your computer. Under the hood the calculator is casting from a char to a int. Then it returns a string (implicitly) for you to read.
At least in Python version 2.x when doing comparisons (say x
Waow, thanks guys. You guys are really helpful!
@Rogerdoger91 @AdamDadeH Good answers. It's an aspect of Python I don't care for very much, not just operator overloading but the ability also to cross types. I am sure benefits for this allowance but I think it leads to more confusions than anything else.
So, I think there's a little bit of confusion here. @Rogerdoger91 - thank you for the posts but there are some inaccuracies in them. Python does not convert strings to ints and then return stings 'implicitly'. The ASCII tables are showing you the bit representation of characters - not their integer equivalents. Think of the ASCII table as getting you closer to the actual combination of 0's and 1's that your computer understands in the lower level languages, for each of these characters. "Int" is a type in python, as is string, tuple, float, etc. Python does not use these bit representations in comparisons, but first asks, "what type(s) am I dealing with?" and then proceeds with it's own comparison rules for that type (or those types). As AdamDadeH says, all ints are considered 'less than' strings. Python allows you to compare other types as well, which, and I'm in agreement with malpaso on this, is annoying. I'm not sure what the benefits are to this, but in any case @GuruDL this won't really matter for completing the assignments. It's more just a general knowledge about quirks in Python heads-up sort of thing. The true way in which low level languages interpret Python - a high level language - is what you would need to really answer this question but it is beyond the scope of this course, so don't worry.
CPython is an interpreted language. What that means is everything we are writing in python is converted into its equivalent C code then compiled into machine executable code. So when you write string = 'I love bacon' in python. It is being converted into a char array in C. When you compare 'a' < 3. Python will implicitly cast it to its ascii value to make the comparison legal because it assumes you want to do that comparison. Chars are data values that are mapped to the ASCII table in memory. They can either be interpreted by their bit values, or their ascii letter. Python doesnt require you to specify ints or chars or strings. It does alot of implying when it converts to C by the context of the program in which its used. I shouldnt have said converts to ints. Thats incorrect, but I was trying to point out that when you write something, the computer is stroing it in memory via binary. And then when you call for it to be printed to the string you dont have to tell it that you want characters appear.
@Rogerdoger91. I don't believe you are correct. CPython is an implementation of Python and it is the mainstream implementation of Python, but not necessarily the only one. There is JPython, for example, which is written in Java. You are correct that Python is an interpreted language. But it's not correct to imply that an interpreted language converts everything into C. That's not what it means for a language to be an interpreted language. Also, it's my understanding that Python converts to bytecode which then runs inside a virtual machine (Python Virtual Machine). In short, there is no intermediate step where Python structures are converted to C structures (e.g. char array in C) and then into machine executable code. The steps are: the Python implementation (which can be written in C or Java in the case of JPython) converts the input to bytecode which is then executed inside the Python virtual machine.