Python Route--day 02

Source: Internet
Author: User
Tags extend

Stitching between strings
S1 = ' Qwe '
S2 = ' ASD '
Print (S1 + s2)

Qweasd
S1 = ' Qwe '
S2 = ' ASD '
Print (S1 * 8)
In Python,%s represents a variable of type string,%d is a variable of integer type, a dictionary of formatted representations, for example

Conversion encoding and decoding between bytes and Str


A slice is a paragraph that intercepts a string by index (index: Index: STRIDE), forming a new string (the principle is that Gu head ignores butt)
A = ' Abcdefghijk ' Print (A[0:3]) print (A[2:5]) print (a[0:]) #默认到最后print (a[0:-1]) #-1 is the last print (A[0:5:2]) #加步长
Print (A[5:0:-2]) #反向加步长

String common methods.

S1 = ' DASDFASASD '

Print (S1.capitalize ()) #首字母大写print (S1.swapcase ()) #大小写翻转s2 = ' Egon say hi ' Print (S2.title ()) #每个单词的首字母大写

Content centered, total length, white space filled a1 = ' 123 '
A2 = A1.center ("*") print (A2)

The number of elements in the number string that appear. Ret3 = A1.count ("A", 0,4) # can be sliced print (RET3)
A2 = "hqw\t" #\t in front of the completion of the default will be a TAB key into 8 spaces, if the character in front of the tab is less than 8, then complete 8, if the character length before the TAB key is more than 8 16, and so on, and so on each complement 16. Ret4 = A2.expandtabs () print (ret4) a4 = "dkfjdkfasf54" #startswith determine whether to ... Start #endswith judge whether to ... End ret4 = A4.endswith (' jdk ', 3,6) # Gu Tou ignoring butt print (RET4) # Returns a Boolean value Ret5 = A4.startswith ("KFJ", 1,4) print (RET5) #寻找字符串中的元素是否存在 # R Et6 = A4.find ("Fjdk", 1,6) # print (RET6) # Returns the index of the found element, if not found return -1# ret61 = A4.index ("Fjdk", 4,6) # print (ret61) # Returns the index of the found element, Unable to find an error. What #split divide and eventually form a list this list does not contain this split element. # ret9 = ' Title,tilte,atre, '. Split (' t ') # print (ret9) # ret91 = ' Title,tilte,atre, '. Rsplit (' t ', 1) # Print (ret91) # Format three gameplay formats output res= ' {} {} {} '. Format (' Egon ', ' Male ') res= ' {1} ' {0} {1} '. Format (' Egon ', ', ' Male ') res= ' {name} {age} { Sex} '. Format (sex= ' Male ', name= ' Egon ', age=18) #stripname = ' *egon** ' Print (Name.strip (' * ')) print (Name.lstrip (' * ')) Print (Name.rstrip (' * ')) #replacename = ' Alex Say:i has one tesla,my name is Alex ' Print (Name.replace (' Alex ', ' SB ', 1) # # # # # # # # # # # # #is系列name = ' jinxin123 ' Print (Name.isalnum ()) #字符串由字母或数字组成print (Name.isaLpha ()) #字符串只由字母组成print (Name.isdigit ()) #字符串只由数字组成 
Further research for logical operations:

1, in the absence of () the not priority is higher than and,and priority above or, that is, the priority relationship is () >not>and>or, the same priority is computed from left to right.

List

The list is one of the underlying data types in Python, and other languages have data types similar to lists, such as JS called arrays, which are enclosed in [] and each element is separated by commas, and he can store various data types such as:

Li = [' Alex ', 123,ture, (Wusir), [All-in-all, ' xiaoming ',],{' name ': ' Alex '}]

Compared to strings, lists can store not only different data types, but also large amounts of data, 32-bit Python is limited to 536,870,912 elements, and 64-bit Python is limited to 1.,152,921,504,606,85e,+18 elements. And the list is ordered, there are index values, can be sliced, convenient to take value.

Increase:

A = [1, ' A ', ' B ', 2,3, ' a ']# a.insert (0,55) #按照索引去增加 # Print (a) # # a.append (' aaa ') #增加到最后 # a.append ([triple]) #增加到最后 # Print (a) # # a.extend ([' q,a,w ']) #迭代的去增 # a.extend ([' q,a,w ', ' aaa ']) # a.extend (' a ') # a.extend (' abc ') # a.extend (' a,b,c ') # print (a)

By deleting:

# a = Li.pop (1) #按照位置去删除, with a return value of # print (a) # del A[1:3] #按照位置去删除, you can also slice delete without a return value. # print (a) # A.remove (' a ') #按照元素去删除 # Print (a) # a.clear () #清空列表

Change:
# a = [1, ' A ', ' B ', 2,3, ' a ']# a[1] = ' Dfasdfas ' # print (a) # A[1:3] = [' A ', ' B ']# print (a)

Check:
Traversing with slices or for loops





Dictionary

Increase:

# dic[' a '] = ["A", "B", "C"]
# Print (DIC)
# SetDefault adds a key-value pair to the dictionary, if only the key that corresponds to the value is none, but if the original dictionary has a set of key-value pairs, then he will not change or overwrite.
# dic.setdefault (' K ', ' V ')
# print (DIC) # {' Age ': ' ' name ': ' Jin ', ' sex ': ' Male ', ' k ': ' V '}
# dic.setdefault (' K ', ' v1 ') # {' Age ': +, ' name ': ' Jin ', ' sex ': ' Male ', ' k ': ' V '}
# Print (DIC)

By deleting:

# Dic_pop = Dic.pop ("A", ' no key default return value ') # Pop deletes a key-value pair based on key and returns the corresponding value if no key returns the default return value # print (dic_pop) # del dic["name"]  # no return value. # print (DIC) # DIC_POP1 = Dic.popitem ()  # Randomly deletes a key-value pair in the dictionary, returns the deleted key-value pair as a Ganso return # print (DIC_POP1)  # (' Name ', ' Jin ') # Dic_ Clear = Dic.clear ()  # Empty Dictionary # Print (dic,dic_clear)  # {} None

Change:
# dic = {"Name": "Jin", "Age": +, "sex": "male"}# Dic2 = {"Name": "Alex", "Weight": 75}# dic2.update (DIC)  # Add all the key-value pairs of dic (same overlay, no add) to Dic2 # print (DIC2)

Check:
# value1 = dic["Name"]  # no error # print (value1) # # value2 = Dic.get ("DJFFDSAFG", "Default return value")  # no return value can be returned # print ( value2)




Python Route--day 02

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.