YAPT

Yet Another Python Tutorial (support@openanswer.io)

View on GitHub

Variables

“Elementary, my dear Watson.” -Sherlock Holmes

Did you know that Holmes never said that particular phrase in any of the books? Turns out the quote was first used by P. G. Wodehouse, in Psmith Journalist, 1915. The author of the quote turned out to be P. G. Wodehouse, not Holmes nor Sir Arthur Conan Doyle!

After reading the paragraph above you probably almost subconsciously created two variables. A quote for "Elementary, my dear Watson.", and an author for Sherlock Holmes or Sir Arthur Conan Doyle. You may have then changed the author to P. G. Wodehouse. In programming, variables are just that - some information we name in order to later recall it.

Variables are created by assigning values to a particular name using the assignment operator ( = ):

 > variable = 'value'
 #            ^     ^ quotes define strings (a text data type, more on this later)
 > author = 'Sherlock Holmes'
 > quote = 'Elementary, my dear Watson.'
 > year = 1915

Variables can be reassigned:

 > variable = 'a different value'
 > author = 'Wodehouse'

Can contain other variables (more on this later):

 > a = 1
 > b = a

And the information stored in the variables can be recalled:

 > a
=> 1
 > b
=> 1
 > variable
=> 'a different value'
 > quote
=> 'Elementary, my dear Watson.'
 > author
=> 'Wodehouse'
 > year
=> 1915
Previous (Introduction) Home Next (Operators)