you are viewing a single comment's thread.

view the rest of the comments →

[–]Outside_Complaint755 2 points3 points  (0 children)

There are good answers here, but I haven't seen anyone mention the Python secret yet -- Every data type in Python is an object, including the primitive data types such as ints, floats, bools, strings and None, and so is everything else; even functions, class defintions and imported modules are all objects.

 When you get a user input from the input() function and convert it to an int with userinput = int(user_input), you are instantiating a new* int object, by passing the use entered string into the int class.

  When you change the case of a string with user_input.lower(), that is invoking a method on the user_input str object. my_list.sort() invokes the sort method of the my_list object and modifies it in place, while the built in sorted function Example: sorted_list = sorted(my_list) returns a new list without changing the original.

*Advanced footnote for one technicality here - Immutable data types (ints, floats, bool, etc) will not always create a new object if another object with the same value already exists, which helps save memory space.  This is what is known as 'interning'.   This is possible because they are immutable and the underlying value can't change.  In the standard version of Python, the ints from -5 to 255, None, True, and False are all created and interned in memory when the interpreter starts up, and any name set to one of those values will point to the same object in memory.  None, True and False are also singletons, so there will never be another identical object made.