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中只能讲变量当成常量使用,只是不要修改它。

  • 相关阅读:
    Android与WebView的插件管理机制
    在mac下搭建Apacheserver
    “懒”也要有境地---大部分程序猿都在的地方,再不来就out了。
    codeforces Looksery Cup 2015 H Degenerate Matrix
    HDU 1247 Hat’s Words(字典树变形)
    SICP 习题 (1.38)解题总结
    scikit-learn:4.2. Feature extraction(特征提取,不是特征选择)
    iOS_高效开发之道
    亚马逊2014在线面试第一题
    通过AO连接多个EO并进行使用
  • 原文地址:https://www.cnblogs.com/libingql/p/10161823.html
Copyright © 2011-2022 走看看