The get() method on dicts and its "default" argument

Youngjae·2020년 9월 22일

Python

목록 보기
5/8
name_for_userid = {
    382: "Alice",
    590: "Bob",
    951: "Dilbert",
}


def greeting(userid):
    return "Hi %s!" % name_for_userid.get(userid, "there")


r = greeting(382)
# r: "Hi Alice!"

r = greeting(333333)
# r: "Hi there!"

https://realpython.com

profile
Eighty percent of success is showing up.

0개의 댓글