Python Basics 1

Source: Internet
Author: User
Python Basics 1 about memory allocation

New defined string variables open up a new memory space by default

Other indexes, such as list, tuples, or dictionary values, point the variable name to the same address space, as shown below:

1 ## create a new memory space for the new string definition 2 >>> str1 = 'hoho' 3 >>> str2 = str1 4 >>> id (str1 ), id (str2) # view the address of the memory object and observe the memory address. that is, str2 has opened up memory space 5 (140297199501752,140 297199501752) # Here we can see that the same is caused by an internal mechanism of python. if the string is large enough, it will be different, do not tangle 6 >>> str2 = 'heihei' 7 >>> str1 8'hoho' 9 >>> str210 'heihei' 11 >>> id (str1 ), id (str2) # check whether the memory address has changed 12 (140297199501752,140 297214622552) 13 14 # Non-character server, such as list, tuples, after a dictionary value is defined, it only points the new variable name (which can be understood as a tag) to the same memory address. for example, the dictionary is shown as follows: 15 >>> dic1 = {'name ': 'hohoho'} 16 >>>> dic2 = dic117 >>> id (dic1), id (dic2) 18 (140297199190088,140 297199190088) 19 >>> dic1 = {'name ': 'hoho'} 20 >>>> dic2 = dic121 >>>> id (dic1), id (dic2) # view the address of the memory object, therefore, dic2 was modified. In fact, dic1 also followed by 22 (140297199191752,140 297199191752) 23 >>> dic2 ['name'] = 'heihei' 24 >>> dic225 {'name ': 'heihei'} 26 >>> dic127 {'name': 'heihei '}

Copying list, tuples, and dictionaries (use of the copy module for shortest and deep copy)

1. you can use slices for the list and tuples for shortest copy, or use the copy module for shortest copy (including dictionaries)

2. use copy. deepcopy () instance for deep replication

1 >>> import copy 2 >>> list1 = [1, 2] 3 >>> list2 = list1 4 >>> list2 [0] = 2 # changed list2, list1 followed by 5 >>> list1 6 [2, 2] 7 >>> list3 = list1 [:] # very complex, use array slices for shallow copy 8 >>> list3 = copy. copy (list1) 9 >>> id (list1), id (list2), id (list3) # Here we can see that the address space is different 10 (140297199250696,140 297199250696, 140297199247560) 11 >>>
12 >>> list4 = [1, [2] ## complex structures must be copied in depth. 13 >>> list5 = list4 [:] 14 >>> list515 [1, [2] 16 >>> list5 [1] [0] = 617 >>> list418 [1, [6] # Here we can see that the list in the inner layer is not copied, and list4 has changed 19 >>> list6 = copy. deepcopy (list4) # Use deep copy here 20 >>> list6 [1] [0] = 821 >>> list622 [1, [8] 23 >>> list424 [1, [6] # The copied

Common built-in functions

There are a lot of built-in functions in python, just remember the common ones, but you will know how to view the built-in functions and help functions.

Under normal circumstances, take three steps

Type (variable) ---> get the class to which the variable belongs

Dir (class name) ---> check which methods are available under the class. There are generally alternative methods similar to _ abs _ that begin with a double underline, for example: _ abs _ <=> abs ()

Help (class name or function name) ---> View function usage under the class or directly View function usage

Integer


1 >>> s, y = pmod (7,3) ## pmod returns data with the value (quotient, remainder). It can be used for paging 2 >>> s, y3 (2, 1) 4 >>> a =-25 >>> abs (-2) # abs obtains the absolute value of 6 27 >>> len (str (-2 )) # get speed length 8 2

View Code

Floating point


1 >>> a = 7.02 >>> pmod (a, 3) 3 (2.0, 1.0) 4 >>> a = 7.2355 >>>. _ round _ (2) # Rounding to 6 7.247>. _ trunc _ () # round 8 7

View Code

String


1 >>> str1 = 'This is a string '2 >>>> 'is in str1 # member judgment 3 True 4 >>> str1 [] # slice operation and index 5 'hi' 6> len (str1) # Length 7 16 8 >>> str1.find ('IS ') # search for a string and return the index value 9 210 >>>> str1.find ('IS) 11 512 >>> str1.find ('iss') # return-1 is not found. if it is an index, the system reports an error 13-114 >>> str1.index ('is', 3) 15 516 >>> str1.index ('iss') 17 Traceback (most recent call last): 18 File "", line 1, in 19 ValueError: substring not found20 >>> str1 = 'AAA' 21 >>> str1.strip () to blank, line feed, press enter 22 'AAA' 23 >>> str1.lstrip () 24 'AAA' 25 >>> str1.rstrip () 26 'AAA' 27 >>> str1 = 'duiqi '# alignment operation 28 >>> str1.ljust (20) 29 'duiqi' 30 >>> str1.ljust (20 ,'*') 31 'duiqi **************** '32 >>> str1.20.ust (20 ,'*') 33 '**************** duiqi' 34 >>> str1.center (20 ,'*') 35 '******** duiqi *********' 36 >>> str1 = 'This is a string '37 >>> str1.split () # Split operation 38 ['this', 'is ', 'A', 'string'] 39 >>> str1.splitlines () 40 ['This is a string'] 41 >>> list1 = [1, 2, 3] 42 >>> '-> '. join ([str (I) for I in list1]) # connection operation 43 '1-> 2-> 3' 44> str145' this is a string '46 >>> str1.count ('is ') # count 47 248 >>> str1.replace ('is, OS ') # replace 49 'thos OS a string' 50 >>>> str1.replace ('is, OS ', 1) # Replace, replace only one 51 'thos' is a string '52 53 str1.startswith ('sub') # start with 54 str1.endswith ('sub ') # end with 55 str1.lower () # Convert to lowercase 56 str1.upper () # Convert to uppercase

