هک‌ها و تشعشعات وجدان آزاد

نوشته‌های سیدمحمدمسعود صدرنژاد

ابزار کاربر

ابزار سایت


Action disabled: revisions
courseware:python_programming:resources:code:23

مثال‌هایی از کار با دیکشنری

# dict declaration
empty_dict = dict()
another_empty_dict = {}
 
people = {
    "masoud": {'position': 'teacher', 'age': 21},
    "hasan": {'position': 'student', 'age': 22},
    "taghi": {'position': 'programmer', 'age': 23},
    "asghar": {'position': 'artist', 'age': 24}
}
people["masoud"]['phone'] = '09121231231'
 
print(list(people.keys()))
print(list(people.values()))
 
for person_k, person_v in people.items():
    print(person_k, '->', person_v)
 
# You can't use a list as a dict key in python because it's mutable
mydict = {[1, 2, 3]: 'Masoud', 'position': 'teacher', 'age': '24'}  # this line of code have an error!
 
# But these lines of code are Ok:
mydict = {(1, 2, 3): [1, 2, 3], 'position': 'teacher', 'age': '24', 'age': 25}
mydict = {'name': 'Masoud', 'position': 'teacher', 'age': '24', 'age': 25}
courseware/python_programming/resources/code/23.txt · آخرین ویرایش: 2021/10/20 17:23 توسط smmsadrnezh