Python data type

Source: Internet
Author: User
Tags shallow copy

Lesson Two

Today mostly speaks of data types

Divided into str string, int integer, dictionary, list, set, tuple

# ########################################## STR string ###############################################

#1, string first letter capital capitalize

# name = ' liuming '

# v = name.capitalize () #设置生成一个新的值

# print (name) #字符串本身不会变

# print (v) #新的值变化


# 2, string all uppercase to lowercase casefold

# name = ' liuming '

# v =name.casefold ()

# print (v)


#3, all uppercase lowercase

# name = "Liuming"

# V=name.lower ()

# print (v)


#4, set length, and center text

#参数1: Indicates medium length

#参数2: Fields that are filled in blanks (only 1 in length)

# name = ' liuming '

# v = name.center (15, '-')

# print (v)


#5, indicates the number of occurrences of the transfer value in the string

#参数1: The value to find (sub-sequence)

#参数2: Starting position (Index)

#参数3: End Position (index)

# name = ' Alexliumangnishausaisha '

# v = name.count (' a ')

# print (v)

# v = name.count (' li ', ') #起始位置

# print (v)

# v = name.count (' A ', 0,18) #结束位置

# print (v)


#6, whether to end with XX

# name = ' liuming '

# v = name.endswith (' ng ') #正确返回True, error returned false

# print (v)


#7, whether to start with XX

# name = ' liuming '

# v = name.startswith (' lb ') #正确返回True, error returned false

# print (v)


#8, convert into bytes

# name = ' Liu Mingchuan '

# V1 = Name.encode (encoding= ' utf-8 ') #指定编码

# v2 = Name.encode (encoding= ' GBK ') #指定编码

# Print (V1,V2)


#9, find the tab \ t to replace (including the beginning of the value), according to the tab, automatically generate the table (\ n line-break tab)

# name = ' Liu\tming\tal\nex\twu\tpei '

# v = name.expandtabs (#指定宽度)

# print (v)


#10, finds the index position of the specified subsequence: no return-1

# name = ' Liumingalex ' #不存在返回-1

# v = name.find (' q ')

# print (v)

# v = name.index (' o ') #不存在则报错

# print (v)


#11, string formatting

# i = ' My name:%s; age:%s; Gender:%s '

# v = i% (' Liu Mingchuan ', 19, ' Male ') #使用占位符格式化字符串

# print (v)

# i = ' My name: {0}; Age: {1}; Gender: {2} '

# v = I.format (' Liu Mingchuan ', 19, ' Male ') #使用format格式化字符串 (string position)

# print (v)

# i = ' My name: {name}; Age: {Ages}; Gender: {gender} '

# v = i.format_map ({' name ': ' Liu Mingchuan ', ' age ': +, ' gender ': ' That '}) #使用字典格式化字符串

# print (v)


#12, whether it is digital, Chinese characters

# name = ' alex8 Kanji '

# v =name.isalnum () #允许有数字

# print (v)

# v2 =name.isalpha () #不允许有数字

# Print (v2)


#13, judging whether it is a number

# num = ' ② 1232 '

# V1 = num.isdecimal () #可以识别 ' 123 '

# v2 = num.isdigit () #可以识别 ' 123 ', ' ② '

# v3 = num.isnumeric () #可以识别 ' 123 ', ' ② ', two

# Print (V1,V2,V3)


#14, whether it is an identifier

# n = ' name '

# v = n.isidentifier ()

# print (v)


#15是否全部是小写

# name = ' Lmslns '

# v =name.islower () #是否全部是小写

# print (v)

# v = name.isupper () #是否全部是大写

# print (v)


#16, all Caps

# name = ' liuming '

# v = name.upper () #字符串全部变大写, lower () all lowercase

# print (v)


#17, whether to include hidden xx

# name = ' two dog two dog two dog \nsbsbsbs '

# v = name.isprintable ()

# print (v)


#18, whether all are spaces

# name = ' '

# v =name.isspace ()

# print (v)


#19, element concatenation (element String) * * * * *

# name = ' Alex '

# v = ' _ '. Join (name) #指定拼接字符

# print (v)

# name_list = [' Alex ', ' liuming ', ' Liukang ')

# v = ' say '. Join (Name_list)

# print (v)


#20, left and right padding

#中center左rjust右ljust

# name = ' Alex '

# v =name.center (12, '-')

# print (v)

# v =name.rjust (12, ' * ')

