Python self-study day-One day note-String concatenation + if while for loop, python -- one
Note
Single line comment #
Comment ''' in multiple lines. Three single quotes or three double quotes """
''' With three quotation marks, you can assign values to multiple rows.
User interaction input
String concatenation
+ "" % () "". Format () is 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 the 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: used after getpass is referenced. getpass. getpass ()
If else usage
Example:
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
Example:
Myage = 37
InputAge = int (input ("please input my age :"))
If InputAge = Myage:
Print ("It's right ")
Elif InputAge> Myage:
Print ("Think small ")
Else:
Print ("Think big ")
While else loop
Count = 0
While count <3:
Myage = 37
InputAge = int (input ("please input my age :"))
If InputAge = Myage:
Print ("It's right ")
Break
Elif InputAge> Myage:
Print ("Think small ")
Else:
Print ("Think big ")
Count + = 1
Else:
Print ("fuck you! ")
Break jumps out of the current cycle
Continue jumps out of the current loop and enters the next loop
Job
Write login interface
- Enter user name and password
- Welcome information displayed after successful authentication
- Lock after three wrong attempts
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 is incorrect! Please input again ...")
Else:
Print ("three errors, account locked ")
Open (r 'C: \ Users \ Administrator \ Desktop \ lockname.txt ', 'A'). write (username +' \ n ')
String Basics
(1) Escape strings
(2), raw string -- escape mechanism open (r 'C: \ tmp \ a.txt ', 'a + ')
(3) Unicode string
(4) format the string "age % d, sex % s, record % m. nf" % (20, "man", 73.45)
Basic string operations
+ Join, * repeat, s [I] index, s [I: j] slice, and for loop Traversal