Python knowledge based on Python3.5
output Hello World
print(“HelloWorld”)
The python execution file does not pay attention to the suffix name but only the content, (.py is used to distinguish)
Variables and Data Types
Data types are roughly divided into three categories (dictionary list tuple and so on later), int (integer), string (string), boolean
int:
On a 32-bit machine, the number of integers is 32 bits, and the value range is -2**31~2**31-1, that is, -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits, and the value range is -2**63~2**63-1, that is, -9223372036854775808~9223372036854775807
str: the contents of single or double quotes, or multiple quotes (for multi-line strings)
Strings can be simply added and multiplied
boolean:
TRUE and FALSE are mainly used to judge right or wrong (whether a statement is true)
name = "that kid" #Python does not need to define the data type name data type is string
age = 23 #age's data type is int
coding
Use utf-8 encoding
Notes
General use#
# name = "that boy" comment a single line
PEP8 check:
Comment on a single line: # +1 space + comment content
Comments following the code: 2 spaces+#+1 space+comment content
One blank line before the function, two blank lines before the class
Some words have always been underlined, you can right-click "Spelling" Typo: Save "XXX" to dictionary
""" """ or ''' '''Three double quotes or three single quotes Comment on multiple lines
‘’’
name = "that kid"
age = 23
‘’’
In addition to being able to comment multiple lines, you can also print multiple lines
but =
‘’’
name = "that kid"
age = 23
‘’’
What print(mas) outputs is
enter
name = input(“name:”)
age = input(“age:”)
Note: The data type of input input is string by default, if you need to change it to integer
age = int(input("age:")) # data type is coerced to integer
Combining variables with strings
name = input ("name:")
age = int(input("age:"))
job = input("job:")
infor = '''info of %s
name:%s
age:%d
job:% s
'''%(name,name,age,job) #This % is to associate the previous string with the variable in the brackets
# A %s is a placeholder corresponding to the value in the brackets, s represents a string (character String), it can also be %d (specified as int type), the corresponding variable should also be int, and the variable of %s can be int.
age%d emphasizes that the input of age must be a number corresponding to the previous data type
infor2 = '''info of {0}
name:{0}
age:{1}
job:{2}
'''.format(name,age,job)
infor3 = '''info of {Name}
name:{Name}
age:{Age}
job:{Job}
'''.format(Name=name,Age=age,Job=job)
operator:
arithmetic operations
The following hypothetical variables: a=10, b=20
comparison operation
The following hypothetical variables: a=10, b=20
assignment operator
The following hypothetical variables: a=10, b=20
logic operation
call the standard library
#import getpass
#!/usr/bin/env python
import getpass
name = input("name:")
password = getpass.getpass("password:")
print(name,password)
if statement
#!/usr/bin/env python
import getpass
_name = "naxiaozi"
_password = "123"
name = input("name:")
password = getpass.getpass("password:")
if name == _name and _password == password:
print("welcome")
else:
print("name or password is wrong")
if:
else:
if:
elif:
Note: Python uses indentation to reflect the relationship between statements.
If the condition is satisfied, the statement should be indented. The else is side by side with if, so there is no need to indent.
while
while True:
while condition:
Exercise 1: Age Guessing Game (10 minutes)
Require:
- The user is allowed to try at most 3 times. If the user fails to guess correctly after 3 attempts, he will exit directly. If he guesses correctly, print the congratulatory message and exit.
#!/usr/bin/env python
count = 0
old_of_boy = 23
while count < 3:
count +=1
guss_old = int(input("guss old:"))
if old_of_boy == guss_old :
print("yes,you get it")
break
elif old_of_boy > guss_old:
print("think bigger")
elif old_of_boy < guss_old:
print("think smaller")
else :
print("you have tried too many time ")
while的else只在中间不执行break才执行。
break to end the current loop
continue to end the current loop and proceed to the next loop
for loop
for i in range(10): #rang from 1 to 10 plus one each time, range (1:10:2) from 1 to 10 plus 2 each time
print("loop:",i)
Exercise 2: Age Guessing Game Upgrade (10 minutes)
Require:
- Allow users to try up to 3 times
- After every 3 attempts, if the user has not guessed correctly, ask the user if he wants to continue playing. If the answer is Y or y, continue to let him guess 3 times, and so on. If the answer is N or n, exit the program
- How to guess right, just quit
#!/usr/bin/env python
count = 0
old_of_boy = 23
while count < 3:
count +=1
guss_old = int(input("guss old:"))
if old_of_boy == guss_old :
print("yes,you get it")
break
elif old_of_boy > guss_old:
print("think bigger")
elif old_of_boy < guss_old:
print("think smaller")
if count == 3:
print("是否还想猜:")
k = input("想就输入‘是’,否则输入‘否’")
if k == '是':
count = 0
elif k == 'N'or k == 'n':
exit()
else :
print("you have tried too many time ")