Kishor
Kishor

Reputation: 450

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.

Upvotes: 3

Views: 51

Answers (2)

Arun Kaushal
Arun Kaushal

Reputation: 631

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

Upvotes: 1

rfj001
rfj001

Reputation: 8498

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.

Upvotes: 4

Related Questions