zoukankan      html  css  js  c++  java
  • Python之基础

    一、注释

    代码注释分单行和多行注释, 单行注释用#,多行注释可以用三对双引号""" """,或三对单引号''' '''。

    代码注释的原则:

    #1. 不用全部加注释,只需要在自己觉得重要或不好理解的部分加注释即可
    #2. 注释可以用中文或英文,但不要用拼音

    文件头:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    二、变量

    变量是什么:变量是为了存储程序运算过程中的一些中间结果,为了方便日后调用。

    1、声明变量:

    #!/usr/bin/env python
    # -*- coding: utf-8 –*-
    name = "wupeiqi"

    上述代码声明了一个变量,变量名为: name,变量name的值为:"wupeiqi"

    变量的作用:昵称,其代指内存里某个地址中保存的内容

    clip_image001

    1.1、变量定义的规则:

    1. 要具有描述性(简单声明这个变量的作用)
    2. 变量名只能 _ ,数字,字母组成,不可以是空格或特殊字符(#?<.,¥$*!~)
    3. 变量名的第一个字符不能是数字
    4. 不要以中文为变量名
    5. 变量名区分大小写
    6. 变量名不宜过长
    7. 以下关键字不能声明为变量名 ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

    常量 :不变的量 pie = 3.141592653....

    在py里面所有的变量都是可变的 ,所以用全部大写的变量名来代表此变量为常量。(python 并没有语法去代表常量,只不过是程序猿之间约定的)

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    name1 = "wupeiqi"
    name2 = "alex"

    clip_image001[9]

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    name1 = "wupeiqi"
    name2 = name1

    clip_image001[13]

    2、例子

    #!/usr/bin/env python
    # –*- coding: utf-8 –*-
    # @Date    : 2018-04-25 12:02:29
    # @Author  : hjc (284738209@qq.com)
    # @Link    : http://www.cnblogs.com/tootooman/
    # @Version : 1
    
    li = 'hello'
    a, *_, e = 'hello'
    print(a, e)        # h o
    
    a, *_, e = [1, 2, 3, 4, 5]
    print(a, e)        # 1 5
    
    a, *_ = [1, 2, 3, 4, 5]      # 只取第一个
    *_, e = [1, 2, 3, 4, 5]      # 只取最后一个

    3、小结

    0、变量需要先声明再调用

    1、nums_of_alex_gf = 2   下划线命名

    2、AgeOfOldboy = 56        驼峰体吗,命名

    3、5name = 2;数字不能开头

    4、特殊字符不能有,! ~ & * ......% ¥¥ 空格

  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/tootooman/p/8952133.html
Copyright © 2011-2022 走看看