数据类型与变量
数据类型与变量
数据类型
在计算机中,不仅要对数字进行计算,还要对图片,视频和音频等信息进行处理,Python里面提供了各种数据类型来适应满足计算机处理不同数据的需求,在Python中数据类型主要有:
- 整型
- 浮点型(小数)
- 空值(None)
- 布尔型(Bool)
- 字符串
- 列表
- 元组
- 集合
- 字典
关于列表,元组,集合以及字典放在后面讲解,先讲解前面几个较好理解的。
空值
在Python中用None来表示空值
None:表示空对象
注意点:空字符串和空列表之类的,并不是表示为None数据类型,还是表示为本身类型,可以用Python内置的type()进行数据类型的查看
Python 3.12.7 (main, Nov 8 2024, 17:55:36) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(None)
<class 'NoneType'>
>>> type("")
<class 'str'>
>>> type([])
<class 'list'>
>>>
布尔型
Bool:Python中使用True和False来表示布尔值,注意首字母大写,用来判断Python对象、返回值、表达式真假的一组特殊数据类型
注意点:None的布尔值为False,非0的数字布尔值都为True,0的布尔值为False,空字符串以及空列表之类的布尔值为False
Python 3.12.7 (main, Nov 8 2024, 17:55:36) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> bool(None)
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(-2)
True
>>> bool("")
False
>>> bool([])
False
>>>
整型
整型即整数,很简单,不多赘述。
Python 3.12.7 (main, Nov 8 2024, 17:55:36) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 2
>>> c = 3
>>> a
1
>>> b
2
>>> c
3
>>>
浮点型(小数)
浮点型就是小数
>>> d = 3.14
>>> d
3.14
>>>
关键字
关键字又被称作保留字,每种语言都有自己的一套预先保留的特殊标识符,Python也不例外,可以用Python自带的keyword模块来查看全部关键字
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', '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']
>>>
注意点:不能用关键字命名标识符(名字)。标识符:变量名,函数名,类名都是标识符,标识符说白了就是名字。
内置函数
Python有很多内置函数,可以用“dir()”来查看
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BaseExceptionGroup', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EncodingWarning', 'EnvironmentError', 'Exception', 'ExceptionGroup', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'aiter', 'all', 'anext', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
可以用help()函数来查看内置函数具体作用以及用法,例如:可以用help(len)来查看len()函数的用法。
作为渗透测试工程师,我们经常需要去阅读别人写的脚本代码,如果我们能够理解别人的代码并且根据我们的环境做一些修改,原先不能成的脚本可能就可以了,这个就是拉开了与“脚本小子”的差距。
变量
编程语言中为了能够更好的处理数据,都需要使用一些变量。在Python中不需要去声明指定变量数据类型,会自动识别。使用type(变量)查看变量数据类型。
Python 3.12.7 (main, Nov 8 2024, 17:55:36) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> type(a)
<class 'int'>
>>> b = 3.14
>>> type(b)
<class 'float'>
>>> c = "burgess"
>>> type(c)
<class 'str'>
>>>
变量的命名规则
- 不能使用关键字命名
- 不能使用数字开头
- 可以使用大小写字母,数字,下划线随意组合
命名法
-
小驼峰命名法:第一个单词首字母小写后面的大写
firstName = "burgess" -
下划线命名法:在Python里用的很多
first_name = "burgess"
浙公网安备 33010602011771号