# print (v)

# v =name.ljust (13, ' $ ')

# print (v)


#21, correspondence + translation

# m = Str.maketrans (' aeojj ', ' 12344 ') #对应关系

# name = "ASDKAHKJFHKJSH123HKJ;ASDSFJJJCNNN" #讲对应关系的字母翻译为数字

# v = name.translate (m)

# print (v)


#22, split preserving elements of a split

# name = ' Liu Mingchuan SB-li SB in '

# v = name.partition (' SB ')

# print (v)


#23, replace

# name = ' Liu Mingchuan SB-li SB in '

# v =name.replace (' SB ', ' hehe ') #把SB改成hehe

# print (v)

# v = name.replace (' SB ', ' heh ', 1) #把第一个SB改成heh

# print (v)



#24. Remove whitespace, \n\t, custom

# name = ' alex\t\n '

# v =name.strip ()

# print (v)


#25. Case conversion

# name = ' Alesx '

# v = name.swapcase ()

# print (v)


#26. Filling

# name = ' Alex '

# v = Name.zfill (#指定填充的个数)

# print (v)


#



# # #字符串总结

Name= ' liuming\t '

Name.capitalize () #字符串首字母大写

Name.casefold () #所有大写变小写

Name.lower () #所有大写变小写

Name.center #文本居中, 20 specify width,-Specify fill

Name.count (' u ', 1,6) #查找值, U is the value to find, 1 string position begins, 6 end of string

Name.startswith (' li ') #查询以xx开头

Name.endswith (' ng ') #查询以xx结尾

Name.expandtabs () #找到制表符 \ t to make a replacement table (containing the preceding values)

Name.format_map ({' name ': ' Alex '}) #字符串格式化, according to the dictionary

Name.format (' liuming ') #字符串格式化

Name.isalnum () #允许有数字

Name.isalpha () #不允许有数字

Name.isdecimal () #是否有数字可以识别123

Name.isdigit () #是否有数字可以识别123, ②

Name.isnumeric () #是否有数字可以识别123, two, ②

Name.isidentifier () #是否是标识符

Name.islower () #是否全部是小写

Name.isupper () #是否全部是大写

Name.upper () #全部变大写

Name.lower () #全部变小写

Name.isprintable () #是否包含隐藏的xx

Name.isspace () #是否全部是空格

' _ '. Join (name) #元素拼接

Name.rjust (A, '-') #左填充, 20 Specifies the width,-the filled thing

Name.ljust (A, '-') #右填充, 20 Specifies the width,-the filled thing

#对应关系 + Translation

m = Str.maketrans (' aeojj ', ' 12344 ') #对应关系

Name = "ASDKAHKJFHKJSH123HKJ;ASDSFJJJCNNN" #讲对应关系的字母翻译为数字

v = name.translate (m) #讲对应关系的字母翻译为数字

Name.partition (' um ') #分割, preserving the split elements according to the UM split

Name.replace (' um ', ' SB ', 1) #替换, um Replace with sb,1 specify location substitution

Name.strip () #移除空白 \t\n

Name.swapcase () #大小写转换

Name.zfill (20) #填充0, Width specified

Len (name) #测宽度


# ########################################## int integer ###############################################

#1. The binary representation of the current integer, the minimum number of digits

# age = 4 # 001

# Print (Age.bit_length ())

# #获取当前数据的字节表示

# age = 15

# v1 =age.to_bytes (10,byteorder= ' big ') #10进制, from right to left

# v2 =age.to_bytes (10,byteorder= ' little ') #10进制, from the left

# Print (V1,V2)


# ########################################## BOOL Boolean value ##########################################

# v = 0 #True

# v = ""-#空内容: False

# v = []--> #空内容: False


# ########################################## List ##########################################

#列表为可变类型

User_list = [' Liu Mingchuan ', ' Lee ', ' Gold ', ' Harry ', ' Li ']

#1. Append

# user_list.append (' Zhao Si ') #在最后追加赵四

# Print (user_list)


#2. Clear

# user_list.clear () #清空表格内容

# Print (user_list)


#3. Copy (Shallow copy)

# v = user_list.copy () #拷贝到v

# print (v)

# Print (user_list)


#4. Counting

# v = user_list.count (' li ') #计算列表中有几个李

# print (v)

# Print (user_list)


#5. Extending the original list

# user_list.extend ([' amount of protection ', ' Alex ']) #写新表扩展到之前表格里

