Python Learning-w1

Source: Internet
Author: User
Tags case statement

Just beginning to learn python for a week, master the basic knowledge of program development:

1 variable assignment and reference

2 Conditional control statements, such as if ... Else,for loops, while loops, etc.

3 arrays, tuples, dictionaries basic concepts and operations (add, delete, change). The sense arrays and dictionaries are used extensively in Python development

4 Basic module OS, current main understanding OS.GETCWD (), get current path and os.chdir () switch path

5 Basic Module Sys, mainly about SYS.ARGV get the number of parameters

6 file basic operation and corresponding mode


code Example 1:

Impersonate user login, mainly include: Verification failure lock Account + account Registration + Password Modification + View account + Create account

#!/usr/bin/env python
#-*-Coding:utf-8-*-

User_allow = []
User_deny = []

While True:
Banner = "Welcome"
Print (Banner.center (20, "="))
If User_allow:
For I in range (3):
Banner_login = "Please enter your name and password to login!"
Print (Banner_login)
LName = input ("Login Name:")
Lpwd = input ("Login Password:")
If User_deny:
For M in range (len (user_deny)):
if lname = = User_deny[m][0]:
Print ("It is locked. Please contract your administrator! ")
Else
Pass
For-N in range (len (user_allow)):
if lname = = User_allow[n][0] and lpwd = = User_allow[n][1]:
Print ("Welcome%s to login!"% lname)
select = Input ("list[l]| Reset Password [c]| users[a]| LOGOUT[X]: ")
If select = = "L":
For Luser in range (len (user_allow)):
Print (User_allow[luser][0])
Break
Elif select = = "X":
Print ("logout! Bye Bye! ")
Break
Elif select = = "C":
Print ("Now reseting the password ...")
Opwd = input ("Your old password:")
Npwd = input ("Your New password:")
NPWD2 = Input ("Confirm:")
if npwd = = Npwd2:
if opwd = = User_allow[n][1]:
USER_ALLOW[N][1] = Npwd2
Print ("The password is updated!")
Else
Print ("It's not match!")
Continue
Elif select = = "a":
Print ("==add new user info==")
Add_user = input ("The New Account:")
ADD_PWD1 = input ("The Password:")
ADD_PWD2 = Input ("Confirm:")
If Add_user not in user_allow and add_pwd1 = = Add_pwd2:
User_allow.append ([ADD_USER,ADD_PWD2])
Print ("The user is added.")
Continue
Else
Print ("It is not match!")
Continue
Else
i = 2-i
If i > 0 and I <=2:
Print ("Sorry,you has%s times"% i)
elif i = = 0:
Print ("Sorry,your account is locked!")
User_deny.append ([lname,lpwd])
Exit ()
Else
Print ("Please register first!")
uname = input ("New Name:")
Upwd = input ("New Password:")
User_allow.append ([uname,upwd])
Print ("The Register is successful!")

Example 2:

Level 3 menu, mainly includes display menu list + exit + return function:

#!/usr/bin/env python
#-*-Coding:utf-8-*-

Menu = {
"Servers": {
"HP": {
"G9": ["2c/32t", "256G", "1t*8", 50000]
}
},
"Labtops": {
"IBM": {
"T560": ["1c/8t", "8G", "128ssd*1+1t*1", 12000]
}
},
"Networks": {
"CISCO": {
"C2950": ["48P", 6000]
}
}
}


While True:
For I in menu:
Print (i)
Select1 = input ("Your Select[quit:q]:") #servers
If Select1 in menu:
While True:
For I2 in Menu[select1]:
Print ("\ T", I2)
Select2 = input ("Your select[back:x| QUIT:Q]: ") #HP
If Select2 in Menu[select1]:
While True:
For i3 in Menu[select1][select2]:
Print ("\t\t", i3)
SELECT3 = input ("Your select[back:x| QUIT:Q]: ") # G9
If SELECT3 in Menu[select1][select2]:
While True:
For I4 in Menu[select1][select2][select3]:
Print ("\t\t\t", I4)
Select4 = Input ("[back:x| QUIT:Q]: ")
if Select4 = = "X":
Break
elif Select4 = = "Q":
Exit ()
elif Select3 = = "X":
Break
elif Select3 = = "Q":
Exit ()
elif Select2 = = "X":
Break
elif Select2 = = "Q":
Exit ()
elif Select1 = = "Q":
Exit ()


Feel:

Used to use Shell to write scripts, in the case of simple features, or like the shell of the simple rough, straightforward. Especially close to system level operations, reading files, filtering strings and other operations.

Python has no grep filtering capabilities like the shell, and many judgments rely primarily on the in statement. It also has no case statement, can build the menu, currently mainly through the dictionary form.

When I first started writing shell scripts, I was usually familiar with commands, but lacked ideas. And now learning Python development, often have ideas, but through what functions to achieve, more unfamiliar.

I believe that after a period of practice, familiar with the characteristics of Python, to master more command commands, to develop more complex programs will be more handy.

This article is from "Clark's operation and Maintenance" blog, reproduced please contact the author!

Python Learning-w1

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.