zoukankan      html  css  js  c++  java
  • Python基础:语法基础(3)

    本篇主要介绍Python中一些基础语法,其中包括:标识符、关键字、常量、变量、表达式、语句、注释、模块和包等内容。

    1. 标识符和关键字

    1.1 标识符

      标识符是变量、常量、函数、属性、类、模块和包等指定的名称,Python语言中标识符的命名规则如下:

      (1)区分大小写,例Name与name是两个不同的标识符;

      (2)标识符首字母可以是下划线“_”或字母,但不能是数字;

      (3)标识符除首字母外的其它字符,可以是下划线“_”、字母和数字;

      (4)关键字不作为标识符;

      (5)Python内建函数不能作为标识符。

    1.2 关键字

      Python语言中有33个关键字,其中只有三个(即True、False和None)首字母大写,其它均为全部小写。

    >>> help()
    
    Welcome to Python 3.7's help utility!
    
    If this is your first time using Python, you should definitely check out
    the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.
    
    Enter the name of any module, keyword, or topic to get help on writing
    Python programs and using Python modules.  To quit this help utility and
    return to the interpreter, just type "quit".
    
    To get a list of available modules, keywords, symbols, or topics, type
    "modules", "keywords", "symbols", or "topics".  Each module also comes
    with a one-line summary of what it does; to list the modules whose name
    or summary contain a given string such as "spam", type "modules spam".
    
    help> keywords
    
    Here is a list of the Python keywords.  Enter any keyword to get more help.
    
    False               class               from                or
    None                continue            global              pass
    True                def                 if                  raise
    and                 del                 import              return
    as                  elif                in                  try
    assert              else                is                  while
    async               except              lambda              with
    await               finally             nonlocal            yield
    break               for                 not

    2. 变量和常量

    2.1 变量

      在Python中声明变量时,不需要指定数据类型。Python是动态类型语言,不会检查数据类型,在声明变量时不需要指定数据类型。

      在使用变量前需要对其赋值。没有赋值的变量是没有意义的,编译器会编译不通过。

      同一个变量可以反复赋值,而且可以是不同类型的变量。

      当不能确定变量或数据的类型时,可以使用解释器内置的函数type进行确认。

    >>> hello = 'Hello World!'
    >>> hello
    'Hello World!'
    >>> type(hello)
    <class 'str'>
    >>> hello = 100
    >>> hello
    100
    >>> type(hello)
    <class 'int'>

    2.2 常量

      Python不能从语法上定义常量,Python没有提供一个关键字使得变量不能被修改。

      Python中只能讲变量当成常量使用,只是不要修改它。

  • 相关阅读:
    《TD项目开发小结》
    感悟
    毕业两年了
    ip+port无法访问nginx问题
    问题解决之道
    调休9天的那些日子
    关于类加载器(ClassLoader)
    ios核心蓝牙之心率监控(swift)
    git(git-flow)的高效管理使用
    WKWebview加载本地图片时出现路径问题
  • 原文地址:https://www.cnblogs.com/libingql/p/10161823.html
Copyright © 2011-2022 走看看