# Print (user_list)


#6. Find an element index and cannot find an error

# v =user_list.index (' Li ')

# print (v)


#7. Delete and get the element index

# v =user_list.pop (1) #删除位置为1的值

# print (v)

# Print (user_list)


#8. deleting

# user_list.remove (' Lee ')

# Print (user_list)


#9. Flip (reverse)

# User_list.reverse ()

# Print (user_list)


#10. Sorting

# nums =[11,33,22,44,1,2]

# Nums.sort () #排序从小到大

# Print (nums)

# Nums.sort (reverse=true) #排序从大到小

# Print (nums)


###### Extra:

# user_list = [' Li Quan ', ' Liu Yi ', ' Li Quan ', ' Liu Kang ', ' peas ', ' dragons ']

# User_list[0]

# User_list[1:5:2] #索引

# del User_list[3] #删除

# for I in User_list:

# Print (i)

# User_list[1] = ' Ginger Day '

# user_list = [' Li Quan ', ' Liu Yi ', ' Li Quan ', ' Liu Kang ', ' peas ', [' Day ', ' Day ', ' Teddy '], ' little Dragons '

# li = [' Eric ', ' Alex ', ' Tony ']

#

# v = Len (li) #长度

# print (v)

#

# li.append (' seven ') #添加

# Print (LI)

#

# li.insert (0, ' Tony ') # Insertion

# Print (LI)

#

# Li[1] = ' Kelly ' #根据索引替换

#

# li.remove (' Eric ') #删除

# Print (list)

#

# v = li.pop (1) #删除并获取元素

# print (v)

# Print (LI)

#

# del li[2] #删除表



# del Li[0:2] # 0 =<x < 2

# Print (LI)


# li.reverse () #翻转

# Print (LI)


# for I in Li: #循环表

# Print (i)





# ######################################### Strong plug: range,enumrate #########################################

# 1. Please output 1-10

# 2.7: Generate all numbers now

# range (1,11) # Build 1-10


# 3.x: Will not be generated immediately, only one generation at a time of loop iteration

# for I in Range (1,11): #使用for循环生成1-10

# Print (i)


# for I in Range (1,11,2): # Generate 1-10,2 for not long build 1 3 5 7

# Print (i)


# for I in Range (10,0,-1): #

# Print (i)


# 1. 3.x will not be generated, after the iteration is created one after another;

"""

-2.7:

Range ()

Xrange () will not be generated, after the iteration is created one after another;

-3.x

Range () is not generated, and is created one after the iteration;

"""

# 2. Range: three parameter (+)

#

# li = [' Eric ', ' Alex ', ' Tony ']

# # Range,len,li Loop

# for I in range (0,len (LI)):

# ele = Li[i]

# Print (ele)



# li = [' Eric ', ' Alex ', ' Tony ']

# for I in Li:

# Print (i)


# for I in range (0,len (LI)):

# print (I+1,li[i])



# Enumerate generates an extra column of ordered numbers

# li = [' Eric ', ' Alex ', ' Tony ']

# for I,ele in Enumerate (li,1):

# Print (I,ele)

#

# v = input (' Please enter the product serial number: ')

# v = Int (v)

# item = Li[v-1]

# Print (item)


# ###### #列表总结

# user_list = [' Li Quan ', ' Liu Yi ', ' Liu Kang ', ' peas ', ' dragons ']

# user_list.append (' Liu I ') #追加

# user_list.clear () #清空表

# v =user_list.copy () #浅拷贝

# v =user_list.count (' Liu Yi ') #计数

# user_list.extend ([' Yes ', ' Aston ']) #扩展原列表

# v =user_list.index (' peas ') #查找索引, no error found

# v =user_list.pop (' 1 ') #删除并获取元素

# user_list.remove (' Dou Dou ') #删除

# user_list.reverse () #翻转

# User_list.sort () #排序从小到大

# User_list.sort (reverse=true) #排序从大到小

# User_list[1:5:2] #索引从1 -5,2 not often

# del user_list[2] #删除

# v= Len (user_list) #统计长度

# User_list.insert (1, ' Tony ') #插入


# ######################################### Dict: Dictionary: mutable type #########################################


# 1. Empty

# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}

# dic.clear ()

# Print (DIC)


# 2. Shallow copy

# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}

# v = dic.copy ()

# print (v)


# 3. Gets the specified value according to key; no error is present

# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}

# v = dic.get (' k1111 ', 1111)

