Python Road, Day3-python Foundation

Source: Internet
Author: User
Tags shallow copy

Level three Menu

1menu = {2     'Beijing':{3         'Haidian':{4             'Five crossing':{5                 'Soho':{},6                 'NetEase':{},7                 'Google':{}8             },9             'Zhongguancun':{Ten                 'Iqiyi Art':{}, One                 'Autohome':{}, A                 'Youku':{}, -             }, -             'on the ground':{ the                 'Baidu':{}, -             }, -         }, -         'changping':{ +             'Shahe':{ -                 'old boy':{}, +                 'Beihang University':{}, A             }, at             'Tin Tong Court':{}, -             'Huilongguan':{}, -         }, -         'Chaoyang':{}, -         'Dongcheng':{}, -     }, in     'Shanghai':{ -         'Minhang':{ to             "People's square":{ +                 'Fried Chicken Shop':{} -             } the         }, *         'Zhabei':{ $             'Train Warfare':{Panax Notoginseng                 'Ctrip':{} -             } the         }, +         'Pudong':{}, A     }, the     'Shandong':{}, + } -  $Current_level =Menu $Last_levels = [] -  -  the  whileTrue: -      forKeyinchCurrent_level:Wuyi         Print(Key) theCity = input ('"'). Strip () -     ifLen (city) = = 0:Continue Wu     ifCity = ='b': -         ifLen (last_levels) = = 0: Break AboutCurrent_level = last_levels[-1] $ Last_levels.pop () -     ifCity = ='Q': -          Break -  A     ifCity not inchCurrent_level:Continue + last_levels.append (current_level) theCurrent_level = current_level[city]




1. Dictionaries

Dictionary Key:value

#元组, element is not variable
L = ()
#定义符号 (), exactly the same as the list, except that the elements in the tuple are not mutable
#





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 variable
#key类型
#key的定义规则: 1. Immutable, defines a data, then changes the data, whether the ID is changed, the ID is changed to an immutable type, and the ID does not change to a mutable type. #数字, string, tuple immutable, (list, dictionary variable).
# 2. As long as the hash can be hashed, it can be defined as a key Key,hash
#3. Key is unique in the dictionary
#value类型
#value定义规则: Any type

#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 Shallow 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 ([+ +], ' Alex ')
Print (data)

2. Collection
 #关系运算 
#集合中都是不同的元素, do not repeat
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}
#求和, intersection
Print (a&b)
Print (a.intersection (b))
#并集
Print (a|b)
Print (a.union (b))

#差集, remove the same element as a in B
print (A-B)
Print (A.difference ())

#对称差集, different parts
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 = { , ' a '}
print (' A ' in S3)
for I in S3:
Print (i)
# # # #元组用途
t1= (all in a)

other built-in methods of the collection
S1 = {A-i}
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)

#删除, the return value is empty, the element does not exist, no error
Print (S1.discard (' W '))
Print (S1)


S1 = {, ' A ', ' e '}
s2={1,2,3}
S1.difference_update (S2)
Print (S1)

S1.isdisjoint ()
3. File processing
#-*-Coding:utf-8-*-
Print (Open (' test.txt '). Read ())

#read, R: Read mode
#w: Create write mode
f = open (' MyFile ', ' W ')

#a: Add mode

#删除模式

#r +; read-write mode, append to Last
#w +: Write read, empty create, then write
#a +: Append read, append to last

F.closed #检查文件是否关闭
f.encoding# Print File Encoding format
F.fileno () #文件在操作系统中的索引值
F.flush () #实时强制刷新文件 (Save)
F.isatty () #判断是不是终端文件
f.name# Print File name
f.newlines#
F.readable () #是否为不可读文件
F.seek () #寻找, move the cursor to the specified position
F.seek (#从第十个字段开始读)
F.tell () #告诉你光标在文件中的位置
F.truncate () #截断, truncated from the position of the cursor
data = [' alex\n ', ' jack\n ']
F.writelines (data) #将列表按行插入文件
#eval (data) to convert a string to a dictionary


4. Character encoding

High Voltage: 1
Low voltage: 0

Computers only know numbers

Character ------translation------" number
A------11

1. Memory fixed using Unicode encoding, hard drive encoding (i.e. you can modify the software encoding)
2. What encoding to use to save the hard disk, and what code to read
3. The program runs in two River stages: 1. read from hard disk to memory 2.python interpreter run code read to memory
4. The difference between Python and Nodpad++\vimde is a second step for a test.py file
5. Head encoding type, determines how memory is read from the hard disk

Share

"Consumer Behavior Science"
5-minute Business school app: get

Lib Brothers even PHP
The play says PHP

"Linda See America"



Python Road, Day3-python Foundation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.