zoukankan      html  css  js  c++  java
  • python基础语法

    • 注释
    ###单行注释
    
    '''
    多行注释
    '''
    
    #TODO注释,这种注释会高亮显示
    
    • 比较
    '''
    is 比较的是地址
    == 比较的是值
    '''
    a = [1,2,3]
    b = [1,2,3]
    c = a
    
    print(a is b)  
    print(a == b)
    print(a is c)
    
    False
    True
    True
    
    • 拷贝
    import copy
    a = [1,[2,3]]
    b = a
    c = a.copy()          #浅拷贝:内层元素是直接内存地址引用,会随着原来地址的内容改变而改变
    d = copy.deepcopy(a)  #深拷贝:内层元素是新建出来的
    print("a is" + str(a), "
    b is" + str(b),"
    c is" + str(c),"
    d is"+ str(d))
    a[1][0] = "new"
    print("a is" + str(a), "
    b is" + str(b),"
    c is" + str(c),"
    d is"+ str(d))
    
    
    '''
    a is[1, [2, 3]] 
    b is[1, [2, 3]] 
    c is[1, [2, 3]] 
    d is[1, [2, 3]]
    a is[1, ['new', 3]] 
    b is[1, ['new', 3]] 
    c is[1, ['new', 3]] 
    d is[1, [2, 3]]
    '''
    
    • 运算符
    ''' 
    + 加,字符串+字符串=拼接字符串,适用列表、元组、字符串
    -  减
    *  乘,字符串*数字=多个字符串,适用列表、元组、字符串
    /  除
    // 取整除
    % 取余数
    ** 幂
    '''
    
    '''
    in         在其中     a in (a,b,c)
    not in   不在其中  anot in (b,c)
    适用于列表、元组、字符串,字典匹配key键
    '''
    
    • 三元操作符
    变量 = 值1 if(条件) else 值2
    #条件成立取值,条件不成立取值2
    
    • 格式化字符串
    name = 'lee'
    age = 30
    money = 888.888
    print("姓名%s,年龄%06d,余额%.2f" %(name,age,money))
    
    '''
    姓名lee,年龄000030,余额888.89
    '''
    '''
    %s:字符串
    %d:十进制数字,%06d代表显示不全的话,前面补0
    %f:浮点数,%.2f代表小数点后面显示两位
    %%:显示%本身
    %x:显示十六进制
    '''  
    • 逻辑操作符
    and:两边都为真才为真
    or:两边都为假才为假
    not:取反
    
    • 类型
    a = 'string'
    b = 100
    c = []
    print(type(a),type(b),type(c))
    print(isinstance(a,str))
    
    '''
    <class 'str'> <class 'int'> <class 'list'>
    True
    '''
    
    '''
    type(变量):查看变量类型
    isinstance(变量,类型):判断变量是否是该类型
    '''
    
    • 类型转换
    int(变量):转换为整数
    float(变量):转换为浮点数
    str(变量):转换为字符串
    
    #变量 = int(input(“number”))
    
    • 变量交换
    #交换a和b的值
    a,b = b,a
    
    #多元复制
    a,b = value1,value2
    
    #下划线不占内存,只能出现一次
    a,b,_,d = value1,value2,value3,value4
    
    #*_去除剩下的所有值
    a,b,*_ = value1,value2,value3,value4
    
    • 函数的定义
    #最基础的函数
    def 函数名():
        函数代码
        return 值1,值2
    a,b = 函数名()
    
    #带参数的参数,
    #*args代表传参进来的任意字符串,会被整合成元组
    #**kwargs代表传进来的键值参数,会被整合成字典
    def 函数名(*args,**kwargs)
        函数代码
    
    #有默认值的函数,定义函数时给予默认值,有默认值必须放在最后
    def 函数名(参数1,参数2=值)
        函数代码
    
    • 列表推到式
    #超过三层的循环不要用列表推导式
    #不容易排错,不能debug
    number = [x*y for x in range(10) for y in range(10) if x == y]
    print(number)
    
    '''
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    '''
    • 生成器表达式
    #不占内存
    number = (x*y for x in range(10) for y in range(10) if x == y)
    for i in number:
        print(i, end=',')
    
    '''
    0,1,4,9,16,25,36,49,64,81,
    '''
    
    • 匿名函数
    fun = lambda a,b,c:a+b+c
    print(fun(1,2,3))
    
    '''
    6
    '''
    
    • if语句
    if 判断条件1:
        执行语句1……
    elif 判断条件2:
        执行语句2……
    elif 判断条件3:
        执行语句3……
    else:
        执行语句4……
    
    • while循环
    #!/usr/bin/python
     
    count = 0
    while (count < 9):
       print 'The count is:', count
       count = count + 1
     
    print "Good bye!"
    
    ------------------
    
    while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:
    # continue 和 break 用法
     
    i = 1
    while i < 10:   
        i += 1
        if i%2 > 0:     # 非双数时跳过输出
            continue
        print i         # 输出双数2、4、6、8、10
     
    i = 1
    while 1:            # 循环条件为1必定成立
        print i         # 输出1~10
        i += 1
        if i > 10:     # 当i大于10时跳出循环
            break
    
    
    ----------------
    在 python 中,while … else 在循环条件为 false 时执行 else 语句块:
    #!/usr/bin/python
     
    count = 0
    while count < 5:
       print count, " is  less than 5"
       count = count + 1
    else:
       print count, " is not less than 5"
    
    • for循环
    Python for 循环语句
    Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    for letter in 'Python':     # 第一个实例
       print '当前字母 :', letter
     
    fruits = ['banana', 'apple',  'mango']
    for fruit in fruits:        # 第二个实例
       print '当前水果 :', fruit
     
    print "Good bye!"
    
    ------------------
    通过序列索引迭代
    另外一种执行循环的遍历方式是通过索引,如下实例:
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    fruits = ['banana', 'apple',  'mango']
    for index in range(len(fruits)):
       print '当前水果 :', fruits[index]
     
    print "Good bye!"
    
    -------------------
    循环使用 else 语句
    在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    for num in range(10,20):  # 迭代 10 到 20 之间的数字
       for i in range(2,num): # 根据因子迭代
          if num%i == 0:      # 确定第一个因子
             j=num/i          # 计算第二个因子
             print '%d 等于 %d * %d' % (num,i,j)
             break            # 跳出当前循环
       else:                  # 循环的 else 部分
          print num, '是一个质数'
    
    • break语句
    Python break 语句
    Python break语句,就像在C语言中,打破了最小封闭for或while循环。
    break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。
    break语句用在while和for循环中。
    如果您使用嵌套循环,break语句将停止执行最深层的循环,并开始执行下一行代码。
    Python语言 break 语句语法:
    
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    for letter in 'Python':     # 第一个实例
       if letter == 'h':
          break
       print '当前字母 :', letter
      
    var = 10                    # 第二个实例
    while var > 0:              
       print '当前变量值 :', var
       var = var -1
       if var == 5:   # 当变量 var 等于 5 时退出循环
          break
     
    print "Good bye!"
    
    • continue语句
    Python continue 语句
    Python continue 语句跳出本次循环,而break跳出整个循环。
    continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。
    continue语句用在while和for循环中。
    Python 语言 continue 语句语法格式如下:
    
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    for letter in 'Python':     # 第一个实例
       if letter == 'h':
          continue
       print '当前字母 :', letter
     
    var = 10                    # 第二个实例
    while var > 0:              
       var = var -1
       if var == 5:
          continue
       print '当前变量值 :', var
    print "Good bye!"
    

      

      

    初学linux,每学到一点东西就写一点,如有不对的地方,恳请包涵!
  • 相关阅读:
    Component 组件props 属性设置
    template 模版制作
    vue生命周期钩子函数
    Vue.set 全局操作 结构器外面修改数据
    Vue.extend 构造器的延伸
    vue.directive自定义指令
    实战笔记
    实战-第二讲 Vue-cli搭建开发环境
    实战-第一讲 画设计图
    webpack-第03节:配置文件:入口和出口
  • 原文地址:https://www.cnblogs.com/forlive/p/12608493.html
Copyright © 2011-2022 走看看