Python Notes---DAY3: formatted output, for loop, list action

Source: Internet
Author: User

1. Use quotation marks:

Multi-line variable with three quotation marks

Python single and double quotes are the same, note the use of the outermost quotation marks when there are quotes in the command content

2. formatted output:

Name=input ("Name:")

Age=int (Input ("Age:"))

Job=input ("Job:")

Salary=input ("Salary:")

Number=input ("Number:")

If Salary.isdigit (): Determines whether the input is a number, where . The function is to bring up its properties

Salary=int (Salary)

Else

Exit ("must input digit") exits the program by entering an error

Msg= ' multi-line variable using three quotation marks

-----Info of%s----- %s as a placeholder,s is string

name:%s

Age:%s

Job:%s

salary:%s

You'll retire in%s years

number:%d must enter a number,D is a digit integer , andfloat is a floating point in%f , that is, decimals

-----End-----

'% (Name,name,age,job,salary,65-age) age converted to int format makes it possible to subtract

Print (msg)

3. Other directives:

CTRL +? a few lines of command all commented

string concatenation as far as possible, rather than +, because + slow running speed

for is a finite loop, while is an infinite loop

4. For Loop:

For I in Range (1,101,2):

If I<50 or i>70: determine output I range

Print ("Loop:", i)

Range ,1 is the starting number,101-1 is the end number, and2 is the step size

5. User login:

username1= "Lilei"

Password1= "123"

Passed_authentication=false Adding a flag bit

For I in range (3): loop 3 times

Username=input ("Name:")

Password=input ("Password:")

If Username==username1 and Password==password1:

Print ("Welcome%s login ..."%username)

Passed_authentication=true

Break input is correct after exiting as secondary loop

Else

Print ("Invalid username or password!") the hint does not specify the error, otherwise the information will be guessed

If Passed_authentication==false: three times output fails when prompted

Print (" smelly Rogue ")

6. For next else Edition User login:

username1= "Lilei"

Password1= "123"

For I in range (3):

Username=input ("Name:")

Password=input ("Password:")

If Username==username1 and Password==password1:

Print ("Welcome%s login ..."%username)

Passed_authentication=true

Break has been break for the for after not executed with the for same column else

Else

Print ("Invalid username or password!")

else: for normal execution and then else

Print (" smelly Rogue ")

7. User Login while version:

username1= "Lilei"

Password1= "123"

Counter=0

While counter<3:

Username=input ("Name:")

Password=input ("Password:")

If Username==username1 and Password==password1:

Print ("Welcome%s login ..."%username)

Break

Else

Print ("Invalid username or password!")

Counter+=1

If counter==3:

Keep_going_choices=input (" do you want to continue?" : [y/n] ")

If keep_going_choices== "Y":

Counter=0 If you want to continue typing, clear the number of cycles by 0

Else

Print (" smelly Rogue ")

8 . Use of continue and break:

Exit_flag=false

For I in range (10):

If i<5:

Continue ends this cycle and goes back to the beginning of a cycle.

Print (i)

For j in Range (10):

Print ("Layer 2", j)

If j==6:

Exit_flag=true defines the flag bit

Break ends the loop and jumps out of the loop

If Exit_flag:

Break use flag to jump two cycles

9, the operation of the list:

a=["A", "B", "C", "D", "E", "F"]

B=a[1:4] from the list of 2 data to the first 4 data

C=a[1:] take the last data from the first 2 data in the list

D=a[1:-1:2] from the list of 2 data to the bottom 2 data, the step is 2

E=a[-1:1:-2] from the list of right number 2 data to the left number 2 data, the step is 2

A.append ("ZZZ") add content at the end of the list, append

A.insert (1, "ZZZ") Add content as position 1 Insert list

a[1:3]=["LL", "DD"] Modify the contents of the list 2 data and the 3 data

A.remove ("a") removes "a" from the list , deleting only one element

A.pop (1) Delete The first 2 data in the list , without specifying the last 1 elements are deleted by default

Del A[0:4:2] delete 1 and 3 data from a list

B=a.count ("a") calculates the number of repetitions of an element in a list

A.extend (b) b after The element has been added to a

C=a.index ("B") queries the position of "b" In A, if a contains multiple "b " "Shows thelocation of section 1 "B"

A.reverse () inverts the elements in a

A.sort () sorts the elements of a in ASCII Order, uppercase before,a.sort (recerse=true) Reversible Order

A.clear emptying the list

Python Notes---DAY3: formatted output, for loop, list action

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.