GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Docs
  • Python
  • Variables

This view

  • Original page
  • New address

GeezerVison is showing maker.makecode.com — Variable Declarations

Skip to main content
About FAQ Projects GitHub
Reference
music pins control serial
Blocks
On Start
Loops
repeat for while for of
Logic
if Boolean
Variables
assign change var var
Math JavaScript blocks Custom blocks
Python
Calling Sequencing Variables Operators Statements Functions Classes
JavaScript
Calling Sequencing Variables Operators Statements Functions Types Classes Interfaces Generics
Types
Number String Boolean Array Function
Miscellaneous
About Support Translate Sharing projects Offline support Save Reset Beta version Home page content
Developers
Command Line Interface Visual Studio Code support Blocks Embed

Variable Declarations

Declaring a variable is simply a matter of just assigning a value to a variable identifier:

a = 10

Block-scoping

When a variable is declared with an assignment, it uses what some call lexical-scoping or block-scoping. Block-scoped variables are not visible outside of their nearest containing block or for-loop.

def f(inp):
    a = 100

    if inp:
        # Still okay to reference 'a'
        b = a + 1
        return b

    # Error: 'b' doesn't exist here
    return b

Here, we have two local variables a and b. a's scope is limited to the body of f while b's scope is limited to the containing if statement's block.

Another property of block-scoped variables is that they can't be read or written to before they're actually declared. While these variables are "present" throughout their scope, all points up until their declaration are part of their temporal dead zone. This is just a sophisticated way of saying you can't access them before the first time they are assigned.

a++ # illegal to use 'a' before it's declared
a = 0

Edit this page on GitHub
Edit template of this page on GitHub
Edit template of this page on GitHub

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.