Python3 default Character Set default is Unicode
Python2 default character set is ASCII by default
Print (' hello_world! ')
name = ' Little Tiger '
Age = 27
Print ("Let ' s go!")
Print (' You're so Handsome ')
Print ("Let's Go" handsome ")
‘‘‘
This is a multi-line comment
You can do it in three quotes.
‘‘‘
Note that you cannot use keywords to make variable names in Python
name1 = input (' Please enter your name: ')
Print (' Your name is: ', name1)
Branch statement if elif else
# username = input (' Please enter your username: ')
# password = input (' Please enter your password: ')
# if Username = = ' Wangsilei ' and password = = 123456:
# print (' Welcome! ‘)
# Else:
# Print (' Account/Password Error! ‘)
# input is received by the STR type
# score = input (' Please enter your score: ')
# if INT (score) < 60:
# if Int (score) >= 50:
# print (' 50-60 ')
# Else:
# print (' Too low! ‘)
# print (' less than lattice! ‘)
# elif <= Int (score) < 80:
# print (' Pass! ‘)
# elif <= Int (score) < 90:
# print (' Good! ')
# Else:
# print (' Excellent! ‘)
# = = = < > <= >= comparison operator
Sex = input (' Please enter your gender: ')
if sex = = ' male ' or sex = = ' Female ':
Print (' Gender input is correct! ‘)
Else
Print (' wrong sex input! ')
# Boolean type True False
For, while Loop
Import Random
# Loop Iteration Traversal
# for while
# Break End Loop
# continue end the loop and continue the next cycle
# count = 0
# while Count < 10:
# print (' haha-ha ')
# count + = 1
# print (' 666 ')
# break
# Else: # loop runs normally and ends it
# print (' done! ')
# count = 0
# While Count < 3:
# username = input (' Please enter name: ')
# password = input (' Please enter password: ')
# if Username = = ' Wangsilei ' and password = = ' 123456 ':
# print (' Login successful! ‘)
# break
# Else:
# print (' Name/password error! ‘)
# count + = 1
# Else:
# print (' Excessive number of errors! ')
# for I in range (10):
# Print (i)
Random_number = Random.randint (1, 1000)
For I in range (3):
Number = Int (input (' Please enter the numbers you guessed: '))
If number > Random_number:
Print (' You guessed too big! ')
Elif Number < random_number:
Print (' You guessed too small! ')
Else
Print (' You guessed right, the answer is: ', Random_number)
Break
Guess number game
Import Random
# The following code does not add continue can also
Random_number = Random.randint (1, 1000)
Count = 0
While Count < 7:
Count + = 1
Number = Int (input (' Please enter the numbers you guessed: '))
If number > Random_number:
Print (' You guessed too big! ')
Continue
Elif Number < random_number:
Print (' You guessed too small! ')
Continue
Else
Print (' You guessed right, the answer is: ', Random_number)
Break
String formatting
Username = input (' Please enter your name: ')
Time = ' 2017-12-17 17:19:00 '
Print (' Welcome ' + username) # concatenation of two strings via + #
Print (' Welcome to%s, time is%s '% (username) #) # This is a highly efficient way
Print (' Welcome to {}, time is {} '. Format (username) # This is better than the second kind
Print (' Welcome {name} ', Time is {date} '. Format (Name=username, date=time))
#%s followed by string%d followed by integer%f decimal type
Number = 123
Print ('%.2f '% number) # reserved Two decimal places
Login Applet
Username = ' Wangsilei '
Password = 123456
For I in range (3):
Name = input (' Please enter account name: ')
PWD = input (' Please enter account password: ')
If name = = username and int (pwd) = = Password:
Print (' {} Welcome to login '. Format (name))
Break
elif name = = "or pwd = =":
Print (' Account name/account password cannot be empty ')
Elif name.isspace () or Pwd.isspace ():
Print (' Account name/account password cannot be a space ')
Else
Print (' Account name/password Error! ‘)
Generate phone number and write to txt applet
Import Random
phone_lists = [' 130 ', ' 131 ', ' 132 ', ' 133 ', ' 134 ', ' 135 ', ' 136 ', ' 170 ', ' 180 ']
Phone_str = ' 0123456789 '
n = Int (input (' How many random phone numbers are generated: '))
With open (R ' C:\Users\wangsilei\Desktop\phone.txt ', ' W ') as F:
For I in range (n):
Phone_numbers = Random.choice (phone_lists) + '. Join (Random.choice (PHONE_STR) for J in Range (8)) + ' \ n '
F.write (Phone_numbers)
Python's HelloWorld