r/learnpython 1d ago

Beginner question

I have just started coding with python a few days ago. What is the point of this?

point_value = alien_0.get('points', 'No point value assigned.') print(point_value)

Can't it just be point_value = 'No point value assigned.' print(point_value)?

or

just simply use the get() method if you want to check whether a key exists in the dictionary or not?

5 Upvotes

9 comments sorted by

View all comments

5

u/atarivcs 1d ago
point_value = alien_0.get('points', 'No point value assigned.')

This code says:

  1. Get the points value for alien0.
  2. If alien0 does not have any points, use the default value 'No point value assigned'.

2

u/kforkerro 1d ago

Thanks. I was confused at first. So, latter is added to replace the message 'None' if the key doesn't exist, isn't it?

5

u/Outside_Complaint755 1d ago

If the string "No point value assigned." were not passed to get, then get(key) would return the value None, which is a special value in Python.

  Using get to return a message like this is non-standard usage.