99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python字典,函數(shù),全局變量代碼解析
Python字典,函數(shù),全局變量代碼解析
2018-02-12
收藏

Python字典,函數(shù),全局變量代碼解析

字典    
dict1 = {'name':'han','age':18,'class':'first'}
print(dict1.keys())    #打印所有的key值
print(dict1.values())   #打印所有的values值
print("dict1['name']:",dict1['name'])   #打印name相對應(yīng)的value值
print(dict1.get('name'))  #通過字典的get方法得到name相對應(yīng)的value值
 
dict1['age']=28  #字典的修改相當(dāng)于重新賦值?。?!
 
dict1['address']='beijing'  #字典的增加是:dict[key] = value 這樣的形式
 
del dict1['name']  #刪除字典中的一個元素
dict1.clear()    #字典的清空,返回一個空字典
# del dict1     #刪除字典,字典就完全刪除了

字典用法注意:

1、鍵是不允許相同的,如果相同,后面的會覆蓋前面的

2、鍵是不可變的,所以只能用數(shù)字、字符串、元組來擔(dān)當(dāng)   
dict2 = {(1,2):5,"元組":(4,5)}  #字典里的元素只能用不可變的數(shù)據(jù)類型!??!
print(dict2)
print(dict2['元組'])
print(dict2[(1,2)])
 
for i in dict2.keys():      #取出的數(shù)值更干凈?。?!謹(jǐn)記老師教導(dǎo)
  print("字典中的key值為:",i)
for j in dict2.values():
  print("字典中的values值為:",j)

函數(shù)

1、函數(shù)的定義

函數(shù)是實(shí)現(xiàn)特定功能而封裝起來的一組語句塊,可以被用戶調(diào)用

2、函數(shù)的分類

自定義函數(shù);預(yù)定義函數(shù)(系統(tǒng)自帶,lib自帶)

3、使用函數(shù)的好處

降低編程難度、將大問題分解為若干小問題、可以多次調(diào)用

4、函數(shù)語法

定義

def函數(shù)名字(參數(shù)):

函數(shù)體

return語句#不帶表達(dá)式的return相當(dāng)于返回none

調(diào)用

函數(shù)名字

以下是函數(shù)的幾種:
#定義函數(shù),函數(shù)名最好以_分割
def print_str(str):
  print(str)
  return
# 調(diào)用函數(shù)
print_str("調(diào)用一下")
    
# 函數(shù)傳遞
#所有參數(shù)在python里都是按引用傳遞
#一句話:要變都變?。?!
def charge_me(mylist):
  mylist.append([1,2,3,4])
  print("函數(shù)內(nèi)取值:",mylist)
  return
mylist = [10,20,30]
charge_me(mylist)
print("函數(shù)外取值:",mylist)   #函數(shù)外和函數(shù)內(nèi)打印是相同的!??!
    
#函數(shù)的賦值引用
def print_info(name,age=3):
  print("name",name)
  print("age",age)
  return
# print_info(name="xiao",age=18)
print_info(age=50,name="xiao")    #python中顛倒是可以的?。?!
print_info(name='haha')    
#函數(shù)的不定長參數(shù)
def p_info(arg1,*vartuple):
  print("輸出:",arg1)
  for var in vartuple:
    print(var)
  return
p_info(10)
p_info(70,60,50,40,30)
    
匿名函數(shù)lambda,了解即可
 # 1、lambda只是一個表達(dá)式,而不是一個代碼塊,函數(shù)體比def簡單很多。僅僅能在lambda表達(dá)式中封裝有限的邏輯
 # 2、lambda[arg1[,arg2,...argn]]:expression
 sum1 = lambda arg1,arg2:arg1+arg2
 print("相加后的值為:",sum1(10,20))    
# return語句
def sum2(arg1,arg2):
  total = arg1+arg2
  print("函數(shù)內(nèi):",total)
  return total           #把total去掉之后返回none
abc = sum2(10,40)
print("函數(shù)外:",abc)

#全局變量和局部變量
#全局變量比較容易出問題,能不用就不用
    
total = 0
def sum3(a,b):
  total = a+b
  print("函數(shù)內(nèi)(局部變量)的值為:",total)
  return total
# total = sum3(10,400)
sum3(20,70)
print("函數(shù)外(全局變量)的值為:",total)    
count = 1
def do_st():
  global count     #全局變量:global
  for i in (3,4,5):  #循環(huán)三次
    count += 1
    # print(count)
do_st()
print(count)
# 思路:當(dāng)count=1時進(jìn)入循環(huán)+1并賦值給count
#     在for循環(huán)三次后為3+1=4
#     count是全局變量,最后打印出的結(jié)果為4

小練習(xí)
# 不能創(chuàng)建字典的語句是C (字典中的元素不能以列表作為key)
# A、dict1 = {}
# B、dict2 = { 3 : 5 }
# C、dict3 = {[1,2,3]: “uestc”}
# D、dict4 = {(1,2,3): “uestc”}
 
#以下代碼輸出什么?輸出的是6
# 思路:原始key的值為1,
#    copy給另一個字典值為1,
#    重新賦值原來的字典值為5,
#    所以相加等于6
dict1={'1':1,'2':2}
theCopy=dict1.copy()
dict1['1']=5
sum=dict1['1']+theCopy['1']
print(sum)
 
# 合并生成新的字典
dict1 = {3:"c", 4:"d"}
dict2 = {1:"a", 2:"b"}
dict2.update(dict1)    #更新添加dict1進(jìn)dict2
print(dict2)
 
# 標(biāo)準(zhǔn)日期輸出
a = "20170303"
b = a[:4]
c = a[4:6]      #構(gòu)思:通過列表分割的方式實(shí)現(xiàn)
d = a[6:]
print("格式化后輸出的日期是:%s年%s月%s日"%(b,c,d))

無return函數(shù),返回什么?

答:在函數(shù)中無return函數(shù),返回none

如何在一個function里面設(shè)置一個全局的變量?

答:在函數(shù)體內(nèi)定義一個全局的函數(shù)global
#隨機(jī)生成驗證碼的兩種方式:    
import random
list1=[]
for i in range(65,91):
  list1.append(chr(i))  #通過for循環(huán)遍歷asii追加到空列表中
for j in range(97,123):
  list1.append(chr(j))
for k in range(48,58):
  list1.append(chr(k))
ma = random.sample(list1,6)
print(ma)     #獲取到的為列表    
ma = ''.join(ma)  #將列表轉(zhuǎn)化為字符串
print(ma)    
import random,string
str1 = "0123456789"
str2 = string.ascii_letters
str3 = str1+str2
ma1 = random.sample(str3,6)
ma1 = ''.join(ma1)
print(ma1)     #通過引入string模塊和random模塊使用現(xiàn)有的方法

總結(jié)

以上就是本文關(guān)于Python字典,函數(shù),全局變量代碼解析的全部內(nèi)容,希望對大家有所幫助

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }