当前分类:python>>正文

Python 关键字及其功能解析

来源:互联网   更新时间:2023年8月4日  

Python 笔记

一、Python关键字介绍

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

二、关键字的详细功能解析

    >>> a = 2
    >>> b = 3
    >>> if a < 3 and b > 2:
    ...     print('a小于3且b大于2')
    ...
    a小于3且b大于2
    >>> num = 3
    >>> if num == 1:
    ...     print('num的值为1')
    ... elif num == 2:
    ...     print('num的值为2')
    ... else:
    ...     print('num的值为其它值')
    ...
    num的值为其它值
    >>> i = 0
    >>> while i < 5:
    ...     i += 1
    ...     if i == 3:
    ...         break
    ...     print(i)
    ...
    1
    2
    >>> def add(x, y):
    ...     return x + y
    ...
    >>> result = add(3, 5)
    >>> print(result)
    8
    >>> x = 10
    >>> def func():
    ...     global x
    ...     x += 10
    ...
    >>> print('x的值为:', x)
    10
    >>> func()
    >>> print('x的值为:', x)
    20
    >>> def outer_func():
    ...     x = 10
    ...     def inner_func():
    ...         nonlocal x
    ...         x += 10
    ...         print('x的值为:', x)
    ...     inner_func()
    ...     print('x的值为:', x)
    ...
    >>> outer_func()
    x的值为: 20
    x的值为: 20

三、小结

本文固定链接:https://6yhj.com/leku-p-5233.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: seo