简体   繁体   中英

Explanation needed for a syntax in python

I am new to python and after learning some topics i wanted to do a small project (an email sender).When i was researching a bit about libraries needed and some examples, I saw the following piece of code :

msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

I am confused about the syntax:

var['something'] = anything

What does this syntax imply? Please help.

This sort of syntax is used for accessing/modifying Python dictionaries . The example var["Something"] = anything is setting the value of the variable anything in the dictionary var for the key "Something"

Keys must be immutable objects, such as strings, integers, floating-point numbers, or tuples. Dictionary values can be any python object.

This syntax is used for python data structure dictionary which much like telephone dictionary enables us to associate a keyword (in square brackets) with a value (on LHS). For more details, please refer to section 5.5 in tutorial https://docs.python.org/2/tutorial/datastructures.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM