Original link: https://www.bytecho.net/archives/2033.html
symbol
- All in English (unless the output is Chinese characters)
Notes (for easy understanding)
single line comment
-
#
multi-line comment
-
Ctrl
+/
(Add or uncomment the selected multi-line text)
variable (stores data)
- Naming: It consists of uppercase and lowercase letters, numbers and underscores. There cannot be spaces in between, and the length is not limited. It cannot start with a number. (The unusable variable name reserved by py)
Note: The size of the variable name can represent different variables and should not be confused
assignment statement (variable = expression)
a = "he" print(a) # >>he(输出为he)
Note: Programs are executed sequentially from top to bottom
a,b = "12" print(a,b) # >>he 12 a,b=b,a #交换a,b的值print(a,b) # >>12 he
Note: You cannot add spaces before the py statement
string
-
Must be enclosed in single, double or triple quotes
x = "I said:'hello'" print(x) #>>I said:'hello'
Note: The string will not contain variables and the string must be enclosed in quotes.
s = 1.83 print(s) print("I am sm tall.") # >>I am sm tall.
-
Newlines, tabs, and other special characters can be enclosed in triple double quotes
print("""多行字符可以使用以下特殊字符: \t \n """)
-
subscript (number) of the string
- Each character has a length of 1
- Use “+” to concatenate strings (characters in strings cannot be modified)
-
Use
in,not in
judge substringsa = "hello" b = "python" print("el" in a) # >>True print("th" not in b) # >>False
String to Number Conversion
-
int(x): Convert a string to an integer (round off the end)
- If x is a decimal, round off to the end
- float(x): convert to decimal
- str(x): convert to string
- eval(x): Treat it as a py expression and evaluate its value (
runtime error
may be an illegal conversion)
input Output
-
output statement print
print(1, 2, 3, end="") # end=""使其不换行print("ok") # >>1 2 3ok
-
input statement input
x = input()
(The content in the brackets can prompt the input content, the input content will be assigned to x, and input will only input one line at a time)
list
-
Lists can have 0 to any number of elements, and elements can be accessed by subscript
empty = [] # 空表list_1 = [1,2,3,4,5]
-
Use in to check if a list contains an element
matrix_a = [[1, 2, 3, 1], [2, 6, 8, 1]] print([1, 2, 3, 1] in matrix_a) # true
example:
s = input() numbers = s.split() print(int(numbers[0]) + int(numbers[1])) # 若输入8 9则输出17
(If x is a string, the value of x.split() is a list containing all strings of string x separated by spaces, tabs, and newlines)
print("ac hello".split) # >>['a','c','hello']
Exercises in this chapter
c = input("请输入一个构成三角形的字符:") print(" "+c)#为什么c前要有+,不可用,会增加空格的数量print(" "+c+c+c)#输入空格需要" " print(c+c+c+c+c)#此处的五个c相加可用c*5代替,此处的c为字符串#输入一个字符,构成已知大小的等腰三角形
s = input("请输入三个数字:").split() n1, n2, n3 = int(s[0]), int(s[1]), int(s[2]) print((n1 + n2) * n3) # 输入三个数,输出(n1 + n2) * n3 的答案
n = int(input("请输入以一个三位数:")) w1 = int(n/100) w2 = int(n%100/10) w3 = n%100%10 t = w1+w2*10+w3*100 print(t) # print(("%d%d%d")%(w1,w2,w3)) #输入三位数反向输出,此法不足:首位为0 则失效,需再补足#法二: n = input("请输入以一个三位数:")#此处为字符串,不使用split()可以达成目的#输入的字符串没有空格w1,w2,w3=n[0],n[1],n[2]#或用s=n[2]+n[1]+n[0],则print(n) print(("%s%s%s")%(w3,w2,w1))
Byte Planet Forest Stack 2022-07-23 Without permission, reprinting is strictly prohibited!
https://www.bytecho.net/archives/2033.html
This article is reprinted from: https://www.bytecho.net/archives/2033.html
This site is for inclusion only, and the copyright belongs to the original author.