Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

CISC101 Lesson 22

Variable scope determines where a variable can be accessed in a program. Local variables defined inside a function are only visible within that function, while global variables defined outside of any function can be accessed anywhere. When a function is called, local variables are created and destroyed upon function termination. Global variables exist for the entire program runtime. Constants are conventionally defined as global variables in all uppercase. Syntax errors occur during coding, while runtime errors happen during program execution. Exceptions represent runtime errors and can be caught using try/except blocks.

Uploaded by

raasiboi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

CISC101 Lesson 22

Variable scope determines where a variable can be accessed in a program. Local variables defined inside a function are only visible within that function, while global variables defined outside of any function can be accessed anywhere. When a function is called, local variables are created and destroyed upon function termination. Global variables exist for the entire program runtime. Constants are conventionally defined as global variables in all uppercase. Syntax errors occur during coding, while runtime errors happen during program execution. Exceptions represent runtime errors and can be caught using try/except blocks.

Uploaded by

raasiboi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lesson 22 Variable Scope

Variable Scope
Variable scope is the block of code in which you can access it
(x) local variable if defined inside a piece of code
-

Only visible inside a function


Local scope!

(x) global variable if defined outside of code


- Visible everywhere in a program
- Global scope!

Example
Def fun1():
B=2
Def fun2()
A=5
Print(A)
Print(B)
Fun2() >> 5 then error because B wasnt defined

Def fun1()
B=2
Def fun2()
B=8
Fun1()
Print(b)
Fun2()
8 calling a function wouldnt matter because it looks at the local variable

Variable Lifetime
-

The period of time that a variable exists is called its lifetime.

Local variables are automatically created when a function is called and


destroyed when the function terminates
Thus the values of local variables are not retained from one function call to
the next.

What if you need a variable that is visible and will survive


-

A global variable is a variable that is defined outside of any function


definition
They have global scope

A global variable is available to any function


Global Variables die after the program terminates

Constants
-

By convention, placed as global variables


ALL_CAPS to make them easy to see
For example:
INCH_TO_CM = 2.54

Errors
-

Syntax error is a problem with how you wrote a line a code, before it runs
Run-time errors happen because of unforeseen problems when you run the
code

Exceptions
-

An exception is a runtime error


Every run-time error in python has a name this is the type of exception

Catching exceptions
Try:
Try_statements
Except exception:
Except_statements
Exception is the name of the exception you are catching
Try_statements is code that could generate a run time error. The code here stops as
soon as there is an error

You might also like