zoukankan      html  css  js  c++  java
  • Python自学笔记2-语法

    这里介绍Python的基本语法和编程风格。

    Python的保留字(如下表,不能以这些名字给函数或变量命名)

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

    Python的代码不用括号区分结构,而是用空格。创建一块代码,需要四个空格,通常也就是按一个tab键。

    例如:

    def bar(x):
        if x == 0:
            foo()
        else:
            foobar(x)


    变量和变量命名:

    Python是动态语言,不需要在定义变量时,声明变量的类型。例如:

    1
    2
    3
    x = 2
    y = "Hello World"
    z = -3

    可以利用type()方法查看变量类型:

    print type(x)

    会输出 <class 'int'>,变量为整数型

    Python对空格有严格要求,不能用tab键,像如下一样对齐代码:

    x            = 2
    sentence     = "Hello World"
    variable_z   = -3

    可以用一个空格隔开

    2
    3
    x = 2
    sentence = "Hello World"
    variable_z = -3
    Python的注释有两种,分别是单行注释和多行注释,单行注释前面加#,多行注释要卸载三对双引号中间:

    2
    3
    x = 2
    sentence = "Hello World"
    variable_z = -3

    1
    2
    3
    # 这里是单行注释,不会执行
    x = 3
    y = 2
    2


    2
    3
    4
    5
    ''' 这里是多
    行注释 '''
    x = 3
    y = 2



    2
    3
    x = 2
    sentence = "Hello World"
    variable_z = -3
    2
    3
    4
    5
    ''' This is a
    multi-line Python
    comment. '''
    x = 3
    y = 2
  • 相关阅读:
    Linux学习笔记:常用100条命令(三)
    Linux学习笔记:常用100条命令(二)
    k8s的imagePullSecrets如何生成及使用
    Jenkins基于https的k8s配置
    ubuntu supervisor管理uwsgi+nginx
    ubuntu Django + Uwsgi + Nginx 的生产环境部署
    ubuntu 安装和配置 GitLab
    ubuntu 安装harbor仓库
    ubuntu 安装Jenkins
    php过滤html标签截取部分内容
  • 原文地址:https://www.cnblogs.com/AlexanderZhao/p/12879016.html
Copyright © 2011-2022 走看看