Global, Local and Nonlocal variables in Python

Global, Local and Nonlocal variables in Python

irst of all, I’m not the one on that image above. I’m just a benevolent writer who is here to talk about one of the most confusing concepts in Python programming “Global, Local and Nonlocal variables”. I know after reading the title you will be like “Why should I even worry about this”. Well, the answer is sometimes not knowing these teeny tiny itsy bitsy would cost you a lot. So without further ado, let’s get started.

F

irst of all, I’m not the one on that image above. I’m just a benevolent writer who is here to talk about one of the most confusing concepts in Python programming “Global, Local and Nonlocal variables”. I know after reading the title you will be like “Why should I even worry about this”. Well, the answer is sometimes not knowing these teeny tiny itsy bitsy would cost you a lot. So without further ado, let’s get started.

In programming languages like C/C++, every time a variable is declared simultaneously a memory would be allocated this would allocation would completely depend on the variable type. Therefore, the programmers must specify the variable type while creating a variable. But luckily in Python, you don’t have to do that. Python doesn’t have a variable type declaration. Like pointers in C, variables in Python don’t store values legitimately; they work with references highlighting objects in memory.


Topics

The list of topics that would be covered in this article is given below:

  • *Variables *— A quick introduction
  • Global Variables **— How to get rid of `UnboundLocalError**`
  • Local Variables — How to get rid of **NameError**
  • *Nonlocal Variables *— When and how to use them

Variables

A variable is more likely a _container _to store the values. Now the values to be stored depends on the programmer whether to use integer, float, string or etc.

A Variable is like a box in the computer’s memory where you can store a single value. — Al Sweigart

_Unlike in other programming languages, in Python, you need not declare any variables or initialize them. _Please read _[**_this](https://stackoverflow.com/a/11008311/11646278).**

Syntax

The general syntax to create a variable in Python is as shown below:

**variable_name****value**

The **variable_name**in Python can be short as sweet as **a, b, x, y, ...** or can be very informative such as **age, height, name, student_name, covid, ...**

Although it is recommended keeping a very descriptive variable name to improve the readability.

python3 python-programming python programming variables

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Guide to Python Programming Language

Guide to Python Programming Language

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).