Basic knowledge of Python, examples

Note: I use PyCharm. What kind of fairy tutorial is Python crawler technology ? I love this teacher so much. But who would have thought that there is such a good introductory course in the crawler tutorial.

1. Operation

Annotation

#print("hello")   #第一种方法

'''
第二种方法:三个英文单引号
第三种方法:选中内容 Ctrl+/  注释、取消注释
Ctrl加斜杠也是很多编辑器通用的注释方法
'''

run

(1) Click Run in the toolbar of the first row;
Insert picture description here
(2) Right-click the code editing area and select Run'test1'
(3) Pay attention to the lower left and upper right corners . If you create a new file that has not been run before, the two runs will not work, and the run is not the newly created file.
Insert picture description here
Insert picture description here
(4) Shortcut key Shift + F10
Description: If the shortcut key does not respond, it is not like the teacher said that your computer is not a computer used for programming. Press Fn and the shortcut key at the same time.

open a file

After File —>open or open recent, if it is the smallest unit py file, open it directly, if it is a folder, that is, the code package package, this window will appear.
This Window means to overwrite, close the current one and open a new file; New Window means to open two PyCharm at the same time.
Insert picture description here

Two, input and output

#输入:
a = input("")
b = input("请输入b:")
#输出:
print("a=",a)
print("b的值为%s。" %b)
age = 19
print("我今年%d岁"%age)   #数字用%d,字符串用%s     

s = "中国"
print("我的名字是%s,我的国籍是%s"%("小张",s))
print("我的名字是%s,我是%s人"%("小张","中国"))

print("www","baidu","com",sep=".")
print("hello",end="")    #表示不换行
print("world",end="\t")     #空格
print("python",end="\n")    #换行
print("你好\n")
print("hhh")
print("\n")        #反斜杠实现转义字符的功能
print(r"\n")      # 加r表示直接显示原始字符串,不进行转义
print("over")

# password = input("请输入密码:")
# print("您输入的密码为:",password)

Insert picture description here
Python also has an output situation that can be called cheating:

print("a"*10)
# 输出: aaaaaaaaaa

Three, type conversion (about input)

The above code, if changed to:

age = input("")
print("我今年%d岁"%age)

It will report an error:
Insert picture description here
Because the received inout input is either a number or a text, it will be assigned to the variable as a string. Solution: Change %d to %s, or perform type conversion. (But the number will not report an error with %s)

age = input("")
print(type(age))
a = int(age)
print(type(a))
#所以可以直接写  age = int(input(""))
#同理,转字符串型 就是str()
print("a=%d"%a)
print("age=%s"%age)

Insert picture description here

Four, basic sentences

Elif:

Non-zero and non-empty values ​​are True, 0 or None is False.
Python does not use curly braces, it is necessary to pay attention to spaces and indentation.

score = int(input("请输入成绩:\n"))    #score = input("")
print("成绩为:",score)
if score >= 90 and score <= 100:
    print("90-100")
elif score>=80 and score<90:
    print("80-90")
elif score >= 70 and score < 80:
    print("70-80")
elif score >= 60 and score < 70:
    print("60-70")
else:       #或 elif score <=60:
    print("<=60")

Insert picture description here

for:

for i in range(5):    #和range(0,5)一样
    print(i,end="\t")
#0	1	2	3	4

for i in range(0,16,3):    #负数也可以
    print(i,end=" ")
# 0 3 6 9 12 15

name = "chengdu"
for x in name:
    print(x,end="\t")         
#c	h	e	n	g	d	u

a = ["a","b","c","d","e"]
for i in range(len(a)):
    print(i,a[i],end="\t")     
#0 a 1 b 2 c 3 d 4 e

while:

else can be used in conjunction with while:

count = 10
while count < 5:
    print(count, "小于5")
    count += 1
else:
    print(count,"大于或等于5")
# 输出:
# 10大于或等于5
#求和
i = 0
sum = 0
n = 6
while i <= n:
    sum += i
    i += 1
print("1到%d的和为%d"%(n,sum))     
# 输出:
# 1到6的和为21

Guess you like

Origin blog.csdn.net/qq_43144103/article/details/106332606