CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Python · 6 min · free

Scope of Variables: Local and Global

6 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn Python variable scope for CBSE Class 12: local and global variables, the global keyword, LEGB, and exam output questions. Scope is the part of a program where a variable name can be used.

Follows the syllabus of: CBSE Class 12 Computer Science (083), Cambridge IGCSE Grade 10 Computer Science (0478)

On screen in this lesson

Three new words

Scope: the part of a program where a name can be used
Global variable: made outside every function
Local variable: made inside a function

Scope drawn as nested boxes

BoxHoldsCan see
Global (outer) boxcity = "Pune"city only
weather() boxtemp = 31temp and city

Two scope errors

MistakeErrorWhy
Use local outsideNameErrorbox is closed
score = score + 1UnboundLocalErrorlocal not set yet

Rules for global

Write global before using the name
Needed only to change a global, not to read it
Too many globals make bugs hard to find
Better: pass values in and return them

How Python finds a name: LEGB

OrderScopeWhere
1stLocalcurrent function
2ndEnclosingouter function
3rdGlobaltop of the file
4thBuilt-inprint, len

Quick recap

Scope is where a name can be used
Local: inside a function; global: outside
Storing inside a function makes a new local
global lets a function change an outer variable
Python searches Local, Enclosing, Global, Built-in

Quick answers

Why does score = score + 1 inside a function give UnboundLocalError?

The = makes score local, so Python reads a local score that has no value yet.

When do you need the global keyword?

Only to change a global variable inside a function, not to read it.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. You change a variable inside a function, but outside, the old value is still there. Why? Today we will learn scope, local and global variables, the global keyword, and how Python finds a name.

First, three new words. Scope means the part of a program where a variable name can be used. A global variable is created outside every function, at the top level of the file. A local variable is created inside a function, and it lives only while that function runs.

Picture scope as boxes inside boxes, and the weather box sits inside the global box. The big outer box is the global scope, and it holds city, so from outside you can see only city. The weather function gets its own small box inside it, holding temp, and from inside you can look out and see city too. But outside cannot look into the small box, so temp is hidden there.

Here is the same picture in code. City is global, and temp is local to the weather function. Inside the function, both names work, so it prints Pune thirty one. Outside, print of city still works. But if we added print of temp at the bottom, Python would stop with a name error. Temp is hidden in the small box.

What if a function uses the same name as a global? A single equals sign stores a value, and storing inside a function creates a new local variable. So the inner score is a different variable, which only hides the global one. Inside, it prints fifty. Outside, the global score is untouched, so it prints ten.

Two errors come from scope. Using a local variable outside its function gives a name error, because Python cannot find that name. Inside a function, score equals score plus one, with no global line, gives an unbound local error. The equals sign makes score local, so Python tries to read a local score that has no value yet.

To change a global variable inside a function, use the global keyword. The line global wallet tells Python, do not make a new local, use the outer wallet. The wallet has one hundred rupees, and a recharge costs thirty. The minus sign subtracts, so the global wallet becomes seventy, and seventy is printed outside.

Keep four rules in mind. Write the global line before the name is used in the function, or you get a syntax error. You need global only to change a global variable, not just to read it. Many globals make bugs hard to find, because any function can change them. Usually it is better to pass values in and return the answer.

Name resolution means how Python decides which variable a name refers to. It searches four boxes in a fixed order, remembered as L E G B. First, the local scope of the current function. Second, the enclosing scope, which is an outer function around a function written inside it. Third, the global scope at the top of the file. Fourth, the built-in names that Python already knows, such as print and len.

Here, a function named inner is written inside a function named outer. Inner has no local variable with this name. So Python moves out one box, finds the enclosing value, and prints enclosing. It stops at the first match. Pause and predict. If we delete the third line, what prints? Python finds nothing enclosing, so it moves out to the global box and prints global. One more keyword for advanced learners. To change the enclosing variable from inside inner, use the nonlocal keyword, not global.

Now two output questions, the kind exams love. Inside change, marks is a new local variable holding two. So it returns two times three, which is six. The global marks was never touched, so it is still five. The answer is six, then five.

This time the function uses global, so every call changes the same count. Python works out the print items from left to right. The first call makes count five. The second call adds four more, making nine. By the time Python reaches the last item, count is already nine. So it prints five, nine, nine.

Some exams show scope in pseudocode. The arrow means store a value. Total is declared outside the procedure, so it is global. Extra is declared inside the procedure, so it is local. In Java and C plus plus too, a variable declared inside a method or function is local to it.

Let us revise what we learned today. Scope is the part of the program where a name can be used. Local variables live inside a function, and global variables live outside. Storing a value inside a function makes a new local variable. The global keyword lets a function change an outer variable. And Python always searches local, enclosing, global, then built-in.

Courses that teach this

CourseUnit
CBSE Class 12 Computer Science (083)Computational Thinking and Programming – 2
ICSE Class 10 Computer ApplicationsEncapsulation
ISC Class 12 Computer Science (868)Statements and Scope
Cambridge IGCSE Grade 10 Computer Science (0478)8. Programming
Programming All levels PythonFunctions
Programming All levels C++Control Flow and Functions

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →