5-4 全局變量

1、函數內部使用全局變量時,需要申明global

 1 name = '小明'  # 定義一個全局變量name,并給它賦值'小明'
 2 stus = []  # 定義一個空list
 3 # list、字典、集合
 4 
 5 def a():
 6     # 字符串、int、float、元組  需要聲明global
 7     global name  # 函數內部使用局部變量時,需要申明global
 8     stus.append('abc')  # 往空列表中增加abc
 9     name = '哈哈哈'  # 將'哈哈哈'賦值給name
10     # 輸出變量name,調用a函數的時候,因為申明了是全局變量,所有會替換全局變量的初始值
11     print(name)
12 
13 def b():
14     print('stus',stus)  # 輸出stus列表中的值
15     print(name)  # 打印name的值
16     age = 18  # 定義一個變量age,把18賦值給age
17     print(age)  # 輸出age的值
18 a()  # 調用a函數
19 b()  # 調用b函數

?

2、全局變量及函數調函數

 1 money = 500  #全局變量money
 2 def test(consume):  # 傳入一個參數:消費金額
 3     # 把消費后的金額返回給函數
 4     return money - consume
 5 
 6 def test1(money):  # 將money傳給test1函數
 7     # 調用test(money)這個函數,將money傳給test函數,500-500,返回的0元
 8     # + money   500
 9     return test(money) + money
10 
11 # 調用test1函數,定義一個變量money來接收返回的結果
12 money = test1(money)
13 print(money)  # 500

?

3、

 1 def test():
 2     global a  # 申明a是一個全局變量
 3     a = 5
 4 
 5 def test1():
 6     c = a + 5
 7     return c
 8 print(test())  # None
 9 res = test1()
10 print(res)  # 10

?

轉載于:https://www.cnblogs.com/hushaoyan/p/10068262.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278752.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278752.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278752.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

黑客攻防:從入門到入獄_每日新聞摘要:游戲服務黑客被判入獄27個月

黑客攻防:從入門到入獄On Christmas day, 2013, many delighted people opened up new Xbox and Playstation gifts. That excitement turned to disappointment when they were unable to log onto game services and play. Now the hacker responsible will spend 27 months …

Self Introduction

名流時尚服飾 dior 夏季 男裝 男士t恤襯衫衛衣休閑褲牛仔褲英倫 socool 搜酷女包◆任選兩款正價包包郵◆5周年店慶◆5折瘋搶 紫紫 超人氣包郵特價創意家居收納壓縮袋飾品服飾配件包包 socool 搜酷女包◆任選兩款正價包包郵◆5周年店慶◆5折瘋搶 dior 風格 CF Homme 男裝 男士t恤…

爬蟲notes

