Comments
Single-line Comment #
Multiline comment "' three single quotes or three double quotes" "
"Use three quotes to assign a multiple line
User interaction Input
string concatenation
+ ""% () "". Format () recommended
Name = input ("Name:")
age = Int (input (' Age: '))
Sex = input ("Sex:")
Example: +
# string concatenation +
INFO1 = '----info in ' + name + '---
Name: ' + name + '
Age: "+ Age +" "
Sex: ' + sex + '
'''
Example: ""% ()
#% format string
info = '------Info in%s-------
name:%s
age:%d
sex:%s
'% (' name ', ' name ', age, ' sex ')
# "". Format ()
Info3 = ' '---info in {_name}---
Name:{_name}
Age:{_age}
Sex:{_sex}
'. Format (_name=name,
_age=age,
_sex=sex)
Info4 = ' '---info in {0}---
NAME:{0}
Age:{1}
SEX:{2} '. Format (name, age, Sex)
Module definition:
Ciphertext password: Use after Getpass reference, Getpass.getpass ()
If Else uses
Cases:
Username = "username"
Password = "123456"
_username = Input ("Username:")
_PASSWD = Input ("Password:")
If username = = _username and Password = = _passwd:
Print ("Welcome user {Name} to Beij". Format (Name=username))
Else
Print ("Invalid username or passwd")
If elif Else
Cases:
Myage = 37
inputage = Int (input ("Please input Me Age:")
if inputage = = Myage:
Print ("It ' s Right")
Elif inputage > Myage:
Print ("small")
Else
Print ("Big")
While else loop
Count = 0
While Count < 3:
Myage = 37
inputage = Int (input ("Please input Me Age:")
if inputage = = Myage:
Print ("It ' s Right")
Break
Elif inputage > Myage:
Print ("small")
Else
Print ("Big")
Count+=1
Else
Print ("Fuck you!")
Break jumps out of the current loop
Continue out of the current loop and into the next loop
Homework
Write Login interface
Enter User name password
Show welcome information after successful authentication
Wrong three times, lock up.
Old_uname = open (R ' C:\Users\Administrator\Desktop\username.txt ', ' R '). ReadLines ()
Count = 0
While Count < 3:
Username = input ("Please your Username:")
passwd = input ("Please your passwd:")
For I in Old_uname:
If i = = Username:
Print ("Wolcome to your Blogs:{_uname}". Format (_uneme=username))
Break
Else
Continue
Else
Count + 1
If Count = 3:
Continue
Print ("The password you entered are incorrect!please input again ...")
Else
Print ("Three errors, account locked")
Open (R ' C:\Users\Administrator\Desktop\lockname.txt ', ' a '). Write (username + ' \ n ')
The
String base
(1), Escape string
(2), Raw string--escape mechanism open (R ' C:\tmp\a.txt ', ' A + ')
(3), Unicode string
(4), formatted string "Age%d,sex%s,record%m.nf"% ( "Man", 73.45
String Base action
+ Connection, * Duplicate, S[i] Index (index), s[i:j] slice (slice), for loop traversal