Python Learning--looping

Source: Internet
Author: User

Today I learned the first day of Python . Today mostly the data types are spoken. Contains int, float, string, list, bool, and loop.

If you want to see the type of a variable you can use:print (type(variable name))

The data type can also be cast. Method:Int(variable name),float(variable name),str(variable name),list(variable name).

One, one of today's priorities loops . Loops are repeated execution of the loop body inside the code, you can use for and while.

The For loop does not require a register and cannot write a dead loop. To give a for-loop chestnut:

For I in range (5):

Print ("Hello!" ")

Repeat print 5 times "Hello".

Raise a while loop of chestnuts. The while loop first sets a register

While I <5:

Print ("You're pretty!" ")

I+=1

I is the register, each time after the end of the cycle to increase

continue andbreak are used in the loop, so let's talk about their differences:

continue is to end this cycle and go to the next cycle.

break can only be used inside the loop body to end the entire loop, so the code behind the break will not be executed.

You can try the following code, from the results to verify the difference between the two

For I in range (5):

print ("Hello!" ")

Continue

While I <5:

Print ("You're pretty!" ")

Break

I+=1

Second, today another focus is list

The list is done by subscript, and the subscript is starting from 0. For example a list of cpy=[' A ', ' B ', ' C ', ' d ']. If you want to remove the element D, the way is cpy[3].

You can actually construct a list using the range () method.

For I in Rang (5):

A[i]=i or A=list (range (5))

Print a

For the list can be added, delete, change, check operation.

Increment: For example, add element e to list cpy.

cpy.append (' e ') at the last position of the list, add element E

Cpy.insert (4, ' e ') adds element e at the specified position in the list

Delete: For example, remove the element e from the list cpy.

Cpy.pop (4) incoming is the subscript of the list, deleting the element at the specified position in the list

cpy.remove (' e ') is passed in to the element in the list, removing the specified element from the list

del Cpy[4] incoming is the subscript of the list, deleting the element at the specified position in the list

cpy.clear () empties the entire list of elements

Change: For example, modify list cpy D element is DD

cpy[3]= ' DD ' is directly assigned according to subscript

Look at the value of the third element of the list cpy, for example

CPY[2]

Other methods:

The number of elements in the cpy.count (' dd ') list

Types of type (cpy) list

Cpy.reverse () reverses the elements in the list

cpy.index (' DD ') DD This element one occurrence of the subscript. If the element does not exist in the list, an error is found.

stus=["Zhang Shuai", "John Doe"]

cpy.extend (stus) merges elements from the list Stus into the cpy list

stus+cpy is to merge the Stus and cpy lists into a new list

cpy.sort () list elements are sorted by default ascending

Cpy.sort (reverse=true) list in descending order

Python Learning--looping

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.