‘’’ 爬取思路: 1、requests(url) 2、requests json 3、requests XPath 4、requests BeautifulSoup 5、selenium 6、scrapy框架 7、scrapy-redis 及分布式 OS: import os os.system(“C: && p.txt”) os.system(“p…

Android 電量優化

Android系統上App的電量消耗主要由cpu、wakelock、數據傳輸(流量和wifi)、wifi運行、gps、other senior組成,而耗電異常也是由于這幾個模塊的使用不當。 BroaddcastReceiver 為了減少應用損耗的電量,代碼中需要盡量避免無用的操作代碼的執行 …

如何下載手機的App Store中不再存在的應用程序

Smartphone app stores are well established at this point, and as much as we love to see new apps become available, that also means the inevitable: sometimes apps go away. Here’s what you can do if your favorites disappear. 在這一點上,智能手機應…

Q_learning簡介與實例

1、算法思想 QLearning是強化學習算法中value-based的算法,Q即為在某一環境下,Q(state,action)在某一時刻的 s 狀態下(s∈S),采取 動作a (a∈A)動作能夠獲得收益的期望,環境會根據agent的動作反饋相應的回…

吳穎二:12.27 午評 地緣政治一波未平一波又起,千三可到?

前言:圣誕節后首個交易日,金銀油均走出了大行情。美國因導彈項目制裁朝鮮兩名官員,地緣局勢再升溫黃金本周能否突破1300關口?油價刷新兩年半高位后,一個重要指標卻已暗示短期內或面臨風險…… 朝鮮局勢進一步惡化的同時…

2-1 gradle安裝

因為Gradle是基于JVM的,所以一定要確保本機已經安裝了JDK,我們可以通過java -version來驗證一下是否已經安裝了JDK。 bin目錄里面是兩個可執行文件,一個是Windows下面的可執行文件,還有一個就是類Unix文件系統的可執行文件。所有的…

Django中session和cookie簡單的使用

一、簡單的理解 session和cookie是request下的兩個對象,操作他們的值就是在操作字典,設置他們的屬性就是調用方法。 會話(Session)跟蹤是Web程序中常用的技術,用來跟蹤用戶的整個會話。Web應用程序是使用HTTP協議傳輸…

攝影中的曝光補償是什么?

When you use your camera in some automatic modes like Program—or one of the semi-manual modes like Aperture Priority or Shutter Speed Priority—you don’t give up total control over everything: you can still control the exposure using exposure compensatio…

ajax回調打開新窗體防止瀏覽器攔截方法

2019獨角獸企業重金招聘Python工程師標準>>> 問題剖析: function click_fun(){ window.open("www.baidu.com");//能打開 $.ajax({ url: ${pageContext.request.contextPath}/activity/savePrizes.htm, type: post, dataType: json, data: data…

ES6學習--對象屬性的遍歷

ES6一共有5種方法可以遍歷對象的屬性。 (1)for...in for...in循環遍歷對象自身的和繼承的可枚舉屬性(不含Symbol屬性)。 (2)Object.keys(obj) Object.keys返回一個數組,包括對象自身的&#xff…

多點認證wi-fi_準備使用Wi-Fi 6:認證將于2019年第三季度啟動

多點認證wi-fiThe Wi-Fi Alliance already announced Wi-Fi 6 back in October. Today, it’s announcing the details of the Wi-Fi 6 certification process, which will launch in the third quarter of 2019. Expect many new Wi-Fi 6 devices later this year. Wi-Fi聯盟已…

jQuery初識和常用事件(一)

文章目錄一、jQuery二、入口函數三、選擇器選擇器小結 ★全部選擇器參考 ☆四、常用的 jQuery 事件方法事件寫法鼠標事件元素事件鍵盤事件文檔/窗口事件全部事件方法參考 ☆五、效果事件顯示與隱藏:hide,show,toggle淡入和淡出:fa…

nginx內嵌變量

FORWARD:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables ngx_http_core_module模塊支持內嵌變量,變量名與Apache服務器對應。 首先,這些變量可以表示客戶端的請求頭字段,諸如$http_user_agent、$ht…

pdf文檔遇到了共享沖突_如何將鏈接共享為PDF格式的Google文檔鏈接

pdf文檔遇到了共享沖突Using Google Docs is a great way to collaborate on and share documents. Sometimes, though, you want to provide somebody with a PDF instead of an editable document. Google Docs now lets you edit your sharing link to provide a PDF. Best …

Visual Studio 2019 preview中體驗C# 8.0新語法

準備工作: Visual Studio 2019 Preview版本中并沒有包含所有的C# 8.0的新功能,但目前也有一些可以試用了。在開始之前,需要進行入兩項設置: 將Framework設置為.net core 3.0 將C#語法設置為8.0 也可以直接編輯.csproj文件&#x…

jQuery 對HTML的操作(二)

文章目錄一、jQuery獲取、設置HTML標簽的內容和屬性獲得內容 - text()、html() 以及 val()獲取屬性 - attr(),prop()二、增刪 HTML 的內容增加刪除三、操作CSSaddClass 添加removeClass 刪除toggleClass 切換css 獲取與設置所有操作html、css方法參考 ☆四、操作元素…

roku能不能安裝軟件_如何在Roku中使用Google Assistant

roku能不能安裝軟件As more of our devices connect to each other, it’s always nice to know that different products from different companies work together. A Chromecast isn’t expensive, but being able to use your TV directly with Google Assistant is better.…

在線建立或重做mysql主從復制架構方法(傳統模式和GTID模式)【轉】

mysql主從復制架構,是mysql數據庫主要特色之一,絕大多數公司都有用到。 而GTID模式是基于事務的復制模式的意思,發展到現在也是越來越多人用。 以前很多文章,介紹搭建mysql主從復制架構,是需要停止應用服務來做的,對于生產環境&am…