0% found this document useful (0 votes)
51 views8 pages

Discussion Forum Cs 1101 Unit 2

The document discusses variable scopes in Python functions. It provides examples demonstrating that: 1) Variables defined inside a function have local scope and can only be accessed within the function. 2) Variables defined outside of functions have global scope and can be accessed inside or outside of functions. 3) If a variable with the same name is defined both globally and locally, the local variable takes precedence and the global variable cannot be accessed within the function.

Uploaded by

Julio Louzano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

Discussion Forum Cs 1101 Unit 2

The document discusses variable scopes in Python functions. It provides examples demonstrating that: 1) Variables defined inside a function have local scope and can only be accessed within the function. 2) Variables defined outside of functions have global scope and can be accessed inside or outside of functions. 3) If a variable with the same name is defined both globally and locally, the local variable takes precedence and the global variable cannot be accessed within the function.

Uploaded by

Julio Louzano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Example 1

def say_something(something: str):   #something is the parameter


    print("crazy", something)

say_something("days")    #days is the argument


The output shows: crazy days

Example 2
def say_something(something: str):   #something is the parameter
    print("crazy", something)

#days is the argument


say_something("days")    #value
some = "friday"
say_something(some)      #variable
say_something(3+9)       #expression

the output shows:


crazy days
crazy friday
crazy 12

Example 3

def say_something():
    some = 50

print(some)

the output shows:


Exception has occurred: NameError
name 'some' is not defined

A variable declaration or alteration defined within a function or a local scope


cannot affect a variable of another function or a global scope.

Example 4

def say_something(some):
    some_inside = some
    return some_inside

say_something(6)
print(some)

the output shows:


Exception has occurred: NameError
name 'some' is not defined

If we try and access the parameter from outside of the function, we will receive a name error is
not defined as the parameter is not defined outside of this function.
Example 5

some = 6
print(some)
def something():
    some = 14
    print(some)
something()

the output shows:


6
14

The variable that defined


outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
it in the same function
The variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
it in the same function
The variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
it in the same function
e variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
it in the same function
The variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
it in the same function
The variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
The variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
The variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
The variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
The variable that defined
outside the functions has
a global scope which
mean we can
call it anywhere but the
variable inside the
function has a local
scope we can just call
The variable that defined outside the functions has a global scope which mean we can call it
anywhere but the variable inside the function has a local scope we can just call it in the same
function

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy