Python Tutorials - Herong's Tutorial Examples - v2.15, by Herong Yang
"=" Statement - Assignment Statement
This section provides a quick introduction of '=' (assignment) statement, which is a simple statement that assigns objects to reference targets.
What Is "=" - Assignment Statement? A "=", or assignment, statement is a simple statement that assigns objects to reference targets.
Assignment statements have several forms:
1. Variable Assignment - It assigns a single object to a single variable target using this syntax:
variable_name = object
If the variable is new, it will be defined and assigned to reference the given object. If the variable is old, its old object reference will be replaced with the given object. For example:
# "x" is a new variable >>> x NameError: name 'x' is not defined # assign a "str" object to a new variable "x" >>> x = "apple" >>> x 'apple' # assign a "float" object to a old variable "x" >>> x = 3.14 >>> x 3.14
2. Attribute Assignment - It assigns a single object to an attribute of a target object using this syntax:
target_object.attribute_name = object
If the attribute is new to the target object, it will be added and assigned to reference the given object. If the attribute is old, its old object reference will be replaced with the given object. For example:
>>> import sys # "x" is a new attribute to "sys" >>> sys.x AttributeError: module 'sys' has no attribute 'x' # assign a "str" object to a new attribute "sys.x" >>> sys.x = "apple" >>> sys.x 'apple' # assign a "float" object to a old attribute "sys.x" >>> sys.x = 3.14 >>> sys.x 3.14
3. Subscription Assignment - It assigns a single object to an item (indexed or named) of a target object using these syntaxes:
target_object[index] = object target_object[key_name] = object
For an index item (like a "list" object item), you can only assign to an existing index. For an named item (like a "dict" object key), you can assign to an old or new key. For example:
# assignment to a new indexed item is not allowed >>> fruits = list(["apple", "orange"]) >>> fruits[2] = "banana" IndexError: list assignment index out of range # assignment to an old indexed item is allowed >>> fruits[0] = "banana" >>> fruits ['banana', 'orange'] # assignment to a new named item is allowed >>> prices = dict(apple=0.99, orange=1.19) >>> prices["banana"] = 0.69 >>> prices {'apple': 0.99, 'orange': 1.19, 'banana': 0.69} # assignment to an old named item is allowed >>> prices["banana"] = 0.79 >>> prices {'apple': 0.99, 'orange': 1.19, 'banana': 0.79}
4. Slice Assignment - It assigns items from an iterable object to a slice of a target object using this syntax:
target_object[low:high] = iterable_object
The entire slice (all items starting from the low index to the last item before the high index) in the target object is replaced with references extracted from the iterable object. The target object may shrink or expand, if the iterable object has less or more items than the slice. For example:
# the target object >>> feet = [10, 20, 30, 40, 50, 60, 70, 80, 90] >>> feet [10, 20, 30, 40, 50, 60, 70, 80, 90] # slice feet[2] to feet[3] is assigned with 3 new references # "feet" is expanded >>> feet[2:4] = ('30', '35', '40') >>> feet [10, 20, '30', '35', '40', 50, 60, 70, 80, 90]
5. Bulk Assignment - A bulk assignment is also call an unpacking operation. It assigns items from an iterable object to a sequence of targets using one of these syntaxes:
target_1, target_2, ..., = iterable_object (target_1, target_2, ...,) = iterable_object [target_1, target_2, ...,] = iterable_object
Each item from the iterable object is assigned to each target sequentially from left to right. The number of items from the iterable object must equal to the number of targets. For example:
# bulk assignment with items from a "list" object >>> fruits = ["apple", "banana", "cherry", "strawberry", "raspberry"] >>> a, b, c, s, r = fruits >>> a, b, c, s, r ('apple', 'banana', 'cherry', 'strawberry', 'raspberry') # the trailing "," indicates a bulk assignment # the only item from the slice is assigned to the variable >>> green, = fruits[0:1] >>> green 'apple' # without trailing ",", it becomes a variable assignment # the slice itself is assigned to the variable >>> green = fruits[0:1] >>> green ('apple',)
5. Starred Bulk Assignment - It assigns items from an iterable object to a sequence of targets with one of them marked with a start "*" Starred bulk assignment statements use one of these syntaxes:
target_1, target_2, ..., *target_i, ..., = iterable_object (target_1, target_2, ..., *target_i, ...,) = iterable_object [target_1, target_2, ..., *target_i, ...,] = iterable_object
A starred bulk assignment is executed in the same as a regular bulk assignment, except that the starred target will be assigned with a "list" object to any extra items. The number of items from the iterable object must equal to or greater than the number of un-starred targets. For example:
>>> fruits = ["apple", "banana", "cherry", "strawberry", "raspberry"] # no extra items assigned to "x" >>> a, b, *x, c, s, r = fruits >>> a, b, x, c, s, r ('apple', 'banana', [], 'cherry', 'strawberry', 'raspberry') # 2 extra items assigned to "x" >>> a, b, *x, r = fruits >>> a, b, x, r ('apple', 'banana', ['cherry', 'strawberry'], 'raspberry')
Table of Contents
Variables, Operations and Expressions
"pass" Statement - Do Nothing Statement
Expression Statement - One Expression Only
►"=" Statement - Assignment Statement
"del" Statement - Delete Statement
"import" Statement to Load Modules
"if" Statement for Conditional Execution
"while" Statement for Execution Loop
"for" Statement for Iterative Execution
"try" Statement to Catch Execution
"with" Statement for Context Manager
"match" Statement for Pattern Match
Function Statement and Function Call
List, Set and Dictionary Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager