Can someone explain the output of the following code regarding max()? [duplicate] - python

This question already has answers here:
How does the max() function work on list of strings in python?
(6 answers)
Closed 2 years ago.
Python code:
print(max(['2020','4','70','5']))
I am getting the output as 70
I want to understand how max() works on strings.
Can anyone explain?

if you provide max() a string, it returns the highest alphabetical character in a string. So it will order based on alphabetical order descending.
As explained here https://www.geeksforgeeks.org/python-string-max/#:~:text=os._exit()-,Python%20String%20%7C%20max(),alphabetical%20character%20in%20a%20string.&text=Return%20value%3A,highest%20character%20in%20the%20string.

max() can be used in 2 ways
Finding the largest item in an iterable
max(iterable, *iterables, key, default)
Getting the largest number in a list
number = [3, 2, 8, 5, 10, 6]
largest_number = max(number);
I think that what you want is this last one. If you also added a
print(largest_number)
It would throw you the output of 10.

Related

Python: Sort a list and print it out with a for loop - 'NoneType' object is not iterable' [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed last year.
I have created a random list with numbers:
numbers = [8, 2, 17, 99, 12]
And I want to print them out with a for loop and permanently sort the numbers in increasing order.
I was able to do that, but i'm not sure if I am using the short way to do that.
numbers.sort()
for number in numbers:
print (number)
I solved that with the code above, but my first instict was to do this:
for number in numbers.sort():
print (number)
But the following error was found: TypeError: 'NoneType' object is not iterable.
Is there a way to sort the numbers in the "for placeholder_variable in variable" or the only way is with the first code? I have to sort them first and then make the for loop?
Thank you.
what you want is:
for number in sorted(numbers):
print (number)
numbers.sort() sorts your list in place but does not return anything itself.

In String Slicing str [:3] , in case the string is less than length 3, will this slicing return or not? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
If I have a string of variable length and I want to return only first 3 characters of the string.
str[:3]
this works but I want to know if the string is of lesser length, suppose 2, "ab" or just "a", will this slicing work for that too?
Thanks in advance
Python will return at most :n characters:
'a'[:3] will simply return 'a'. ''[:3] returns ''.
You could have tested for yourself in less time than it would have taken to open your browser.
But yes.

What does Python max() do if it takes two lists in it? [duplicate]

This question already has answers here:
What is the __lt__ actually doing for lists [duplicate]
(2 answers)
Closed 4 years ago.
How come max([1,2,3], [1,1,4]) returns [1,2,3] not [1,1,4]?
I was asked this question in a class. I don't understand why it returns [1,2,3] and the logic behind it (even if it returns [1,1,4], I still don't understand what max() function does).
The function max will succeed in outputing a maximum as long as the provided arguments are comparable.
In this case, the first argument is greater than the second with regard to list-ordering.
>>> [1, 2, 3] > [1, 1, 4]
True

Python min function tuples with arbitrary tuples [duplicate]

This question already has answers here:
How does tuple comparison work in Python?
(4 answers)
Closed 5 years ago.
What is this python code doing?
min((2,3),(6,'f',1))
Output: (2, 3)
I am not able to follow the documentation.
Can someone explain why the output in (2,3) and not an error?
Because (2,3) < (6,'f',1)
Meaning tuples are compared itemwise, therefore 2 < 6 yields that the first tuple is less than the second one
While this code works on Python 2 and Python 3, it should fail on Python 3 if both items in 1st place were the same. Because it would compare 3 to the string 'f' and such comparison is now invalid.
The min function will call the comparator methods of the objects you pass. In this case, all tuples. It is returning the minimum tuple with respect to lexicographic order.

Algorithm for sorting a list in python does not work properly [duplicate]

This question already has answers here:
How to sort python list of strings of numbers
(4 answers)
Closed 8 years ago.
I am using the following function in order to sort a list in an increasing order. However, while my function works for lists such as: [1,5,6,9,3] or [56,43,16,97,45], it does not work for lists of the form: [20,10,1,3,50].
In such cases, the computer seems to consider that 3>20 and 3>10 and 3 ends up right before 50 (second to last) in the "sorted" list I get. More precisely the result I get is: [1,10,20,3,50].
Here is my code:
def function_sort(L):
for j in range(len(L)):
min=j
for i in range(j+1,len(L)):
if L[i]<L[min]:
min = i
if(min != j):
L[j],L[min] = L[min],L[j]
print L
return L
Could anyone please explain me what is going on?
It sounds like your list consists of strings rather than integers, and you end up getting the elements sorted lexicographically.
By way of illustration, consider the following:
>>> 10 < 2
False
>>> '10' < '2'
True
To fix the issue, convert the elements to integers before sorting:
L = map(int, L)
P.S. I recommend against using min as a variable name since it shadows the built-in function min().

Categories