Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet86/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   82   83   84   85   86   87   88   89   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

WEDNESDAY EXERCISES


  1. . Full Name: Create a function that takes in a first and last name and returns the two names joined together.

  2. . User Input: Within a function, ask for user input. Have this function return that input to be stored in a variable outside of the function. Then print out the input.


Today we learned how to retrieve information from a function. This will allow us to save the data it manipulates for later use.
Thursday: Scope
Today we’re going to discuss an important concept called scope. This concept deals with accessibility of variables declared within a program. We’ll go over the different types of scope and how to do handle them.
To follow along with this lesson, let’s continue from our previous notebook file
Week_05” and simply add a markdown cell at the bottom that says, “Scope.”

Types of Scope



In Python, there are three types of scope: global, function, and class. We haven’t gone over classes just yet, so we’ll discuss class scope in a later chapter. Without knowing it, we’ve used the other two types of scope. Global scope is when you declare a variable to be accessible to an entire file or application. Most of the variables we’ve declared so far have been global; however, in most programs you write, you will want to avoid global variables. It’s okay in Jupyter Notebook for now though. Function scope is in reference to variables being declared and accessible only within functions. A variable declared inside of a function cannot be accessed outside of the function, as once the function terminates, so do the variables declared within it.

Global Scope Access


When global attributes are defined, they’re accessible to the rest of the file. However, we must keep in mind how function scope works. Even when you declare a variable accessible to the entire file, it will not be accessible within the function. Let’s see an example:

# where global variables can be accessed number = 5 def scopeTest( ):
number += 1 # not accessible due to function level scope scopeTest( )
CHapTeR 5 FUnCTIonS
Go ahead and run the cell. We’ll end up receiving an error because the function is limited to variables declared within it or passed in.
Note When passed in, it only passes the value, not the variable.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   82   83   84   85   86   87   88   89   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish