Python - Variable

Card Puncher Data Processing

About

Language - (Variable | Field) in Python.

A variable name must start with a letter and must exclude certain special symbols such as the dot (period). The underscore is allowed in a variable name.

mynum = 4 + 1

The variable being assigned to is called the left-hand side of an assignment, and the expression whose value is assigned is called the right-hand side.

A variable can be bound to a value of any type. You can rebind mynum to a string:

>>> mynum = 'Brown'

An assignment statement binds a variable to the value of an expression, not to the expression itself. Python fi rst evaluates the right-hand side and only then assigns the resulting value to the left-hand side.

The binding lasts until you assign some other value to the variable or until you end your Python session. It is called a top-level binding.

Name resolution

Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42:

i = 10
def f():
    print(i)
i = 42
f()

Management

Assignment

Conditional

expression if condition else expression
>>> x=1
>>> x if x<0 else -x
-1
>>> x if x>0 else -x
1

Unpacking

Initialization and extraction of variables via Python - (Un)Packing

List

Function Description
dir() list of variables in scope
globals() a dictionary of global variables
locals() a dictionary of local variables

Delete

del variableName





Discover More
Card Puncher Data Processing
Python - (Un)Packing

Initialization and extraction of variables Unpacking: A convenient way to initialize variables from a list Sequence packing: The values 12345, 54321 and 'hello!' are packed together in a tuple...
Card Puncher Data Processing
Python - (def|define) function (procedure)

def function are one type of function declaration. See . Functions (called also procedure) are defined using the keyword def that stands for define where: the argument is the calling variable...
Card Puncher Data Processing
Python - Data Type

data (type|structure) in Python of a name generally a variable objecttypeintsstringsfunctionsclassesclass - an integer is a positive or negative whole number. float booleans (True...
Card Puncher Data Processing
Python Package - Module

On a file system, package are the directories and modules are the files within this directories. A module is a file that contains structure definitions including : variables class and functions....



Share this page:
Follow us:
Task Runner