Automated DAY3---python basics (list, dictionaries, tuples, file read and write, string formatting)

Source: Internet
Author: User
Tags shallow copy string methods

1.list

Operation of List

#数组---list array
Stus = [' Xiaohei ', ' Xiaobai ', ' Xiaolan ']
#计算机取值是从0开始的
#0 1 2 3
#下标 angle index refers to a thing
#走后一个值可以用-to indicate
Print (stus[2]) #取第三个值
Print (stus[-1]) #取第三个值

#list的增删改查
#增
#append方法是在list末尾增加, you can only add one at a time
Stus.append (' YBQ ')
Print (Stus)
#insert方法是在指定位置增加
Stus.insert (0, ' MPP ') #0是位置
Print (Stus)
#如果指定的下标不存在, then it is added at the end
Stus.insert (9, ' MPP ') #9是位置, but does not exist
Print (Stus)

#改
Stus[0] = ' YBQ '
Print (Stus)

#查
Print (Stus)
Print (Stus[1])
Print (Stus.count (' YBQ ')) #查看某个元素的个数, if the element does not exist, returns 0
Print (Stus.index (' Xiaohei ')) #找到某个元素的下标, if there are multiple, return the first, if the element does not exist, it will be an error

#删
Stus.pop (1) #默认删除最后一个元素, if you specify subscript, delete the specified element
Print (Stus.pop (1)) #打印 deleted values
Print (Stus)
Stus.remove (' Xiaohei ') #直接写元素, if there are multiple elements, delete only one
Print (Stus)
Del Stus[1] #删除指定下标的值
Stus.clear () #清空整个list

#反转list
Stus.reverse ()
Print (Stus)
#
#排序, the default sort is ascending
Stus.sort ()
Stus.sort (reverse=true) #指定reverse =true, which is descending order
Print (Stus)

