python是一种解释型、面向对象、带有动态语言的高级编程语言,是当今的主流语言之一。python以简洁的语法结构、规范的代码的代码格式、强大而稳定的标准库以及对第三方库的良好兼容得以广泛应用。
python的六大数据结构类型
**数字(number)**包括int、long、complex、float、bool
**字符串(String)**例如:“python”、‘python’
**列表(list)**例如:[1,2,3,4]、[5,6,7]、[8,9]
**字典(Dictionary)**例如:{1:“study”,2:“python”}
**元组(Tuple)**例如(1,“shuang”,2)
**集合(Set)**例如{‘p’,‘y’,‘t’,‘h’,‘o’,‘n’}
python关键字
用以下代码可以在pycharm查看当前环境下的关键字
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(_builtins_)
[‘class’, ‘contains’, ‘delattr’, ‘delitem’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘getitem’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘iter’, ‘le’, ‘len’, ‘lt’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘setitem’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘clear’, ‘copy’, ‘fromkeys’, ‘get’, ‘items’, ‘keys’, ‘pop’, ‘popitem’, ‘setdefault’, ‘update’, ‘values’]
python变量的作用域
在python程序中变量并不是在哪个位置都可以访问的,变量定义的位置决定了变量的访问权限,具体变量的作用域分为:全局作用域、局部作用域、闭包函数的嵌套作用域、内置作用域。
全局作用域
例
def fun():
num1 = 2
print("num1为", num1)
num1 = 1
fun()
print("初始num1", num1)
在代码中改变全局全局变量的值,那么有效范围仅限于函数的内部,函数外全局变量的值未改变。
注意空行和缩进,python不再使用{}来区分代码模块而是用缩进,同一代码块中的语句必须包含相同的缩进空格数如上例
def fun():
num1 = 2
print("num1为", num1)
num1 = 1
fun()
print("初始num1", num1)
没有合理的使用缩进会导致编译错误。