# print (v)

# v = dic[' k1111 ')

# print (v)


# 4. Delete and get the corresponding value value

# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}

# v = dic.pop (' K1 ')

# Print (DIC)

# print (v)


# 5. Randomly deletes a key-value pair and gets the key value to delete

# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}

# v = dic.popitem ()

# Print (DIC)

# print (v)


# k,v = Dic.popitem () # (' K2 ', ' v2 ')

# Print (DIC)

# Print (K,V)


# v = dic.popitem () # (' K2 ', ' v2 ')

# Print (DIC)

# print (v[0],v[1])


# 6. Add, do not operate if present

# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}

# dic.setdefault (' K3 ', ' v3 ')

# Print (DIC)

# Dic.setdefault (' K1 ', ' 1111111 ')

# Print (DIC)

# 7. Bulk additions or modifications

# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}

# dic.update ({' K3 ': ' V3 ', ' K1 ': ' V24 '})

# Print (DIC)



# dic = Dict.fromkeys ([' K1 ', ' K2 ', ' K3 '],123)

# Print (DIC)

# dic = Dict.fromkeys ([' K1 ', ' K2 ', ' K3 '],123)

# dic[' k1 ' = ' asdfjasldkf '

# Print (DIC)


# dic = Dict.fromkeys ([' K1 ', ' K2 ', ' K3 '],[1,])

# {

# k1:123123213, # [up]

# k2:123123213, # [1,]

# k3:123123213, # [1,]

# }

# dic[' K1 '].append (222)

# Print (DIC)

# ########## Extra:

#-Dictionaries can be nested

#-Dictionary key: Must be an immutable type

# dic = {

# ' K1 ': ' V1 ',

# ' K2 ': [All-in-all,],

#: ' Lllll ',

# 1: ' Fffffffff ',

# 111: ' Asdf ',

# }

# Print (DIC)

# key:

#-Non-changeable

#-true,1


# dic = {' K1 ': ' v1 '}

# del dic[' K1 ')


# Boolean value:

# 1 True

# 0 False

#

# bool (1111)


# # # # # #字典总结

DiC ={' K1 ': ' v1 ', ' K2 ': ' V2 '}

Dic.clear () #清空

V=dic.copy () #浅拷贝

V=dic.get (' keee ', 111) #根据key获取指定的value, there is no error

Dic.pop (' K1 ') #删除并获取删除的键值

Dic.setdefault (' K3 ', ' v3 ') #增加, do not operate if present

Dic.update ({' K3 ': ' V3 ', ' K4 ': ' v4 '}) #批量增加或修改

Del dic[' K1 '] #删除


# ##################################### set, set, non-repeating list; mutable type #####################################

# S1 = {"Alex", ' Eric ', ' Tony ', ' Li Quan ', ' Li Quan 11 '}

# s2 = {"Alex", ' Eric ', ' Tony ', ' Liu Yi '}


# exists in 1.s1, S2 does not exist

# v = s1.difference (S2)

# print (v)

# # # # # # # # # # # # # # # # # # S1, S2, then empty on S1, then re-copy

# s1.difference_update (S2)

# print (S1)


# exists in 2.s2, S1 does not exist

# v = s2.difference (S1)

# print (v)


# exists in 3.s2, S1 does not exist

# exists in S1, S2 does not exist

# v = s1.symmetric_difference (S2)

# print (v)

# 4. Intersection

# v = s1.intersection (S2)

# print (v)

# 5. and set

# v = s1.union (S2)

# print (v)


# 6. Removed from

# S1 = {"Alex", ' Eric ', ' Tony ', ' Li Quan ', ' Li Quan 11 '}

# S1.discard (' Alex ')

# print (S1)


# S1 = {"Alex", ' Eric ', ' Tony ', ' Li Quan ', ' Li Quan 11 '}

# s1.update ({' Alex ', ' 123123 ', ' FFF '})

# print (S1)

# ##### Extra:


# S1 = {"Alex", ' Eric ', ' Tony ', ' Li Quan ', ' Li Quan 11 '}

# for I in S1:

# Print (i)


# S1 = {"Alex", ' Eric ', ' Tony ', ' Li Quan ', ' Li Quan 11 ', (11,22,33)}

# for I in S1:

# Print (i)







This article is from "Linux, win operations knowledge" blog, please be sure to keep this source http://liuming66.blog.51cto.com/10684323/1923940

Python data type

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.