Python Preview-section III

Source: Internet
Author: User
Tags extend shuffle

A. Introduction of random packets, randomly number of packets import random to generate integers in random range: random.randint (1,100) randomly remove an element from a sequence Random.choice ("abc") Generates a random decimal random.random () generates a decimal number with a specified length of # # random.random () *100# generates a decimal of 3-bit integers random.random () *1000 generates an integer int of the specified digits ( Random.random () *1000) disrupts the order of the internal elements of the list a=[1,2,3,4,5]random.shuffle (a) print ab. Read:>>> lista=[1,2,3,4]>> > Print lista[0]1>>> print lista[-1]4>>> print lista[1]2>>> print lista[-2]3c change:>> > lista[0]=lista[0]+10>>> lista[1]=20>>> Print lista[11, 3, 4]d traverse:>>> for I in Lista:.. . Print I ... 112034e censored:>>> lista=[1,2,3,4]>>> del lista[-1]>>> del lista[0]>>> print lista[2, 3] F add:>>> lista[2, 3]>>> lista.append ("abc") >>> lista[2, 3, ' abc ']>>> lista.append ([ ) >>> lista[2, 3, ' abc ', [1, 2, 3]]>>> lista.extend ([4,5,6]) >>> print lista[2, 3, ' abc ', [ 1, 2, 3], 4, 5, 6]>>> Lista.insert (1,1000) >>> lista[2, 1000, 3, 'ABC ', [1, 2, 3], 4, 5, 6]g Consolidated List:>>> a=[1,2,3]>>> b=[4,5,6]>>> a+b[1, 2, 3, 4, 5, 6]>>>& Gt;>>f Consolidated List:>>> a=[1,2,3]>>> b=[4,5,6]>>> a+b[1, 2, 3, 4, 5, 6]>>>>> >g stored functions in the list, and subsequent calls to stored functions:>>> sum<function sum at 0x000000000231df98>>>> a.append (sum) > >> print A[1, 2, 3, <function sum at 0x000000000231df98>]>>> a[-1]<function sum at 0x000000000231d F98>>>> a[-1] [3>>> print a[-1] (3h) .......... Gorgeous split line ....... ....... SWOT-opportunity/risk/advantage/disadvantage   function is not a method, but in an object-oriented class, it is called a method. Examples of functions: Example: Addition and division short-circuit statement:  none, no return value and Pass import random  a range of random values: Random.randint (A, B) Random decimal values in a sequence (str/list/tuple): Random.choice ()   Fetch (0,1): Random.random () disrupts the order of the internal elements of the list: Random.shuffler (a)   Introducing random packets, randomly number of packages import random to generate integers in random range: random.randint (1,100) randomly extracts an element from a sequence Random.choice ("abc") Generates a random decimal random.random () generates a decimal number with a specified length of # random.random () *100# generates a fractional number of 2-bit integers random.randoM () *1000 the integer int (random.random () *1000) that generates the specified number of digits disrupts the order of the internal elements of the list a=[1,2,3,4,5]random.shuffle (a) print A example: randomly generated 135 mobile phone number: List to add elements to the list: List.append (), can be put into variables, can also be put into functions.   Delete entire List:del list Delete the element at the specified location (1th): del list[0] Delete a value (1th): List.remove (123) List can put list (full list): Listc.append ( Lista) Add all the values in Lista and Listb to LISTC: listc.extend (lista) list read value:list[0]/list[1]    "Teacher Note:" Introduce the random package, Random number of packets import random generates integers in random range: random.randint (1,100) randomly extracts an element from a sequence Random.choice ("abc") generates a random fractional random.random () Generates a decimal number with a specified length # random.random () to generate a 2-bit integer, *100# generates a decimal random.random () of 3-bit integers *1000 generates an integer int (random.random () *1000) of the specified number of digits A=[1,2,3,4,5]random.shuffle the internal element order of the list (a) print A------------------------------------------------------------------------------>>> lista=[1,2,3,4]> >> lista[0]1>>> lista[1]2>>> lista[2]3>>> lista[3]4>>> lista[4]Traceback ( Most recent call last): File "<stdin>", line 1, in <module>indexerror:list index out of range>>>>& Gt;> for I in Lista: ... Print I ... 1234>>> del A[-1]traceback (most recent): File ' <stdin> ', line 1, in <module>nameerror:name ' A ' is not defined>>>>>> del lista[-1]>>> lista[1, 2, 3]>>>----------------------- ---------------->>> del lista[1]>>> lista[1, 3]>>> lista.remove (1) >>> lista[3] >>> Lista.remove (3) >>> lista[]>>> lista.remove (3) Traceback (most recent call last): File " <stdin> ", line 1, in <module>valueerror:list.remove (x): X not in List>>>>>>>>> Lista=[1,2,3]>>> listb=[4,5,6]>>> listc=[]>>> listc.append (lista) >>> Listc.append (LISTB) >>> print listc[[1, 2, 3], [4, 5, 6]]--------------------------------------->>> Lista=[1,2,3,1,3,1]>>> lista.remove (1) >>> lista[2, 3, 1, 3, 1]>>> lista.remove (1) >> > lista[2, 3, 3, 1]>>> lista.remove (1) >>> Lista[2, 3, 3]--------------------------------------->>> a=[10,20,30]>>> b=[1,2,3]>>> B.append (a) >>> b[1, 2, 3, [Ten, 30]]>>> B.extend (a) >>> print b---------------------------------------Read:>>> lista=[1,2,3,4]>>> print lista[0]1>>> Print lista[-1]4>>> print lista[1]2>>> print lista[-2]3---------------------------------------change: >>> lista[0]=lista[0]+10>>> lista[1]=20>>> Print lista[11, 3, 4]---------------------- -----------------Traverse:>>> for i in Lista: ... print i ... 112034---------------------------------------Delete:>>> lista=[1,2,3,4]>>> del lista[-1]>>> Del lista[0]>>> print lista[2, 3]---------------------------------------increase:>>> lista[2, 3]>> > Lista.append ("abc") >>> lista[2, 3, ' abc ']>>> lista.append ([]) >>> lista[2, 3, ' ABC ', [1, 2, 3]]>>> lista.extend ([4,5, 6]) >>> print lista[2, 3, ' abc ', [1, 2, 3], 4, 5, 6]>>> Lista.insert (1,1000) >>> lista[2, 1000, 3, ' abc ', [1, 2, 3], 4, 5, 6]---------------------------------------Consolidated List:>>> a=[1,2,3]>>> b=[4,5,6] >>> a+b[1, 2, 3, 4, 5, 6]>>>>>>---------------------------------------the stored function in the list, and subsequent calls to the stored function: >>> sum<function sum at 0x000000000231df98>>>> a.append (sum) >>> print a[1, 2, 3, < function sum at 0x000000000231df98>]>>> a[-1]<function sum at 0x000000000231df98>>>> a[-1] ( 3>>> Print A[-1] 3  

Python Preview-section III

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.