博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python day2
阅读量:7144 次
发布时间:2019-06-29

本文共 6260 字,大约阅读时间需要 20 分钟。

1、运算符

结果是值:

                算数运算     a  =  10 * 10

                赋值运算     a  =  a + 1    a+=1

结果是布尔值:

                比较运算     a  =  1 > 5

                逻辑运算     a = 1>6 or 1==1

                成员运算     name  =  “b”  in  "abc"        

user = "alex"pwd = "123"v = user == 'alex' and pwd == "123" or 1 == 2 and pwd == "99854" and 1 == 2print(v)

               补充:

                       有括号先计算括号内

                       执行顺序:

                                      从前到后     结果

                                        True     or   ==>  True

                                        True    and   ==>  继续走

                                        False   or   ==>    继续走

                                        False   and   ==>  False                                         

2、基本数据类型

                    数字     int    ,  所有功能,都放在int里

                     - int    

                             将字符串转换为数字  

a = "123"                              print(type(a),a)                              b = int(a)                              print(type(b),b)
num = "0011"                              v = int(num, base=16)                              print(v)                              把这个字符串按照16进制转换成10进制,没有base把这个字符直接转化成10进制

                     - bit_lenght 

                                 # 当前数字的二进制,至少用n位表示

                                 r = age.bit_length()

                     字符串      str                          

# test = "aLex"            # 首字母大写            # v = test.capitalize()            # print(v)
# 所有变小写,casefold更牛逼,很多未知的对相应变小写            # v1 = test.casefold()            # print(v1)            # v2 = test.lower()            # print(v2)
# 去字符串中寻找,寻找子序列的出现次数,可以设置起始位置结束位置            # test = "aLexalexr"            # v = test.count('ex')            # print(v)            # test = "aLexalexr"            # v = test.count('ex',5,6)        #可以设置起始位置结束位置            # print(v)
# test = "alex"            # v = test.endswith('ex')          # 以什么什么结尾            # v = test.startswith('ex')        # 以什么什么开始            # print(v)
***         # 从开始往后找,找到第一个之后,获取其未知                        # test = "alexalex"            # 未找到 -1            # v = test.find('ex')            # v = test.find('ex',b,8)  指定的位置 > 或 >=            # print(v)

              # index找不到,报错 忽略

              # test = "alexalex"
              # v = test.index('8')
              # print(v)

# 格式化,将一个字符串中的占位符替换为指定的值            # test = 'i am {name}, age {a}'            # print(test)            # v = test.format(name='alex',a=19)            # print(v)            # test = 'i am {0}, age {1}'            # print(test)            # v = test.format('alex',19)            # print(v)
# 格式化,传入的值 {"name": 'alex', "a": 19}            # test = 'i am {name}, age {a}'            # v1 = test.format(name='df',a=10)            # v2 = test.format_map({"name": 'alex', "a": 19})
# 字符串中是否只包含 字母和数字            # test = "123"            # v = test.isalnum()            # print(v)
#是否是字母,汉字            #test="asdf"            #v = test.isalpha()            #print(v)
#当前输入是否是数字            test = "123②二"            v1 = test.isdecimal()                              v2 = test.isdigit()                    #可以支持殊数字            v3 = test.isnumeric()                  #可以支持汉字的数字             print(v1,v2,v3)
#字母,数字,下划线 : 标识符 def class            a = "_123"            v = a.isidentifier()            print(v)
#是否为小写           test = "Wsd"           v = test.islower()           print(v)
#是否存在不可显示的字符          #\t  制表符          #\n  换行符          test = "oiuas\tjdjs"          v = test.isprintable()          print(v)
#判断是否全部是空格          test = "      "          v = test.isspace()          print(v)
#判断是否为标题         test = "Return True if S is a titlecased string and there is at least one"         v1 = test.istitle()                #判断是否为标题         print(v1)         v2 = test.title()                  #变成标题         print(v2)         v3 = v2.istitle()                  #再次判断         print(v3)
***  #将字符串中的每一个元素按照指定分隔符进行拼接         test = "你是风儿我是沙"         print(test)         v1 = " ".join(test)         print(v1)         v2 = "❤".join(test)         print(v2)
# test = "12345678\t9"            # v = test.expandtabs(6)          #断距6,6个一组            # print(v,len(v))            #test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"            #v = test.expandtabs(20)            #print(v)
# 设置宽度,并将内容居中            # 20 代指总长度            # *  空白未知填充,一个字符,可有可无            # v = test.center(20,"中")            # print(v)            #把内容放左边,右边填充            #test = "alex"            #v = test.ljust(20,"*")            #print(v)            #把内容放在右边,左边填充            #test1 = "alex"            #v1 = test1.rjust(20,"*")            #print(v1)
#判断是否全部是大小写和转换为大小写            test = "Alex"            v1 = test.islower()    # 判断是否是小写            v2 = test.lower()       #转换为小写            print(v1,v2)            v3 = test.isupper()   #判断是否是大写            v4 = test.upper()      #转换为大写            print(v3,v4)                                   

                         