#多维数组
#二维 three-dimensional four ...
All_num = [123,456,[789,10,11]]
three = All_num = [123,456,[789,10,11,[12,13]]
Print (all_num[2][1]) #先找外边 look inside.
#
All_num.extend (three) #把两个list合并到一起
Print (All_num)

List loops and slices

 mun = [1,2,3,4,5,6,7,8,9,10] 
Print (Mun[1:6:2]) #步长 2 means to take a
print (mun[-1:-6:-2]) #步长是正数的从左往右取 every two, if it's a negative number , from right to left, the preceding value must also write negative
#切片同样适用于字符串, the string also has subscript
title = ' Send red envelopes today ' #空格也算一个字符串
Print (title[1])
Print (Title[1:3])
for T,h in enumerate (title): #enumerate可以同时循环下标和值
Print ('%s:%s '% (t,h)) #同时打印下标和值

2. Dictionary Operation
 #字典是key-vlue form of 
D = {
' name ': ' YBQ ',
' age ': ' + ',
' addr ': ' Haidian '
}
#特点: Value convenience fast
pri NT (d[' name ']) #如果写了不存在的值, error
Print (d.get (' ag0e ')) #如果写了不存在的值, return none,
Print (D.get (' ag0e ', ' dou ') #如果写了不存在的值 , after the value is written, it returns the written value Dou,
#
#字典是无序的
#增
d[' Shengao ']=198
D.setdefault (' Tizhong ', *)
#
#改
D [' Shengao ']=198
D.setdefault (' Tizhong ', ') #这种方法不能修改值, only new
#
#删除
D.pop (' Shengao ')
Print (d)
D.popitem () #随机删除一个数值, no use, basically less than
del d[' Shengao '
#
D.clear () #清空字典

#获取所有key和value
Print ( D.keys ())
Print (D.values ())
#
#
If ' mane ' in D: #判断字典中是否有此值
Pass

#字典循环
for i in D:
Print (i)
for k,v in D.items ():
Print (k,v)
Print (D.items ())
for K in D: #性能最好
Print (K,d[k]

#强制类型转换
Int ()
Str ()
List () cast to list using
Res = list (D.items ())
Print (res[0])

3. Tuple operations
Print (Type (a)) #查看是什么类型
b= (1,2,3,4,5)
#元祖也是一个list, just immutable, unable to modify
B.count (1) #找个数
B.index (1) #返回下标
4. File read and write operations
f = open (' Read and write Records '. txt ', ' A + ', encoding = ' utf-8 ')
F.seek (0) #seek移动指针的时候, only to read so, not to write well
Print (F.read ()) #读取文件里的全部内容
Print (F.readline ()) #只读一行内容
Print (F.readlines ()) #读取文件里的全部内容, put the contents of the file into a list
name = [' Ybq ', ' YB ']
F.write (name) #只能传字符串
F.writelines (name) #写的时候传入一个可迭代的对象即可, is a recyclable
F.truncate () #清空文件内容
F.tell () #查看当前指针位置
F.write (' ybq ' + ' \ n ')
#循环
For I in F:
Print (i)
F.close ()

#flie () Python2 inside the method
#文件打开有三种方式, if the mode is not specified when opening, the default is read
# Read R
# r+ Read and write mode, as long as the R, the file does not exist when the Open will be error
# write W this mode will empty the original file contents
# w+ write mode, as long as the W, the contents of the file will be emptied
#追加 A
#追加读写 A +

5. String methods
#可变变量 List Dictionary
#不可变变量 progenitor, String value cannot be modified

Li = [1,1,2,3,4,5,6,7,8,9]
Li2 = li[:] #深拷贝
Li2 = li# Shallow copy
For I in Li:
If i%2!=0:
Li.remove (i)
Print (LI)
#在循环list的时候不能删东西, this will cause the subscript to change and a copy can be resolved.
#
ID (LI) #查看内存地址
Print (ID (LI))
Print (ID (li2))

#字符串方法都不会改变原来的值
Name = ' Besttest rich '
Print (name)
Print (Name.strip ()) #默认去掉两边的空格和换行符
Print (Name.strip (' besttest '))
Print (Name.lstrip ()) #去掉左边的空格和换行符
Print (Name.rstrip ()) #去掉右边的空格和换行符
Print (Name.count (' t ')) #查找某个字符在字符串里的个数
Print (Name.upper ()) #把所有的大写字母都变成小写
Print (Name.lower ()) #把所有的小写字母都变成大写
#
NA = ' select * from '
Print (Name.startswith (' select ')) #判断字符串是以XX开始的
#
na = ' 1.jpg '
Print (Name.endswith ('. jpg ')) #判断字符串是以XX结尾的
#
f = ' {name} welcome '
Print (F.format (name= ' YBQ ')) #格式化
#
f = ' {name} welcome '
D = {' name ': ' YBQ '}
Print (F.format_map (d)) #字典形式格式化
#
Print (Name.replace (' t ', ' d ')) #字符串替换, the first one is old, the second is the new one.
Print (Name.isdigit ()) #是否是数字
Print (' 123 '. IsDigit ()) #是否是数字
#
Print (Name.isalnum ()) #是否包含数字或字母
Print (Name.isalpha ()) #是否是英文字母
#
Print (Name.capitalize ()) #首字符大写
Print (Name.center, ' * ') #字符串放中间, both sides are padded with *
Print (Name.find (' t ')) #找到这个字符, and returns the subscript, if not present-1
Print (Name.index (' t ')) #找到这个字符, and returns the subscript, if not present, an error

st = ' a,b,c,d,e,f,g '
St_list=st.split (', ') #以逗号分隔字符串, write nothing, separate by space
Print (st_list)

Slist =[' A ', ' B ', ' C ']
SD = ', '. Join (slist) #以逗号连接字符串
Print (SD)





Automated DAY3---python basics (list, dictionaries, tuples, file read and write, string formatting)

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.