Learn Intermediate Python 3 - Higher Order Functions in Python

Link to article:
Learn Intermediate Python 3 - Higher Order Functions
Study this code:

``````def total_bills(func, list):
# This list will store all the new bill values
new_bills = []

# This loop will iterate through our bills
for i in range(len(list)):

# Here we apply the function to each element of the list!
total = func(list[i])
new_bills.append("Total amount owed is \$" + "{:.2f}".format(total) + ". Thank you! :)")

return new_bills
``````

Particularly this line:
`new_bills.append("Total amount owed is \$" + "{:.2f}".format(total) + ". Thank you! :)")`

Is there anything special or any concept about this or is it just a unknown variable(someone, @tgrtim)? I mean the things between the curly braces.
`"{:.2f}"`
This looks strange.

So this is a way of specifying formatting options so to speak onto a string. The colon `:` specifies that a formatting type is to follow for the python compiler to read. The `.2f` specifies that it should be formatted as a floating point number (thatâ€™s the `f`) and then the `.2` specifies that it should be rounded to 2 decimal places. This when your total goes in, it is inserted into the string already rounded to 2dp to work for money, rather than having the usual python floating point number of about 8dp.

Thereâ€™s a ton of these types of formatting you can do, you can find a full (but not necessarily exhaustive) list here and also here is a couple more specific examples.

print("Avogadros constant takes the value of " + "{:e}".format(602300000000000000000000) + ".") # This simply uses the :e option to turn the number into scientific notation. print("The proportion of the population in the Netherlands that are left handed is" + "{: .2%}".format(2286144/17280000)) # This utilises 3 formatting options: # ": " means "put a space before positive numbers and a - before negative numbers". # ".2" we know means round to 2 decimal places. # "%" simply means make the result a percentage. Note we just use this on the raw division, it multiplies by 100 for us print("{:^30,.3f}".format(25304.3675-4632442.34625)) # Here we're using multiple operators again: # "^30" says "make the string 30 characters long and center it". There are equivalents for left and right align. # "," tells it to use the , as a thousands separator # And lastly, we once again use ".3f" to say "render as a floating point number rounded to 3dp.

Hopefully this helps to give you an insight into whatâ€™s going on here and how it can be useful! Any other questions let me know.

3 Likes

I currently use `f` strings instead of `.format()`. Can specifying formatting options be done in the `{}` part(s)?

You can indeed! Let me convert the ones about into f string format to demonstrate:

print(f"Avogadros constant takes the value of {602300000000000000000000:.3e}.") print(f"The proportion of the population in the Netherlands that are left handed is {2286144/17280000:.2%}.") print(f'{25304.3675-4632442.34625:^30,.3f}')

As we can see above the properties work just as normal in the f string format, you essentially just place what would have been in `format()` in front of the colon.

1 Like

So I put the data in the behind the formatting options. Is the formatting options for `f` strings the same as the `.format()` ones?

The Learn Intermediate Python Course didnâ€™t teach me about formatting options; I never learnt it before today. Why does it pop up and Codecademy doesnâ€™t describe it?

Should be mostly the same yes, anything youâ€™d do in format() should work in f strings.

As for why itâ€™s not in the course Iâ€™m afraid youâ€™d be better contacting support to query that one, as maybe it can be an inclusion in future. Itâ€™s been a which since I did the basic Python course but I would presume it would be covered in there and hence the intermediate course doesnâ€™t cover it again.

No, it wasnâ€™t covered. I am going to check the formatting options site you suggested me.

Such a good site! Maybe I should save the site as a shortcut on my Google and also star it. This might be a useful tool to me.

Look at this code:

print(f"I have {1543253425342:b} kitchens.")

This is a binary option.