注释
单行注释 “#”后跟随内容
print('hi') # 此为单行注释内容
多行注释 ”’注释内容 ”’ 注释内容两侧为三个单引号
'''
此
为
注
释
'''
print('hi')
字符串和整型转换
type()函数用于查看数据类型
str()函数可以把数据转换为字符串类型
int()函数可以吧数据转换为整型
a = 10
print(a,type(a)) # 输出结果 10 <class 'int'>
b=str(a)
print(b,type(b)) # 输出结果 10 <class 'str'>
c = int(b)
print(c,type(c)) # 输出结果 10 <class 'int'>
分支语句 if… elif … else
if… elif … else 中间是不可以添加其他东西
加入其他东西,报语法错误
SyntaxError: invalid syntax
小游戏 猜年龄
input() #读取输入信息
input()函数读取结果为字符串
init_age = 50
# name = input("your name :")
age = int(input("your age :")) # 强制转化
if age > init_age:
print('猜测过大')
elif age < init_age:
print("猜测年龄过小")
# print() #此语句就相当于在if... elif ... else 中间添加其他东西,取消注释会报 SyntaxError: invalid syntax
else :
print('猜测正确')
缩进
Python中最特殊之处为了简洁省略了{},但是必须区分执行代码段所以有了缩进成为了必选项
没有缩进报的错误
IndentationError: expected an indented block
缩进级别必须一致
tab 键不等于4个空格 如果同一等级缩进中一条语句用的是tab 一条语句用的是4个空格会报下面错误
“`
TabError: inconsistent use of tabs and spaces in indentation
end!