Re-learn Python, learn python

Source: Internet
Author: User

Re-learn Python, learn python
Application of while statement

 

Example:

1 "2 Statement: the user can log on to the system at most three times 3 after the third failure, the program ends 4 5" 6 user_table = {"python ": "important", "java": "more_important", "shell ": "linux"} 7 time = 1 8 9 while time <4:10 user_name = input ("Enter your Username:") 11 user_password = input ("enter your password: ") 12 if user_name in user_table.keys () and user_password in user_table.values (): # very low, because the user name and password can be mixed, update it later 13 print (" welcome, master !! ") 14 break15 else: 16 print (" you failed login system !! ") 17 if time = :18 print (" Three chances are all userd !! ") 19 time + = 1

PS: 1. The running result is not displayed.

2. The ide I selected is Pycharm, and the registration code is my Baidu Keyword: pycharm 2017 registration code (I can use it by myself, but do not use it in regular companies)

 

 

 

Understanding and examples of continue and break statements

 

Continue: the current cycle jumps out and continues the next cycle

Break: jumps out of the current cycle

1 flag = False 2 for I in range (10): 3 if I <6: 4 continue #0 to 5 is skipped, the subsequent print statement cannot execute 5 print (I) # When I = 6, print 6 for j in range (10): 7 print (j) 8 if j = 5: 9 flag = True10 break # interrupts the current loop, and the second for loop is interrupted 11 # if flag: 12 # if break cancels the comments of the two rows, the first for loop is interrupted and can be determined based on the format.

 

 

 

Python classic data type-list)

Example:

1 list1 = ["a", "B", "c", "e", "f", "d"] 2 3 print (list1 [3]) # The first index is the index value starting from left or right by default. The following index example is also 4 print (list1 [1:]). # The first value to the last value 5 print (list1 [1:-1]) #-1 is the last and second value 6 print (list1 [:-1]) # The list is reversed. -1 indicates 7 print (list1 [4:-1]) from right to left 8 print (list1) 9 10 11 # list Modification 12 list1.append ("python ") 13 print (list1) 14 15 # modify the value of the specified position 16 list1 [2] = "java" 17 print (list1) 18 19 # insert 20 list1.insert (1, "ruby ") 21 print (list1) 22 23 # Delete method remove del pop24 list1.remove ("java") # You can also use the index to specify the value, list1.remove (list [1]) 25 print (list1) 26 list1.pop (2) 27 print (list1) 28 del list1 [1] 29 print (list1)

Running result:

E ['B', 'C', 'E', 'F', 'D'] ['B', 'C', 'E ', 'F'] ['D', 'F', 'E', 'C', 'B', 'a'] ['F', 'E ', 'C', 'B', 'a'] ['A', 'B', 'C', 'E', 'F ', 'D'] ['A', 'B', 'C', 'E', 'E', 'F', 'D', 'python'] ['A ', 'B', 'java', 'E', 'F', 'D', 'python'] ['A', 'ruby', 'B ', 'java', 'E', 'F', 'D', 'python'] ['A', 'Ruby ',' B ', 'E ', 'F', 'D', 'python'] ['A', 'Ruby ', 'E', 'F', 'D ', 'python'] ['A', 'E', 'F', 'D', 'python'] The process has ended. Exit code 0.

 

PS: the built-in functions of the list are put in tomorrow's notes

 

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.