# Is the max() function only available when you have the math library imported already and can you use it with letters or numbers (like I've done in javascript)

#1

The max function imports the highest in a set of numbers:

example:

``````maximum = max(1,2,3)

print maximum
``````

The result will be 3.

My question is asking if you need to import the math library first before using max. I assume no, but I want to make sure with someone that has more experience.

#2

`max` is one of Pythonâs âbuilt inâ functions, and so is always available without you having to import a module.

The full list is here:

https://docs.python.org/2/library/functions.html

#3

can you use the min function like you use the following in javascript:

``````FIRST FROM PAARL
Write a function called firstPaarl that takes a comma separated string parameter with registration numbers and returns the first registration number in the string for Paarl. Remember Paarl registration numbers starts with CJ.

function firstPaarl(firstReg){
console.log(firstReg);
var cjStorer = '';
var stringOrig = firstReg.split(', ');

for (var i = 0; i < stringOrig.length;i++) {
if (stringOrig[i].startsWith('CJ'))
{ cjStorer = stringOrig[i]; break; }

// console.log(emptyList);

}

return cjStorer
};

``````

Note: the above is javascript and paarl (in RSA) start with CJ and then numbers

Feel free to ask me to clarify anything.

Here is the task I am on:
find s in yes exercise

#4

Looks like youâre just splitting a comma-delimited string, then iterating each item from the string to find the first item which starts with âCJâ.

What I canât figure out, unless Iâm missing something obvious, is why you need the `min` function in Python to make an equivalent of that JavaScript?

I noticed youâre edited the topic of the thread as well; to answer your question, yes the `min` and `max` functions will work on strings as well as numbers.

#5

I was thinking if you had the same data that you could use min and use a for loop to get the same result?

I know Iâm missing somethingâŠbut I think min will be splitting the string?

Maybe I used an incorrect exampleâŠ

Intrucitons:
WEEKEND OR NOT
Write a function called weekOrWeekend that takes in a day parameter. If the day passed in is a weekDay it should return âweekâ otherwise is should return âweekendâ;

Start using the assert statement to test your functions.

Assert statements are used to state facts about your code. Scenarios and statements that you expect to be either true or false can be created. When an assert pass nothing happens, only when an assert fails it will report an error.

// after you declared the function
assert.equal(weekOrWeekend(âWednesdayâ), âweekâ);
assert.equal(weekOrWeekend(âSundayâ), âweekendâ);
The assert function will fail and print an error to the screen if the expected value is not returned from the function.

``````function weekOrWeekend (day){

var week = !day.startsWith('S');

var weekend = day.startsWith('S');

if (week === true){return "week";}

else {return "weekend";}

};
``````

In this example you can see that saturday and sunday both start with S. So if you turn all the day of the week into an array and use min() in the array for the first position â in order to find s, would that be a correct usage of min?

Obviously other built in functions that I do not know would be helpful in this situation. for example, how to turn a string into an array.

I did not mention, all the item would be stored in a string like this:

``````days='monday,tuesday,wedneday,thursday,friday,saturday,sunday
``````

at the beginning, prior to you seperating, etc. in a function.

#6

Ahhh, sorry - I see what you mean now.

No, that wouldnât work how you wanted. If you called `min("Saturday")`, the output would be âSâ. If you called `min("saturday")`, youâd get âaâ. I presume that this is due to Python evaluating the ASCII code for the characters, and as capitals have a lower ASCII value than lowercase letters the capital would be favouredâŠ (Thatâs a guess, though. Iâd need to go digging in the technical docs.)

In Python, a string is already an array; you can iterate through a string or retrieve one or more characters by indexâŠ

``````my_word = "Saturday"

# iterate over the string and print each letter
for letter in my_word:
print letter

# grab the 3rd letter
print my_word[2]

# grab the 4th, 5th and 6th letters
print my_word[3:6]
``````

Does that help?

#7

thats a great start. I see what youâre saying but I think I will need to talk it over with one of my mentors haha. But Im glad Iâm on the right track.

#8

As you can tell, Iâm learning still. Iâve already done javascript so by doing python Iâm hoping to review those core programming concepts with python and cement them haha. I spent 4 months learning javascript! It took forever!

#9

No worries.

Keep in mind, though, that Python already includes a `startswith()` method as part of the string type.

https://docs.python.org/2/library/stdtypes.html#str.startswith

The resulting code in Python wouldnât be too different to in JavaScript, then.

I donât think anyone ever really stops learning when it comes to programming. Thereâs always room to learn a new way of doing something, or picking up little tips and tricks from others and so on.

#10

Yes! Iâve already seen that! I was o proud of myself for completing javascript. Now I am learning there are even shorter way to accomplish the same tasks that used to take me hours! For some of the exercies, I would spend at least 6 hours. now my time is down to half an hour! (obviously based on those tips from my mentors and you guys!)

#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.