Python标识符 保留字 变量 数据类型 类型转换 注释

作者猫斯基 2020-01-03 293 人读过

最新文章

查看python保留字

# Author:猫斯基
# Website:www.maosiji.com
import keyword
print( keyword.kwlist )

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

变量

# Author:猫斯基
# Website:www.maosiji.com
name = '猫斯基'
print( id(name) ) # 2180602211664
print( type(name) ) # <class 'str'>
print( name ) # 猫斯基

数据类型

整数类型 int,默认十进制。二进制以0b开头,八进制以0o开头,十六进制以0x开头。

浮点数类型 float

布尔型 bool

字符串类型 str

# Author:猫斯基
# Website:www.maosiji.com
name = '猫斯基'
print( type(name) ) # <class 'str'>
print( type(55) ) # <class 'int'>
print( type(3.16) ) # <class 'float'>
print( type(False) ) # <class 'bool'>

浮点型

# Author:猫斯基
# Website:www.maosiji.com
print( 1.1 + 2.1 ) # 3.2
print( 1.1 + 2.2 ) # 3.3000000000000003
# 导入模块,使得计算精确
from decimal import Decimal
print( Decimal('1.1') + Decimal('2.2') ) # 3.3

Python字符串可以使用单引号''、双引号""、三引号来定义''' ''' """ """

单引号、双引号定义的字符串必须在一行。

三引号订阅的字符串可以是多行。

# Author:猫斯基
# Website:www.maosiji.com
a = '猫斯基';
b = "猫斯基";
c = '''猫斯基'''
d = """猫斯基"""
print(id(a)); # 1454816439632
print(id(b))  # 1454816439632
print(id(c))  # 1454816439632
print(id(d))  # 1454816439632

类型转换

# Author:猫斯基
# Website:www.maosiji.com
# 字符串转换成int型
s = int('123')
print(type(s), s) # <class 'int'> 123
# int型转成字符串
s = str(900)
print(type(s), s) # <class 'str'> 900
# str转成浮点型
s = float('99.99')
print(type(s), s) # <class 'float'> 99.99
# 浮点型转成字符串
s = str(99.999)
print(type(s), s) # <class 'str'> 99.999
# int转成float
s = int(99.99)
print(type(s), s) # <class 'int'> 99
# float转成int
s = float(99)
print(type(s), s) # <class 'float'> 99.0

注释

单行注释 #开头,直到换行结束

多行注释 python没有多行注释标记,通常将一对三引号(''' ''' """ """)之间的代码称为多行注释

中文编码声明注释 在文件开头加上中文声明注释,以指定源码文件的编码格式

#coding:gbk
# Author:猫斯基
# Website:www.maosiji.com
# 单行注释
'''
多行注释
'''
"""
多行注释
"""

本文由 猫斯基 原创发布。

著作权均归用户本人所有。独家文章转载,请联系本站管理员。获得授权后,须注明本文地址! 本文地址:https://www.maosiji.com/2647.html

关注我们

站长

WordPress迷