   Francais

Home

Products

Tutorials

Python Section
• Python Quick Reference

Contact

Basic Operations  on  variables

Assign several variables in the same time

Several variables separated by = (equal) can be assign in the same time. The value must be put after the last equal. That reduces the number of lines and create more readable code. The way to use variable does not change whatever the assignment's type.

Examples :
 Multiple assignment Simple assignment >>>A=B=C=0>>>A+=1>>>B+=2>>>C+=3>>>print A,"\n", B, "\n", C123 >>>A=0>>>B=0>>>C=0>>>A+=1>>>B+=2>>>C+=3>>>print A,"\n", B, "\n", C123
The multiple assignments can be used on list variables too. However, all list variables initialized with the several assignments method will not act in the same way than each one initializes separately. In reality, creates several list variables at once will create one list called by different name.

Examples :
 Multiple assignment Simple assignment >>>A=B=C=[ "1", 2, 3]>>>A+=["4", 5, 6]>>>B+=["7", 8, 9]>>>C+=["10", 11, 12]>>>print A,"\n", B, "\n", C['1', 2, 3, '4', 5, 6, '7', 8, 9, '10', 11, 12]['1', 2, 3, '4', 5, 6, '7', 8, 9, '10', 11, 12]['1', 2, 3, '4', 5, 6, '7', 8, 9, '10', 11, 12] >>>A=[ "1", 2, 3]>>>B=[ "1", 2, 3]>>>C=[ "1", 2, 3]>>>A+=["4", 5, 6]>>>B+=["7", 8, 9]>>>C+=["10", 11, 12]>>>print A,"\n", B, "\n", C['1', 2, 3, '4', 5, 6]['1', 2, 3, '7', 8, 9]['1', 2, 3, '10', 11, 12]

Empty list creation

A empty list variable can be create by assign to it a empty value or with the range() function.

Examples :
 Assign an empty value Use the range() function >>> list=[] >>> print list[] >>> list=range(1,0) >>> print list[]

 4/13