zoukankan      html  css  js  c++  java
  • py-day2-sys模块、os模块、运算符、列表、字典

    一、sys 模块

    import sys

    print (sys.path)#打印环境变量

    print(sys.aegv) #打印脚本的名字相对路径

    print(sys.aegv)1 2 3 4

    print(sys.aegv[2])

    二、os 模块

    os.system("系统命令") #调用系统命令,不保存结果,只显示在屏幕上。

    变量名=os.system("系统命令")

    print(变量名)

    os.popen ("系统命令").read #存到内存的临时地方,用read去取。

    变量名=os.popen ("系统命令").read

    print(变量名)

    os.mkdir("new_dir") #创建目录

    三、字符串格式化输出

     name = "liudong"

    print "i am %s " % name

    输出:i am liudong

    PS:字符串是%s 整数是%d 浮点数是%f

    四、列表

    创建列表:

    name_list = ['liudong','cuina','liuyansheng']

    name_list = list(['liudong','cuina','liuyansheng'])

    简单讲解例

    names = ["liu","dong","cui","yan","na"]
    namess = ["liu","dong",["cui","yan"],"na"]
    #names.append("liudong")#在后面排列有序添加
    #names.insert(1,"liudong")#1前面插入
    #names.clear()#清空
    #names.reverse()#整个列表翻转
    #names.extend(另外一个列表)#两个列表合并,列表还存在
    #del 列表#删除列表
    
    #print(names[:2])#切片 “顾头不顾尾”
    #print(names[-1:])#切片最后一个
    
    
    '''name2 = names.copy()#将names复制一份给name2
    names[0]="刘东"#0坐标被改成中文
    print(names)
    print(name2)#name2没改
    '''
    
    '''name2 = copy.copy(namess)  name3=namess[:]  name4=list(namess) #浅copy
    #print(name2)
    print(namess)
    name2[2][0]="CUI"#改列表里的列表,namess不会改
    print(name2)
    '''
    
    '''name2 = copy.deepcopy(names)#深copy,上面插入着copy模块
    print(name2)
    '''
    
    '''for i in names:
        print(i)  #列表循环打印
    '''
    
    '''print(names[0:-1:2])#间隔输出
    print(names[::2])#0和-1可以省略
    '''
    '''name = "my name is liudong sss"
    #print(name.capitalize())#首字母大写
    #print(name.center(50,"-"))#左右50个“-”号
    #print(name.count("s"))#包含多少个“s”
    #print(name.endswith("ss"))#包含为true
    #print(name[name.find("name"):])#切片
    #print('整数'.isdigit())#是不是一个整数
    #print('数字'.isnumeric())#是不是只有数字
    #print('My Name'.istitle())#判断首字母是不是大写
    #print('LIU DONG'.isupper())#是不是全是大写
    #print('+'.join(['1','2','3','4']))#加一起
    #print(name.ljust(50,'*'))#后面加*
    #print(name.rjust(50,'-'))#前面加-
    #print('liu ddong'.replace('d','D',1))#只换一个大写
    #print('liu l dong'.rfind('l'))#查最后一个“小写L”排在第几个
    #print('1+2+3+4'.split('+'))#只提取数字,去掉+号
    #print('Liu Dong'.swapcase())#大小写去反
    '''
    View Code

    五、元组(不可变的列表)

    创建元组:

    ages = (11,22,33,44,55)

    ages = tuple(11,22,33,44,55)

    六、字典(无序)

    #字典
    #字典是无序的
    #info = {'1':"liudong",'2':"tenglan wu",'3':"hehe",}
    #print(info)
    #print(info["3"])#通过key来取
    #info["2"]="武藤兰"
    #print(info)
    #info["4"]="苍井空"#没有就添加
    #del info["2"]#删除
    #print(info.get('9'))#有就返回 ,没有就返回空
    #print('2'in info)#有就返回true,没有就返回false
    
    '''#创建个薪字典
    b = {'101':"liudong"}
    info.update(b)
    '''#字典info和b合并
    
    #c = dict.fromkeys([6,7,8],"test")#初始化一个新字典
    #print(c)
    
    #print(info)
    
    '''字典的循环
    #for i in info:
    #    print(i,info[i]) 最基本的循环
    '''
    
    
    #p=str.maketrans("abcdef",'123456')
    #print("liudong".translate(p))
    View Code

    七、数据运算

    数字运算:

    比较运算:

    赋值运算:

    逻辑运算:

    成员运算:

    身份运算:

    位运算:

    讲解实例:

    #!/usr/bin/python
    
    a = 60            # 60 = 0011 1100
    b = 13            # 13 = 0000 1101
    c = 0
      
    c = a & b;        # 12 = 0000 1100
    print "Line 1 - Value of c is ", c
      
    c = a | b;        # 61 = 0011 1101
    print "Line 2 - Value of c is ", c
      
    c = a ^ b;        # 49 = 0011 0001
    print "Line 3 - Value of c is ", c
    View Code

    运算符优先级:

    三元运算例:

     

    作者: 东邪西毒
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,负责保留追究法律责任的权利。

  • 相关阅读:
    Codesys——限定符的使用方法[来自Codesys的Help]
    分页后台
    多条件查询判断
    添加跟反射
    试图页面分页首选
    动态游标存储过程 表名为参数
    索引器
    泛型 Generics
    Win10 锁屏图片 路径
    SQL2014 error 40 ( Microsoft SQL Server, 错误2)
  • 原文地址:https://www.cnblogs.com/liuyansheng/p/5721547.html
Copyright © 2011-2022 走看看