Assignment

ASnake Documentation

Table Of Contents

Summary:

Assignment operator to create new variables with values or modify existing ones.

Syntax =:

Here is a list of it's syntax aliases:

  1. =
  2. is

A variable is a name which refers to a memory which stores a value. That value can be any type. The variable name can be any character within the unicode set (including emojis) besides whitespace characters. The first character of a variable cannot be a number, but may contain numbers after the first character.

The assignment syntax signifies the value you wish to assign to the variable. The operator is not required when the next token is a static datatype (int, float, str, list, tuple, dict, set) or variable reference.

myVariable = [1,2,3,4]
hello_world = "Hello, World!"
myFavoriteNumber is 12
♥♥♥ is 'LOVE'*3
pi 3.14

Assignment operator must be preceded by a variable or index reference.

The alias is can be interpreted as a comparison when a assignment was prior, called in a function, or in a conditional expression.

var is 1 is 2
print var
# result: False

if 4 is 4 do 'four'
# result: four

Assignment Operators:

Putting a binary Python operator (besides conditions) prior to the = will modify an existing variable while also operating on it with the operator. The variable must be already defined.

Example:

x = 2
x += 2
print x
# result: 4

x //= 2
# result: 2

You can see examples of all the assignment operators here.

Walrus :=:

Python introduced walrus in Python version 3.8. Walrus allows you to define or modify a variable inside an expression, instead of requiring that be specified at the start of the expression. A walrus requires that you mark its start and end with parenthesis.

x = 99
if ( x := 12 ) > 24 do 'stuff'
# no result
print x
# result: 12
Scroll to top