大家好,我是卷心菜。因为项目的需要,最近在学习Python的爬虫。这一篇是关于Python的基础知识点,也是学习爬虫的入门知识点!如果您看完文章有所收获,可以三连支持博主哦~,嘻嘻。
在我们的编码过程中,因为时间久、遗忘快、代码逻辑复杂等原因,我们可以适当的添加注释,来帮助自己和其他编码人员的解读。因此,学习Python的注释是非常有必要的,我们应当养成编写注释的代码习惯!
和大多数编程语言一样,Python有两种注释:单行注释和多行注释。
单行注释:以#开头,#右边的所有东西当做说明,而不是真正要执行的程序,起辅助说明作用。
多行注释:以 ‘’‘开始,并以 ‘’’ 结束,称之为多行注释。
# 单行注释,解释代码
print('我要过科目一')
'''
多行注释
不要慌,不要慌,太阳下了有月光。
'''
基本语法:变量名 = 变量值question = "你喜欢什么歌?"
message = "晴天"
img = "https://item.jd.com/10046693874903.html"
print(question)
print(message)
print(img)
运行结果:

下图中画X的就是标志符不规范的举例:

| False | None | from |
| True | and | global |
| as | assert | if |
| break | class | import |
| continue | def | in |
| del | elif | is |
| else | except | lambda |
| finally | for | nonlocal |
| not | or | pass |
| raise | return | try |
| while | with | yield |

# Number 数值类型
# int
money = 20
# float
value = 20.5
# boolean 布尔类型
gender = True
sex = False
# string 字符串
message = '我是字符串'
information = '串'
# 嵌套使用
print("'我是单引号'")
print('"我是双引号"')
运行结果:

需要注意的是,在有需要时,单引号和双引号可以嵌套使用~
# 应用场景:当爬取到多个数据的时候,可以把这些数据存储到列表中
book_list = ['爱的教育', '西游记', '三国演义']
object_list = ['斗破苍穹', 123, 3.5, True]
print(book_list)
print(object_list)
运行结果:

age_tuple = (18, 19, 20, 21)
print(age_tuple)
运行结果:

person = {
'name': '我是一棵卷心菜',
'age': 21,
'major': '计算机科学与技术'
}
print(person)

type(变量名),来查看变量存储的数据类型age = 21
print(type(age)) # int
name = '我是一棵卷心菜'
print(type(name)) # string
score = 98.5
print(type(score)) # float
gender = True
print(type(gender)) # boolean
list_type = ['hello', 'world']
print(type(list_type)) # list列表
tuple_type = (12, 13, 14,)
print(type(tuple_type)) # tuple元组
dict_type = {'name': '卷心菜', 'age': 21}
print(type(dict_type)) # dict字典
感谢阅读,一起进步,嘻嘻~