Python Next day Study summary

Source: Internet
Author: User

1. Detailed Data types
int: integral type, for example: 1,4,100 ..... Mainly used to calculate
STR: String. Primarily used to store small amounts of data
List: Storing large amounts of data
Tuple: Similar to list, is a read-only list
Dict: Storing relational data
BOOL: Boolean indicating True and False

2.str
In Python, quotation marks (single quotes, double quotes, and three quotation marks) are all caused by strings.
name = ' CC '
Name = "CC"
info = ""
Name:cc
Age:18
‘‘‘
A. Adding strings
S1 = ' Hello '
S2 = ' CC '
Print (S1 + s2) #输出hellocc

B.strint
Print (' CC '
3) #输出cccccc

C. Index and slice
s = ' dick and Harry cc '
S1 = s[0] #s1依然是str类型
Print (S1) #输出张, left-to-back (0 1 2 3 ...)
Print (s[-1]) #输出c, right-to-left values (-1 -2-3-4-5 ...)
Print (s[-3]) #输出四, right-to-left values (-1-2-3-4-5 ...)
Print (S[0:2]) #输出张三, the slice Gu Tou regardless of the tail, [Start index: End index + 1: Step]
Print (s[0:]) #输出张三李四cc
Print (S[0:4:2]) #输出张李,: 2 is the step size, the default step is 1, Go forward without the default step
print (s[-1:-5:-1]) #输出cc四李, reverse fetch, with reverse step-1
Print (S.find (' three ')) #输出1, index by element, and index usage, Find can not find the reference when the return -1,index will not find the value will be error

D. String manipulation
s = ' Zcchello ' s1 = ' zcc Zhangsan. Lisi9wangwu ' s3 = ' zcchello\t\n ' s4 = ' Zcc,zhangsan,lisi,wangwu '
S5 = [' zcc ', ' Zhangsan ', ' Lisi ', ' Wangwu ']
Print (S.capitalize ()) #字符串首字母大写其余小写, Output Zcchello
Print (S.swapcase ()) #大小写翻转, Output Zcchello
Print (S1.title ()) #非字母隔开的单词首字母大写, output zcc zhangsan.lisi
9Wangwu
Print (S.center ()) #设置总长度为15并居中显示to fill, the default padding is a space
Print (S.upper ()) #全部变成大写, output Zcchello, for verification code
Print (S.lower ()) #全部变成小写, Output Zcchello
Print (S.startswith (' z ')) #判断是否以z开头, Output True
Print (S.endswith (' l ')) #判断是否以l结尾, output false
Print (S3.strip ()) #默认去除字符串前后的空格, newline, tab, and input combined, you can also specify the symbol to be removed or only remove the front (Ltrip) Back (Rtrip), Output Zcchello
Print (S.replace (' zcc ', ' Zhangsan ')) #替换, Output Zhangsanhello
Print (S4.split (', ')) #字符串分割成list, the default is a space-delimited, can specify delimiter, output [' zcc ', ' Zhangsan ', ' Lisi ', ' Wangwu ']
Print (". Join (S5)) #列表转换成字符串, output zcc Zhangsan Lisi Wangwu
Print (len (s)) #测量长度, output 8
Print (S.count (' C ')) #计数c出现的次数, Output 2

3.list
L1 = [3,4], 5,6] L2 = [' Zhangsan ', ' John Doe ', ' cc '] l3 = [1,4,7,8,9,0,3]
########### #查找值
Print (L1[2],type (l1[2))) #输出 (3, 4) <class ' tuple ' > values by index, consistent with the data type of the element itself
Print (L1[0:3],type (l1[0:3)) #输出 [1, 2, (3, 4)] <class ' list ' > by slice, take out a small list
For I in L1:
Print (i) #for循环取值
########## #list增加值
L2.append (' Harry ')
Print (L2) #输出 [' Zhangsan ', ' John Doe ', ' cc ', ' Harry '] append () Append value for list
L2.insert (1, ' Harry ')
Print (L2) #输出 [' Zhangsan ', ' Harry ', ' John Doe ', ' cc '] Insert values can be inserted according to the index
L2.extend ([i])
Print (L2) #输出 [' Zhangsan ', ' John Doe ', ' cc ', 1, 2, 3] extend for adding values that can be iterated
######### #list删除值
L2.pop (1)
Print (L2) #输出 [' Zhangsan ', ' cc '] pop () can be deleted by the index of the list
L2.remove (' cc ')
Print (L2) #输出 [' Zhangsan ', ' John Doe '] remove () can be deleted by the list element
L2.clear ()
Print (L2) #输出 [] Clear () empty list
Del L2[0:2]
Print (L2) #输出 [' CC '] del can be deleted by slice or index, or you can delete list
############# #改变值
L2[0] = ' Zhang San '
Print (L2) #输出 [' Zhang San ', ' John Doe ', ' CC '] can be modified by the index of the corresponding value
L2[0:2] = ' abc '
Print (L2) #输出 [' A ', ' B ', ' C ', ' CC '] can be modified according to the slice, slice is a space, can be re-added any value, if there is a step, must be one by one corresponding
############# #其余方式
Print (len (L1)) #输出5 Len () calculates the total number of list elements
Print (L2.count (' cc ')) #输出1 count () calculates the number of occurrences of an element
L3.sort ()
Print (L3) #输出 [0, 1, 3, 4, 7, 8, 9] sort () for positive ordering
L3.sort (Reverse=true)
Print (L3) #输出 [9, 8, 7, 4, 3, 1, 0] reverse order
L3.reverse ()
Print (L3) #输出 [3, 0, 9, 8, 7, 4, 1] Flip

4.tuple
A tuple is a read-only list that operates like a list

5.dict
DIC = {
' Name ': ' CC ',
' Age ': ' 28 ',
' Sex ': ' Male ',
}
############### #增加值
dic[' high ' = #增加 ' high ': 170
dic[' name '] = ' Zhang San ' # ' cc ' becomes ' Zhang San ' without adding, there is modification
Dic.setdefault (' High ', in #增加 ' High ': 170
Dic.setdefault (' name ', ' Zhang San ') #无任何修改 no increase, there is no change
############### #删除值
Dic.pop (' sex ') #删除 ' sex ': ' Male ' if there is no key to delete, you can specify the relevant return value
Dic.clear () #清空
Del dic[' name '] #删除 ' name ': ' CC ' can also delete the entire dictionary
############### #查询
Print (dic[' name ']) #输出cc query According to key, if there is no corresponding key error
Print (Dic.get (' name1 ', ' no Key ')) #get () query can specify the return value without the corresponding key
Print (Dic.keys ()) #查询所有的key放到一个容器中
Print (Dic.values ()) #查询所有的value
Print (Dic.items ()) #查询所有的 key and value

Python Next day Study summary

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.