Number, String, list, Boolean, tuple, Dictionary, collection(Small knowledge point)
Naming rules for variable names?
#1. Variable names can only be any combination of letters, numbers, or underscores
#2. The first character of a variable name cannot be a number
#3. Keyword cannot be declared as a variable name
Define the way:
Camel Type: ageoldboy=53
underline: age_oldboy=53
Constants: generally uppercase letters :age_of_oldboy=54
Implementation process (emphasis!!!) )
Python3 C:\test.py
1. Start Python3.exe First
2. Read the C:\test.py from the hard disk into memory
3, explain the execution of the file content (recognize Python syntax)
>>> aa=1>>> print (Type (AA)) # #看类型 <class ' int ' >>>> print (ID (aa)) # #看ID值4459412848
String:
# # Priority to master the operation:
msg= ' Hello world ' #优先掌握的操作: #1, by index value (positive to take + reverse) : Only take, Gu Tou disregard tail >>> print (Msg[0:3]) hel>> > print (Msg[5:1:-1]) oll>>> print (msg[-1::-1]) # #2. Length len () #print (Len (msg)) ## In fact, the call is Msg._len_ () # #3. Member Arithmetic >>> print (' o ' in msg) true>>> print ( ' hh ' in msg) false# #4. Remove blank strip>>> passwd= ' 123 ' >>> Print (Passwd.strip ()) 123>>> name= ' yaoruixue ' >>> print (name.strip ()) yaoruixue# #5. Slicing split>>> user_info= ' Root:x:0:0::/root:/bin/bash ' >>> print (user_ Info.split (': ')) [' Root ', ' x ', ' 0 ', ' 0 ', ' ', '/root ', '/bin/bash ']>>> print (user_info.split (' root ')) [', ': x:0:0::/', ':/bin/bash ']# #切分几次 >>> print ( User_info.split (' root ', 2)) [', ': x:0:0::/', ':/bin/bash ']>>> print (' User_info.split ' Root ', 1)) [', ': x:0:0::/root:/bin/bash '] #6. Loop #encoding:utf-8msg= ' Hello world ' #打印字符串的每个元素 # First Method N=0size=len (msg) while n < size: print (Msg[n]) n+=1# The second method of For n in msg: print (N)
String Type Summary:
# 1 Save Multiple Values # 2 ordered # 3 variable
#需要掌握: Remove, remove left, right #1.strip,lstrip,rstripname= ' .... Yaoruixue ... ' Print (Name.strip ('. ')) Print (Name.lstrip ('. ')) Print (Name.rstrip ('. ')) yaoruixueyaoruixue........yaoruixue# #lower, Upper (string case) print (' Yaoruixue '. lower ()) print (' Yaoruixue '. Upper ()) yaoruixueyaoruixue# #startswith,endswith with what opening end msg= ' Yao rui xue ' Print (Msg.startswith (' Yao ')) Print (Msg.endswith (' Xue ')) Print (Msg.startswith (' a '))-Truetruefalseformat three ways to format the string, from the python2.6 new. (The official recommended way, the% way will be eliminated in later versions) print (' my name is %s my age is %d ' % (' Yao ', ") print (' my name is {} my age is {} '. Format (' Yao ', ') ') my name is yao my age is 23print (' {0} {1} {0} '. Format (' Yao ', 23)) # #通过位置参数, the first parameter is 0, and then 1: yao 23 yaoprint (' My name is {name} my age is {age} '. Format (age=19,name= ' Yao ') names={' name ': ' Kevin ', ' name2 ': ' Tom '}print ' hello {names[name]} i am {names[name2]} '. Format (names=names) # #通过字典key hello kevin i am tom#replace Replace msg= ' hello world my name is yao ' Msg=msg.replace (' Yao ', ' Xue ', 1) print (msg) #isdigit判断是否为整形age =input (' >>: ') ). Strip () If age.isdigit (): age=int (age) Else: print ("Number must be entered" ) #了解 #, Find,rfind,index,rindex,count#2, center,ljust,rjust,zfill#3, Expandtabs#4, Captalize,swapcase,title#5, is digital series, is other
Homework:
1 Exercises
-
Write code
-
Implement user input user name and password, When the user name is seven and the password is 123, the login is successful, otherwise the login fails!
-
Implement user input user name and password, when the user name is seven and the password is 123, the display login is successful, otherwise the login failed, the failure to allow repeated input three times
-
Implement user input user name and password, when the user name is seven or Alex and the password is 123, the display login is successful, otherwise the login failed, the failure to allow repeated input three times
-
write code
A. Using a while loop to implement output 2-3+4-5+6...+ 100 and
B. Use the while loop to implement output 1,2,3,4,5, 7,8,9, 11,12 use while loop to implement all odd numbers in output 1-100
e. Use the while loop to implement all even
-
n1 = 123456 n2 = n1
2 job: Writing the Login interface
Basic requirements:
-
Let the user enter the user name password
-
Show welcome message after successful authentication
-
Exit the program after a three-time error
Upgrade Requirements:
-
Can support multiple users login (prompt, through the list to save multiple account information)
-
After the user 3 authentication failed, quit the program, start the program again when attempting to log on, or lock the status (hint: the user locked state must be stored in the file)
-
Python Base 1-string