Python: Programming Language for Beginners - Learn In A Day! (2015)
Chapter 6. Variables
Suppose you want to run a python program but you do not know how to code. At this time your friend can type the code in notepad and save the file with an extension of .py and send it to you. One can download it and run it in the IDLE and check the
Output.
Eg: Notepad file
#Demo program
print "Mary has a little lamb,"
print "It's fleece is white as snow;"
print "And everywhere that Mary went",
print "her lamb was sure to go."
Output on the friends system i.e. in command line:
Mary had a little lamb,
It's fleece was white as snow;
And everywhere that Mary went her lamb was sure to go.
In the above example we see that the last two lines of the notepad file have been joined and the reason behind that is the comma that is placed outside the double quotes.
Variables
Variables are used to store values that can be accessed or changed at later stages.
Example- Variables
#variables
print "Variable demo"
v = 1
print "V=”, v
v = v + 1
print "v=v+1 now”, v
v = 51
Strings
Variables can hold a set of characters also. Variable that can hold text is a string.
Example-Strings
wor1 = "Good"
wor2 = "Morning"
wor3 = "to you too!"
print wor1, wor2
sentence = wor1 + " " + wor2 + " " +wor3
print sentence
Output:-
Good Morning
Good Morning to you too!