A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

I didn't quite get the part where 'a' < 3 is False, and 4 < '3' is True. How is this comparable between strings and numbers? V_Lecture 2 - 12:45 mark

  • This Question is Closed
  1. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://www.asciitable.com/ Basically when you compare a char to an int, the char is interpreted as its integer ascii representation number.

  2. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    char '4' = int 52 char 'a' = int 97 so if you were to substitute them in 97 < 3 = false 4 < 52 = true

  3. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.".

  4. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    At least in Python version 2.x when doing comparisons (say x<y) between two objects of the same type it will the comparison appropriate to the objects. When comparing objects of different types (say x is an int and y is a string) Python orders the objects by their type. The rules seem to be the following None < int < list < string < tuple (by experiment) So None<1=True [4,3,5]<10=False 500 < 'a' = True, etc... This has been (fixed/changed depending on your opinion) in Python 3.x

  6. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Waow, thanks guys. You guys are really helpful!

  7. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @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.

  8. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  9. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  10. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @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.

  11. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.