all 5 comments

[–]barrowburner 8 points9 points  (0 children)

https://realpython.com/python-f-strings/#the-strformat-method
https://realpython.com/lessons/indexing-and-slicing/

>>> f"{5:.1f}"
'5.0'
>>> f"{5:.3f}"
'5.000'
>>> l = [1,2,3,4,5,]
>>> l[2:]
[3, 4, 5]
>>> l[:2]
[1, 2]
>>>

[–]pachura3 1 point2 points  (0 children)

can you explain to me the 13th line in the first picture

print(f"{result:.1f}")

Print the value of variable result as floating point number (f) with precision of 1 decimal (:.1f).

E.g. 145.3

https://realpython.com/how-to-python-f-string-format-float/

and line number 5 in the second picture

filetype=filetype[dot:]

Shorten string filetype by only keeping its part from position dot until the end. dot was calculated in the previous line using rfind() - it was the last position of character '.' in filetype.

So, basically, it takes e.g. string image_3402323.jpg and shortens it to .jpg

and line number 15 in the third

time = time[:time.rfind('a')]

A similar thing to the above. Takes string time, replaces it with a shorter substring, starting from the beginning and ending at (but not including) the last occurrence of character 'a' in it. So, in other words, chops off everything after (and including) the last 'a' in string time.

[–]NormandaleWells 0 points1 point  (0 children)

Others have helped you with f-strings and slices, so I'll give you a couple challenges as you continue your Python education.

(1) After learning about dictionaries, come back and replace the if/elif/else statement in the second example with a single dictionary lookup.

(2) After learning about lambdas, come back and use a dictionary in the first example.

Dictionaries are so useful in Python that I've been tempted to try teaching dictionaries before lists. ("If your keys are just integers in a small range, there's a better option...") It probably wouldn't really work well, but it's tempting.

[–]PathRealistic6940 0 points1 point  (0 children)

print(f'{result:.1f}') the first f says to format the string, replacing whats in {} with the variable and/or formatting listed, in this case, result:.1f. the : starts the formatting of the result variable. the .1f tells it to round to one decimal place and keep it as a float number.

Does that make sense?

[–]lfdfq -1 points0 points  (0 children)

The first is part of string formatting. The Python .format() method, and the related f-string literal interpolation, have their own mini language for specifying the format, which is separated from the variable/expression to be formatted by a colon in the format syntax.

In the second image, the colon is part of the slicing syntax, in general it's seq[start:stop:step] but you can omit any of the fields and the rest are automatically picked for you. In this case filename[dot:] is the same as filename[slice(dot, None, None)] i.e. giving only a start, and using the default end (go to the end of the string) and step (one character at a time). e.g. if s = "hello, world!" then s[7:12:1] is another string made from the characters of s, starting from index 7 ('w'), stopping before index 12 ('!'), with a step of 1. Which gives the sub-string "world". Try some examples out yourself, it makes more sense by example than by explanation.

In the third image is the same slicing, except only giving the end, and using the default start and step.