Real Python: Python's Assignment Operator: Write Robust Assignments :
by:
blow post content copied from Planet Python
click here to view original post
Python’s assignment operators allow you to define assignment statements. This type of statement lets you create, initialize, and update variables throughout your code. Variables are a fundamental cornerstone in every piece of code, and assignment statements give you complete control over variable creation and mutation.
Learning about the Python assignment operator and its use for writing assignment statements will arm you with powerful tools for writing better and more robust Python code.
In this tutorial, you’ll:
- Use Python’s assignment operator to write assignment statements
- Take advantage of augmented assignments in Python
- Explore assignment variants, like assignment expressions and managed attributes
- Become aware of illegal and dangerous assignments in Python
You’ll dive deep into Python’s assignment statements. To get the most out of this tutorial, you should be comfortable with several basic topics, including variables, built-in data types, comprehensions, functions, and Python keywords. Before diving into some of the later sections, you should also be familiar with intermediate topics, such as object-oriented programming, constants, imports, type hints, properties, descriptors, and decorators.
Free Source Code: Click here to download the free assignment operator source code that you’ll use to write assignment statements that allow you to create, initialize, and update variables in your code.
Assignment Statements and the Assignment Operator
One of the most powerful programming language features is the ability to create, access, and mutate variables. In Python, a variable is a name that refers to a concrete value or object, allowing you to reuse that value or object throughout your code.
The Assignment Statement Syntax
To create a new variable or to update the value of an existing one in Python, you’ll use an assignment statement. This statement has the following three components:
- A left operand, which must be a variable
- The assignment operator (
=
) - A right operand, which can be a concrete value, an object, or an expression
Here’s how an assignment statement will generally look in Python:
variable = expression
Here, variable
represents a generic Python variable, while expression
represents any Python object that you can provide as a concrete value—also known as a literal—or an expression that evaluates to a value.
To execute an assignment statement like the above, Python runs the following steps:
- Evaluate the right-hand expression to produce a concrete value or object. This value will live at a specific memory address in your computer.
- Store the object’s memory address in the left-hand variable. This step creates a new variable if the current one doesn’t already exist or updates the value of an existing variable.
The second step shows that variables work differently in Python than in other programming languages. In Python, variables aren’t containers for objects. Python variables point to a value or object through its memory address. They store memory addresses rather than objects.
This behavior difference directly impacts how data moves around in Python, which is always by reference. In most cases, this difference is irrelevant in your day-to-day coding, but it’s still good to know.
The Assignment Operator
The central component of an assignment statement is the assignment operator. This operator is represented by the =
symbol, which separates two operands:
- A variable
- A value or an expression that evaluates to a concrete value
Operators are special symbols that perform mathematical, logical, and bitwise operations in a programming language. The objects (or object) on which an operator operates are called operands.
Unary operators, like the not
Boolean operator, operate on a single object or operand, while binary operators act on two. That means the assignment operator is a binary operator.
Note: Like C, Python uses ==
for equality comparisons and =
for assignments. Unlike C, Python doesn’t allow you to accidentally use the assignment operator (=
) in an equality comparison.
Equality is a symmetrical relationship, and assignment is not. For example, the expression a == 42
is equivalent to 42 == a
. In contrast, the statement a = 42
is correct and legal, while 42 = a
isn’t allowed. You’ll learn more about illegal assignments later on.
The right-hand operand in an assignment statement can be any Python object, such as a number, list, string, dictionary, or even a user-defined object. It can also be an expression. In the end, expressions always evaluate to concrete objects, which is their return value.
Read the full article at https://realpython.com/python-assignment-operator/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
January 16, 2023 at 07:30PM
Click here for more details...
=============================
The original post is available in Planet Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment