0% found this document useful (0 votes)
23 views

Lecture 3

This document discusses Python variables including variable assignment, dynamic typing, naming conventions, and variable scopes. It covers topics such as local, global, enclosing, and built-in scopes as well as using the global and nonlocal keywords.

Uploaded by

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

Lecture 3

This document discusses Python variables including variable assignment, dynamic typing, naming conventions, and variable scopes. It covers topics such as local, global, enclosing, and built-in scopes as well as using the global and nonlocal keywords.

Uploaded by

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

APPLICATION LABORATORY II

Sherina Sally

Python Variables Department of ICT


Faculty of Technology,
University of Colombo

OUTLINE

• Introduction to variables

• Variable assignment
All Rights Reserved

• Dynamic typing

• Naming conventions

• Variable scope

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 2

1
VARIABLES
• Python variables are named entities to store data
x = 10
• Created at the moment when a value is assigned
x = ‘ Joe’ x = “ Joe”

All Rights Reserved


• No explicit variable declaration

• Dynamically typed

• The type of the value assigned can be changed

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 3

NAMING CONVENTIONS
• Start with a letter or the underscore( _ ) character

• Cannot start with a number


All Rights Reserved

• Variable names can only contain letters, numbers and underscores (A-z, 0-9, and _ )

• Reserved keywords cannot be used

• Use meaningful variable names

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 4

2
MULTIPLE VARIABLE ASSIGNMENT

• Value assignment for multiple variables,


p, q, r = 2, “red”, 45.5
• Multiple values assigned to multiple variables

All Rights Reserved


• Single value assigned to multiple variables
p=q=r=5

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 5

SCOPES OF VARIABLES …(1)


• Local scope
- A variable created inside a function body

- Can be accessed only within the function


All Rights Reserved

• Global Scope

- Variables created outside all the functions and classes

- Can be accessed from anywhere within the program

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 6

3
SCOPES OF VARIABLES …(2)
• Enclosing scope
- Applies in nested functions

- The innermost function can access variables of the outer function

All Rights Reserved


- The outer function cannot access the variables of the innermost function

• Built-in Scope

- The accessibility of built-in keywords/ functions without importing any module

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 7

SCOPES OF VARIABLES …(3)


x = “global x”

def outer():
x = “outer x”
All Rights Reserved

def inner():
x = “inner x”
print(x)
inner()
x = max(4,5,3,…..)
print(x)
outer()
print(x)

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 8

4
global
• Enforce a variable to behave in the global scope

• Local variables can be accessed out of the function

All Rights Reserved


x = “global x”

def inner():
global x
x = “inner x”
inner()
print(x)

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 9

nonlocal
• In nested functions, variables in the outer function cannot be changed in the inner function

• nonlocal statement will prevent this behavior

• Enables to change the values of the variables in the outer function from the inner function
All Rights Reserved

def outer():
x=5
def inner():
nonlocal x
x=x+3
print(x)
inner()

outer()
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 10

5
Department of ICT, Faculty of Technology, University of Colombo

You might also like