#test = " alex "#test.lstrip()#test.rstrip()#test.strip()#去除左右空白#v = test.lstrip()#v = test.rstrip()#v = test.strip()#print(v)#test = "\nalex"#v = test.lstrip()#v = test.rstrip()#v = test.strip()#print(v)#去除\n \t#test = "xalex"#v = test.lstrip('xa')#v = test.rstrip('9aexa')#v = test.strip('xa')#print(v)#移除指定字符串#有限最多匹配
#v = "abcdefghigklmn"#m = str.maketrans("abcde", "12345")#new_v = v.translate(m)#print(new_v)#替换
#test = "testasdsddfg"#v = test.partition('s')      #只能分成3个元素#print(v)#v1 = test.rpartition('s')    #从右边分#print(v1)#v = test.split('s',2)         #可以分成指定个元素,但没有分隔符#v = test.rsplit()#print(v)
***#分割,只能根据,True, false: 是否保留换行#test = "asdfg\nfdjffjdfjkf\nfggjhfjgjgj"#v = test.splitlines(True)#print(v)
#判断以XXX开头,以XXX结尾#test = "backend 1.1.1.1"#v = test.startswith('a')#print(v)#test.endswith('a')
#大小写转换#test = "alex"#v = test.swapcase()#print(v)
#替换test = "alexalexalex"#v = test.replace("ex","bbb")#print(v)v = test.replace("ex","bbb",2)print(v)

 

 ##############必须会的7个基本魔法################

jion  split    find   upper  lower  replace

##############灰魔法############################

#索引,下标,获取字符串中的某一字符#test = "alex"#v = test[3]#print(v)
#切片#v = test[0:1]    # >=0 , <1#print(v)
#python3,len获取当前字符串中由几个字符组成#v = len(test)#print(v)#li = [11,22,33,44,55,66,"asdf"]      按逗号计算数量#v = len(li)#print(v)#一个字一个字的拿出来test = "妹子有种冲我来"#index = 0#while index < len(test):#    v = test[index]#    print(v)#    index += 1#print "======"#for循环for i in test:    print(i)
#帮助创建连续的数字,也可以设置步长来指定不连续的数字#v = range(0,100)#v = range(0,100,5)#for item in v:#    print(item)
#将文字对应的索引打印出来#test = input(">>>")#print(test)#l = len(test)#print(l)#r = range(0,l)#for item in r:#    print(item,test[item])test = input(">>>")for item in range(0,len(test)):    print(item, test[item])

 

 ###################1个深灰魔法#################

#字符串一旦创建,不可修改#一旦修改或者拼接,都会造成重新生成字符串#name = "xiaoming"#age = "18"#info = name + age#print(info)

 

转载于:https://www.cnblogs.com/liujinlei/p/9101430.html

你可能感兴趣的文章
[Unity插件]LitJson杂谈
查看>>
调节effective_io_concurrenc优化PostgreSQL bitmap index scan性能
查看>>
MySQL体系结构笔记
查看>>
linux常用syslog日志知识
查看>>
VMware试验问题总结
查看>>
说说Micorsoft集群原理
查看>>
Android开发者指南(18) —— Web Apps Overview
查看>>
Oracle流程控制语句
查看>>
仿CSDN客户端首页(二)----拖拽排序Tabs的实现
查看>>
openstack 虚拟机导出
查看>>
《Flash建站技术》系列6-LoadVars数据提交与表单处理
查看>>
Service Mesh:什么是Sidecar模式
查看>>
python string
查看>>
纠结:决策有依据、局限和方法,也有后果需要承担
查看>>
小米纪录片《一团火》上映,这团火烧到你了吗?
查看>>
一篇好的BUG报告是如何炼成的
查看>>
要做好性能测试,该掌握些什么?
查看>>
今天配置java + selenium 3.0出了很多问题,记录如下
查看>>
xen虚拟化里常用的一些配置
查看>>
在用vi编辑文件时遇到“Terminal too wide”的提示
查看>>