View Code

List and tuples (tuples cannot be modified)


1 >>> lst1 = ['A'] 2 >>> lst1.append ('B') # Add 3 >>> lst1 4 ['A ', 'B'] 5 >>> lst2 = ['C', 'D'] 6 >>> lst1.extend (lst2) # New extension 7 >>> lst1 8 ['A', 'B', 'C', 'D'] 9 >>> lst1.insert (0, 'z ') # insert 10 >>> lst111 ['Z', 'A', 'B', 'C', 'D'] 12 >>> lst1.pop () # Remove end 13 'D' 14 >>> lst115 ['Z', 'A', 'B', 'C'] 16 >>> lst1.remove ('Z ') # Delete the specified element 17 >>> lst118 ['A', 'B', 'C'] 19 >>> lst1 = ['A', 'B ', 'C', 'D'] 20 >>> lst2 = lst1.copy () #21 for the shortest python3 >>>> lst2 = lst1.copy () 22 >>> lst223 ['A', 'B', 'C', 'D'] 24 >>> lst2.clear () # Clear list 25 >>> lst226 [] 27 >>> del lst2 # Delete list 28 >>> lst129 ['D', 'C', 'B ', 'A'] 30 >>> lst1.sort () # Sort 31 >>> lst132 ['A', 'B', 'C ', 'D '] 33 >>> lst1.append ('A') 34 >>> lst1.count ('A') # count 35 236 >>> lst137 ['A ', 'B', 'C', 'D', 'A'] 38 >>> len (lst1) # Length 39 540 >>> lst1.index ('A ') # index 41 042 >>> lst1.index ('A', 1) # index 43 4

View Code

Dictionary


1 >>> dic1 = {'key1': 'A', 'key2': 'B'} 2 >>> dic1.get ('key1') # obtain the dictionary value, if None is returned by default, you can specify 3 'A' 4 >>> dic1.get ('key3') 5 >>> dic1.items () 6 dict_items ([('key2 ', 'B'), ('key1', 'A')]) # returns the list of tuples 7 >>> list (dic1.items () 8 [('key2 ', 'B'), ('key1', 'A')] 9> dic1.keys () # return to the keys list 10 dict_keys (['key2', 'key1']) 11 >>> dic1.values () # Return value list 12 dict_values (['B', 'A']) 13 >>> dic2 = dic1.copy () # light copy 14 >>> dic215 {'key2': 'B', 'key1 ': 'A'} 16 >>> dic1 ['key3'] = 'C' # assign a value (Modify) 17 >>> dic118 {'key2': 'B ', 'key1': 'A', 'key3': 'C'} 19 >>> dic1.pop ('key1 ') # Delete the specified key20'a' 21 >>> dic122 {'key2': 'B', 'key3': 'C'} 23 >>> dic1.get ('key1 ', 'A ') # VALUE. no value is returned for 'a '24' a' 25 >>> dic126 {'key2':' B ', 'key3 ': 'C'} 27 >>> dic1.setdefault ('key1', 'A') # set the default value (it seems useless) 28 'A' 29 >>> dic130 {'key2': 'B', 'key1': 'A', 'key3 ': 'C'} 31 >>>> dic3 = {'name': 'update'} 32 >>> dic1.update (dic3) # update 33 >>> dic1 34 {'key2 ': 'B', 'name': 'update', 'key1': 'A', 'key3 ': 'C'} 35 >>> del dic3 # delete 36 >>> dic137 {'key2': 'B', 'name': 'update', 'key1 ': 'A', 'key3': 'C'} 38 >>> len (dic1) # Length 39 4

View Code

The above is the content of python Basics 1. for more related articles, please follow the PHP Chinese network (www.php1.cn )!

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.