0% found this document useful (0 votes)
10 views7 pages

Oop Presentation1

This document discusses functions and variables in Python. It explains that functions are blocks of code that run when called, and can accept parameters and return values. Variables in Python are dynamically typed and refer to objects. There are three types of variables: global variables defined outside functions, local variables defined inside functions that are only visible there, and nonlocal variables used in nested functions to refer to a parent function's local variable.

Uploaded by

Mohammed Shahab
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)
10 views7 pages

Oop Presentation1

This document discusses functions and variables in Python. It explains that functions are blocks of code that run when called, and can accept parameters and return values. Variables in Python are dynamically typed and refer to objects. There are three types of variables: global variables defined outside functions, local variables defined inside functions that are only visible there, and nonlocal variables used in nested functions to refer to a parent function's local variable.

Uploaded by

Mohammed Shahab
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/ 7

Oop presentation

BY MEHDI HAJJI AND MOHAMMED SHEHAB

Object oriented programming | course 1 | 2021/2/18


Python Functions:

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

To create a function In Python is defined using the def keyword and in order
to function to work we most call it name followed by parenthesis:

Information can be passed into functions as

1- Arguments: arguments are specified after the function name, inside the
parentheses. You can add as many arguments as you want, just separate
them with a comma.

2-Parametars: a parameter is the variable listed inside the parentheses in the


function definition.

PAGE 1
Variables in Python:

A Python variable is a symbolic name that is a reference or pointer to an


object. Once an object is assigned to a variable, you can refer to the object
by that name, in other words it’s a container for data.

In many programming languages, variables are statically typed. That means


a variable is initially declared to have a specific data type, and any value
assigned to it during its lifetime must always have that type.

Variables in Python are not subject to this restriction. In Python, a variable


may be assigned a value of one type and then later re-assigned a value of a
different type

The real deal is what happens when we set a variable, will that depends to
the program language that you use, we use python, will python is a
highly object-oriented language. In fact, every item of data in a Python
program is an object of a specific type or class.

So to create a variable:

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

PAGE 2
There is three types of variables:

1- Global variable:
Variables that are created outside of a function. are known as global
variables.

Global variables are the one that are defined and declared outside a
function and we need to use them inside a function

So Global variables can be used by everyone, both inside of functions and


outside.

The variable s is defined as the string “ISM student” before we call the
function stage2(). The only statement in stage() is the “print section”
statement. As there is no local section, the value from the global section
will be used.

Normally, when you create a variable inside a function, that variable is local,
and can only be used inside that function.

To create a global variable inside a function, you can use


the global keyword.

PAGE 3
2-Local Variables
A variable created inside a function belongs to the local scope of that
function, and can only be used inside that function.
So a variable declared inside the function's body or in the local scope is
known as a local variable.
In order to create a local variable:

As we see the variable (ism), is in local area.

So If you operate with the same variable name inside and outside of a
function, Python will treat them as two separate variables, one available in
the global scope (outside the function) and one available in the local scope
(inside the function):

PAGE 4
In some cases we can use global variable inside the
function:

We also can have both global and local inside of function:

PAGE 5
3- Nonlocal Variables

Nonlocal variables are used in nested functions whose local scope is not
defined. This means that the variable can be neither in the local or the global
scope.

In the above code, there is a nested inner() function. We use nonlocal


keywords to create a nonlocal variable. The inner() function is defined in the
scope of another function.
So in other words if we change the value of a nonlocal variable, the changes
appear in the local variable.

PAGE 6

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