What are Assignment Statements in Python
Assignments
The assignment is the most basic statement in Python, assigning data and an object type to a variable name. You have already seen a number of different examples of this when you learned about the different Python object types. Unlike C, like Perl (in non-strict mode), you do not need to predeclare Python variables before you assign them a value. However, variables must exist when used within an expression or other form of a statement.
There are four basic methods of assignments: basic assignments, and tuple assignments. list assignment, and multiple-target assignment.
Basic Assignments
The basic assignment is the one you have seen most examples of in this chapter. A basic assignment creates a new object using the correct type according to the value assigned to the object, and then the name of the new object, as in the following examples:
number = 45
message = 'Hello World'
mylist = ['I Palindrome I', Mammal']
mytuple = ('mon', 'Tue', 'Wed', 'Thu', Fri')
mydict = {'Twisting' :Flood, Mammal': Apollo 18'}
Tuple and List Assignments
You can extract the individual elements from a tuple or list using a different operator of assignment. For tuples, you can just specify the list of variables to assign to, each separated by a comma:
title, name = 'Mr', 'Martin'
album = ('TMBG', 'Flood', 'Theme from Flood', 'Birdhouse in your Soul')
artist, title= album[0:2]
track1, track2= album [-2:1]
What you’re in fact doing in the preceding statements is creating a new tuple on the left-hand side of the assignment operator. However, the tuple with the variable names is anonymous and the information about the entire tuple is not recorded. Because you don’t have to use parentheses to indicate a tuple, the assignment looks more natural, although you could have written them as:
album = ('TMBG', 'Flood', 'Theme from Flood', 'Birdhouse in your Soul')
(artist, title) = album[0:2]
(track1, track2) = album [-2:]
What you end up with in each case is a new variable pointing to the information on the right-hand side. Note as well that you can also use slices and indexes to extract multiple elements from a tuple.
For lists, you must create a new, anonymous list in the same way that you create an anonymous tuple:
albem = [ 'TMBG', 'Lincoln', 'Ana Ng', 'Cowtown']
[artist, title ] = albem[0:2]
The tuple and list assignments have another trick up their sleeve. Because names are merely pointers, you can “swap” two object/name combinations by performing a tuple or list assignment as follows:
artist, title = title, artist
You can see this better in the following interactive session:
>>> title= 'Flood
>>> artist='TMBG'
>>> artist, title = title, artist
>>> artist, title
('Flood', 'TMBG')
Multiple-Target Assignments
You can create a single object with multiple pointers using the multiple-target assignment statement:
Group = title = 'They Might Be Giants'
In the preceding statement, you created a single-string object with two different pointers called group and title. Remember that because the two pointers point at the same object, modifying the object contents using one name also modifies the information available via the other name. This is effectively the same as:
Group = 'They Might Be Giants'
title = group
Unlike other languages, the default method for communicating with the user is via a statement rather than via a function. The statement has the same basic name, print, but is embedded into the interpreter. Unlike similar print functions, however, the Python print statement actually outputs string representations of the supplied objects to the standard output of the interpreter. The destination is the same as the C stdout or Perl STDOUT file handles, and cannot normally be pointed elsewhere.
Tip:-
Actually, you can redirect the standard output by modifying the object used to paint to the standard output device. It should be no surprise that Python uses objects to communicate with the outside world. The following code demonstrates this point:
Import sys
fp = open('somefile.txt', 'w')
sys.stdout = fp
print 'this goes in the file, not on your stdout'
This works because instead of pointing the standard output to its original location, you copy the object data for the new fp object instead. The stdout object, which is used by the print function, now sends all of the output to the file somefile.txt.
When printing, the statement follows these basic rules:
•Objects and constants separated by commas are separated by spaces when printed. Use string concatenation or the % formatting operator to avoid this.
•A line feed is appended to every output line. To avoid this, append a comma to the end of the line.
For example, the following script, when executed, displays the different forms supported:
Print 'Hello', 'world'
Print 'This is a' + 'concatenated', String
Print 'The first line',
Print 'I ordered %d dozen %s today' % (6, 'eggs')
Contacts =[{'Name', : 'Martin',
'Email', : 'mc@mcwords.com'},
{'Name', : 'bob',
'Email', : 'bob@bob.com'}]
Print contacts
The preceding script produces the following output:
Hello world
This is a concatenated string
The first line plus some continuation
I ordered 6 dozen eggs today
[{ ' Email': 'mc@mcwords.com', 'Name' : 'Martin' }, {'Email' : 'bob@bob.com',
'Name' : 'Bob' }]
Note how in the last line, print displays the complex object in the same format as str and the “operator.
The ability to print out the contents of an object in this way is extremely useful, both as a programming tool and for debugging. For example, you can store the configuration parameters for an application in one or more variables. When you want to save the information, just use print or the str function to write out the statements. To get the information back, all you need do is import the file contents in the same way that you would with any other module.
[…] Assignments […]