Python之路,day3-Python基礎

來源:互聯網
上載者:User

標籤:str   資料   求和   汽車   update   set   bsp   write   eval   

三級菜單

 1 menu = { 2     ‘北京‘:{ 3         ‘海澱‘:{ 4             ‘五道口‘:{ 5                 ‘soho‘:{}, 6                 ‘網易‘:{}, 7                 ‘google‘:{} 8             }, 9             ‘中關村‘:{10                 ‘愛奇藝‘:{},11                 ‘汽車之家‘:{},12                 ‘youku‘:{},13             },14             ‘上地‘:{15                 ‘百度‘:{},16             },17         },18         ‘昌平‘:{19             ‘沙河‘:{20                 ‘老男孩‘:{},21                 ‘北航‘:{},22             },23             ‘天通苑‘:{},24             ‘回龍觀‘:{},25         },26         ‘朝陽‘:{},27         ‘東城‘:{},28     },29     ‘上海‘:{30         ‘閔行‘:{31             "人民廣場":{32                 ‘炸雞店‘:{}33             }34         },35         ‘閘北‘:{36             ‘火車戰‘:{37                 ‘攜程‘:{}38             }39         },40         ‘浦東‘:{},41     },42     ‘山東‘:{},43 }44 45 current_level = menu46 last_levels = []47 48 49 while True:50     for key in current_level:51         print(key)52     city = input(‘》‘).strip()53     if len(city) == 0:continue54     if city == ‘b‘:55         if len(last_levels) == 0:break56         current_level = last_levels[-1]57         last_levels.pop()58     if city == ‘q‘:59         break60 61     if city not in current_level:continue62     last_levels.append(current_level)63     current_level = current_level[city]

 




1.字典

字典 key:value

#元組,元素不可變
l = ()
#定義符號(),與列表完全一致,唯一不同的是元組內元素不可變
#





dic = {‘name‘:‘alex‘,‘age‘:18}

#查詢
#取值兩種方法
print(dic[‘name‘])
print(dic.get(‘name‘))

#增加
dic[‘ggg‘]=‘x‘ #k:v
print(dic)
#元素是
#無序的

#修改
dic[‘name‘]=‘hjhjj‘


#刪除
del dic[‘name‘]
print(dic)

#字典的key不可變,value可變
#key類型
#key的定義規則:1.不可變,定義一個資料,然後改變資料,id是否改變,id改變為不可變類型,id不改變為可變類型。#數字,字串,元組不可變,(列表,字典可變)。
# 2.只要能雜湊hash,就能當key,hash有數就表明可以定義為key
#3.字典中key是唯一的
#value類型
#value定義規則:任意類型

#dic={}---->dict()---->_init_()
dic3=dict() #dic={}
print(dic3)
dic4=dict(name=‘alex‘,age=18)#dic = {‘name‘:‘alex‘,‘age‘:18}
print(dic4)
dic5=dict({‘name‘:‘alex‘,‘age‘:18})
dic6=dict(((‘name‘,‘alex‘),(‘age‘,18)))


dic = {‘name‘:‘alex‘,‘age‘:18}
#dic.clear()
#print(dic)

dic1=dic.copy()
print(dic1)

import copy
#等於
#copy 淺copy
copy.deepcopy


dic2=dict.fromkeys(‘hello‘,1)
print(dic2)

dic.get(‘name‘)#dic[‘name‘]
print(dic.items())

print(dic.keys())
print(dic)
dic.pop(‘name‘)#刪除

dic.popitem()#隨機刪除

dic.setdefault(‘gender‘,[])#增加

dic1={‘gender‘:‘male‘}
dic.update(dic1)#更新

dic.values()#取字典的value

data = dict.fromkeys([1,2,3],‘alex‘)
print(data)

2.集合
#關係運算
#集合中都是不同的元素,不可重複
s1 = {‘a‘,1,2,3,3,3,3}
s2 = {2,3}
print(s1)

a = {1,2,3,4,6,9}
b = {2,4,9,8,7,5}
#求和,交集
print(a&b)
print(a.intersection(b))
#並集
print(a|b)
print(a.union(b))

#差集 ,去掉b中與a相同的元素
print(a-b)
print(a.difference(b))

#對稱差集,不同的部分
print(a^b)
print(a.symmetric_difference(b))

#子集
print(s1<=s2)
print(s2<=s1)
print(s1.issubset(s2))
print(s2.issubset(s1))

#父集
print(s1>=s2)
print(s1.issuperset(s2))

####集合取值
s3 = {1,2,3,‘a‘}
print(‘a‘ in s3)
for i in s3:
print(i)
####元組用途
t1= (1,2,3)

集合的其他內建方法
s1 = {1,2,3}
s1.update(‘e‘)
s1.update((1,2,3,4))
s2= {‘h‘,‘e‘}


s1.update(s2)
s1.update(‘hello‘)
print(s1)

#增加
s1.add(‘hello‘)
print(s1)

#刪除
s1.pop()#隨機刪除
print(s1)

#指定刪除
s1.remove(‘l‘)
print(s1)

#刪除,傳回值為空白,元素不存在,不報錯
print(s1.discard(‘w‘))
print(s1)


s1 = {1,2,3,‘a‘,‘e‘}
s2={1,2,3}
s1.difference_update(s2)
print(s1)

s1.isdisjoint()
3.檔案處理
#-*- coding:utf-8 -*-
print(open(‘test.txt‘).read())

#read,r:讀模式
#w:建立寫入模式
f = open(‘myfile‘,‘w‘)

#a:增加模式

#刪除模式

#r+;讀寫入模式,追加到最後
#w+:寫讀,清空建立,再寫入
#a+:追加讀,追加到最後

f.closed #檢查檔案是否關閉
f.encoding#列印檔案編碼格式
f.fileno()#檔案在作業系統中的索引值
f.flush()#即時強制重新整理檔案(儲存)
f.isatty()#判斷是不是終端檔案
f.name#列印檔案名
f.newlines#
f.readable()#是否為不可讀檔案
f.seek()#尋找,移動游標到指定位置
f.seek(10)#從第十個欄位開始讀
f.tell()#告訴你游標在檔案中的位置
f.truncate()#截斷,從游標的位置截斷
data = [‘alex\n‘,‘jack\n‘]
f.writelines(data)#將列表按行插入檔案
#eval(data)將字串轉為字典


4.字元編碼

高電壓:1
低電壓:0

電腦只認識數字

字元 ------翻譯------》 數字
a------11

1.記憶體固定使用unicode編碼,硬碟的編碼(即你可以修改的軟體編碼)
2.使用什麼編碼往硬碟存,就用什麼編碼讀
3。程式運行分兩河階段:1.從硬碟讀到記憶體2.python解譯器運行已讀到記憶體的代碼
4.針對一個test.py檔案來說python與nodpad++\vimde區別是多了第二步驟
5.頭部編碼類別型,決定記憶體從硬碟怎麼讀

分享

《消費者行為學》
5分鐘商學院 app:得到

lib兄弟連 php
戲說php

《林達看美國》



 
 

Python之路,day3-Python基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.