Python开发Day02

一、运算符

  1. 算数运算符:

  2. 比较运算符:

  3. 逻辑运算符:

     

  4. 成员运算符:

     

  5. 身份运算符:

二、Python的基本数据类型

  1. 数字(int)

     1 a=1
     2 b=2
     3 c=3
     4 print(a.bit_length())
     5 print(b.bit_length())
     6 print(c.bit_length())                    #查看当前数字二进制最短可以占几位
     7 执行结果:
     8 1
     9 2
    10 2
    1 a=1
    2 b=2
    3 print(a.__add__(b))      #数字加法
    4 执行结果:
    5 3

     

  2. 字符串(str)

    1 a='hello work'   #定义变量a
    2 print(a.capitalize())        #将首字母大写
    3 执行结果:
    4 Hello work
    1 a='hello work'      #定义变量a
    2 print(a.center(20,'-'))        #将字符串居中,空白处自定义填充
    3 
    4 执行结果:
    5 -----hello work-----
    1 a='hello work'    #定义变量
    2 print(a.count('h',1,10))    #在变量a的字符串中从1-10的索引中找h并输出h出现过几次
    3 print(a.count('h',0,10))
    4 执行结果:
    5 0
    6 1
    1 a='hello work'
    2 print(a.endswith('k',1,3))
    3 print(a.endswith('k',3,10))      #判断变量索引3到10位置的字符串是不是以k结尾,如果是输出True,否则False
    4 
    5 执行结果:
    6 False
    7 True
    1 a='hello work\t1'
    2 print(a)           #输出变量a
    3 print(a.expandtabs(20))            #将变量a中的tab转换成20个空格,可以不填,默认8个空格。
    4 
    5 执行结果:
    6 hello work    1
    7 hello work          1
    1 a='hello {0}'
    2 print(a.format('Wu'))        #在a变量中有{0}将定义给{0}根据顺序继续向后替换,可以设置多个。
    3 执行结果:
    4 hello Wu
    1 a='hello word'
    2 print(a.index('o'))        #与find一样,但是找不到会报错。
    3 执行结果:
    4 4
    1 a='hello'
    2 print(a.isalpha())     #判断变量是不是字符串,是输出True,否则False
    3 执行结果:
    4 True
    1 a='hello'
    2 print(a.isdigit())     #判断变量是不是数字,是就输出True,否则False
    3 执行结果:
    4 False
    1 a='Hello Word'
    2 print(a.istitle())     #判断变量a是不是标题。
    3 b='Hello word'
    4 print(b.istitle())
    5 执行结果:
    6 True
    7 False
    1 a=' s'
    2 print(a.isspace())     #判断变量是不是空格,是就输出True,否则False
    3 执行结果:
    4 False
    1 a='Hello word'
    2 print(a.isupper())     #判断变量a是不是全是大写。
    3 b='HELLO'
    4 print(b.isupper())
    5 执行结果:
    6 False
    7 True
    1 a=['hello','word']
    2 print('_'.join(a))          #使用_将内容链接起来
    3 执行结果:
    4 hello_word
    1 a='hello word'
    2 print(a.upper())          #将字符串小写改成大写
    3 执行结果:
    4 HELLO WORD
    1     a='hello word'
    2 print(a.partition('w'))        #在字符串w处分割,自动添加到元组
    3 执行结果:
    4 ('hello ', 'w', 'ord')
    1 a='        hello Word         '
    2 print(a.strip())          #将字符串前后的空格去掉
    3 print(a.rstrip())          #将字符串后的空格去掉
    4 print(a.lstrip())          #将字符串前的空格去掉
    5 执行结果:
    6 hello Word
    7         hello Word
    8 hello Word 
    1 a='hello Word'
    2 print(a.lower())          #将字符串大写改成小写
    3 执行结果:
    4 hello word
    1 a='hello word'
    2 print(a.replace('l','qq',1))      #在字符串中找到l替换成qq,只替换一次,如果没有定义替换几次默认全部替换。
    3 执行结果:
    4 heqqlo word
    1 a='hello work\'
    2 print(a.find('l'))        #在a变量中找到l的位置并显示,没有的值显示-1,有重复的只显示第一个。他是从头到尾的找。
    3 print(a[2:4])             #在这里使用的是切片的方法取出对应位置的值
    4 print(a.find('p'))
    5 执行结果:
    6 2
    7 ll
    8 -1
     1 a='hello word'
     2 print(a.split('l',1))      #在字符串中找到l进行分割,如果什么都不填则以空格分隔,只分割一次,可以不填,默认全部分割,最后以列表的形式出现。
     3 print(a.split('l')) 4 print(a.split())
     5 print(a.rsplit('l',1))      #在字符串后面开始找,找到后只分隔一次。
     6 执行结果:
     7 ['he', '', 'o word']
     8 ['hello', 'word']
     9 ['he', 'lo wolrd']
    10 ['hello wo', 'rd']
    a='hello WORD'
    print(a.swapcase())      #将字符串大写变小写,小写变大写。
    执行结果:
    HELLO word
     1 a='hello work'        #在他的里面有空格
     2 b='hellowork'
     3 c='sdfadsf234234'
     4 d='123123'
     5 print(a.isalnum())       #判断字符串中是否有英文或数字。
     6 print(b.isalnum())
     7 print(c.isalnum())
     8 print(d.isalnum())
     9 执行结果:
    10 False
    11 True
    12 True
    13 True
  3. 列表(list)

    1 a=['hello','word']
    2 a.append('hello')#在列表结尾处中添加元素。
    3 print(a)
    4 执行结果:
    5 ['hello', 'word', 'hello']
    1 a=['hello','word']
    2 print(a.index('hello'))    #在列表中找到hello的索引位置
    3 执行结果:
    4 0
    1 a=['hello','word','hello']
    2 print(a.count('hello'))    #判断hello在列表中出现过几次。
    3 执行结果:
    4 2
    1 a=['hello','word']
    2 print(a[0])     #取出索引对应的值
    3 执行结果:
    4 hello
    1 a=['hello','word']
    2 b=a.pop(0)   #删除索引为0的元素,同时删除的值可以赋值给变量,默认删除最后一个。
    3 print(a)
    4 print(b)
    5 执行结果:
    6 ['word']
    7 hello
    1 a=['hello','word']
    2 print(a[1]=="Wu")  #判断列表中对应的索引位置的值为Wu,是的话输出True,否则False
    3 执行结果:
    4 False
    1 a=['hello','word','hello']
    2 a.remove('hello')    #删除列表中为hello的元素,只删除一次,从左到右。
    3 print(a)
    4 执行结果:
    5 ['word', 'hello']
    1 a=['hello','word']
    2 a[0]="Wu"       #将列表中索引为0的元素改为Wu
    3 print(a)
    4 执行结果:
    5 ['Wu', 'word']
    1 a=['hello','word']
    2 a.insert(1,'Wu')                #在指定的索引位置插入Wu这个元素
    3 print(a)
    4 执行结果:
    5 ['hello', 'Wu', 'word']
    1 a=['hello','word']
    2 b=['wu','qi']
    3 a.extend(b)                #把b的列表的元素追加到a的列表中
    4 print(a)
    5 执行结果:
    6 ['hello', 'word', 'wu', 'qi']
    1 a=['hello','word','a','b']
    2 a.sort()     #将列表元素排序
    3 print(a)
    4 执行结果:
    5 ['a', 'b', 'hello', 'word']
    1 a=['hello','word','a','b']
    2 a.reverse()     #将列表元素反转
    3 print(a)
    4 执行结果:
    5 ['b', 'a', 'word', 'hello']
    1 a=['hello','word','a','b']
    2 del a[1]        #删除列表索引为1的元素
    3 print(a)
    4 执行结果:
    5 ['hello', 'a', 'b']

     

    1 a=['hello','word','a','b']
    2 a.clear()    #清空列表
    3 print(a)
  4. 元组(tuple)

    1 a=()    #创建一个空的元组,元组的元素是不可变的也不允许添加,一旦初始化便不可以被修改。
    1 a=('a','b','c','a')
    2 print(a.count('a'))       #统计a元素在元组内出现的次数
    3 执行结果:
    4 2
    a=('a','b','c','a')
    print(a.index('a',0,2))    #查找a元素出现的索引位置,从0到2的元素。,如果没有会报错
    print(a.index('a',1))
    执行结果:
    0
    3
  5. 字典(dict)

    1 a={'wu':'yong','hello':'work'}        #建立一个字典,在字典中只有Kye和value,并且Key是不可以相同的
    1 a={'wu':'yong','hello':'work'}
    2 a['wu']='qi'            #将字典中key为wu的value改成qi,如果没有这个key,就创建一条新纪录
    3 print(a)
    4 执行结果:
    5 {'hello': 'work', 'wu': 'qi'}
    1 a={'wu':'yong','hello':'work'}
    2 print(a['wu'])              #取出字典wu对应的value,如果key不存在会报错。
    3 执行结果:
    4 yong
    1 a={'wu':'yong','hello':'work'}
    2 a.pop('hello')          #删除字典中key为hello的数据,也可以使用del a['hello']。没有会报错。
    3 print(a)
    4 执行结果:
    5 {'wu': 'yong'}
    1 a={'wu':'yong','hello':'work'}
    2 a.popitem()                 #随机删除一条数据,dict为空时用此语法会报错
    3 print(a)
    4 执行结果:
    5 {'hello': 'work'}
    1 a={'wu':'yong','hello':'work'}
    2 print(a.keys())             #查看字典中所有的key
    3 执行结果:
    4 dict_keys(['hello', 'wu'])
    1 a={'wu':'yong','hello':'work'}
    2 print(a.values())             #查看字典中所有的value
    3 执行结果:
    4 dict_values(['yong', 'work'])
    1 a={'wu':'yong','hello':'work'}
    2 print(a.items())             #查看字典中所有的key和value,并且使用for循环以元组的形式出现。
    3 b=a.items()
    4 for i in b:
    5     print(i)
    6 执行结果:
    7 dict_items([('hello', 'work'), ('wu', 'yong')])
    8 ('hello', 'work')
    9 ('wu', 'yong')
    1 a={'wu':'yong','hello':'work'}
    2 print(a.has_key('name'))             #判断字典中是否有个叫’name’的key,此方法只适用于python2.*,在3.*之后不支持。
    3 print(a.has_key('hello'))
    4 执行结果:
    5 False
    6 True
    1 a={'wu':'yong','hello':'work'}
    2 print(a.get('wu'))             #判断字典中是否有个叫’wu’的key,如果有显示其value,否则返回No,如果不定义No,则返回None
    3 print(a.get('wu2','No'))
    4 print(a.get('wu2'))
    5 执行结果:
    6 yong
    7 No
    8 None
    1 a={'wu':'yong','hello':'work'}
    2 a.clear()             #清空字典
    3 print(a)
    4 执行结果:
    5 {}
    a={'wu':'yong','hello':'work'}
    a.setdefault('name','wu')           #找一个key为’name’的纪录,如果这个key不存在,那就创建一个叫’name’的key,并且将其value设置为’wuyongqi’,如果这个key存在,就直接返回这个key的value
    print(a)
    执行结果:
    {'wu': 'yong', 'name': 'wu', 'hello': 'work'}
  6. 小总结

    1. 字符串常用功能:

      1. 移除空白

      2. 分割

      3. 长度

      4. 索引

      5. 切片

    2. 列表中常用功能:
      1. 索引
      2. 切片
      3. 追加
      4. 删除
      5. 长度
      6. 循环
      7. 包含
    3. 元组常用功能:
      1. 索引
      2. 切片
      3. 循环
      4. 长度
      5. 包含
    4. 字典常用功能:
      1. 索引
      2. 新增
      3. 删除
      4. 键、值、键值对
      5. 循环
      6. 长度
  7. for循环:

     1 a=[1,2,3,4,5,6,7,8,9]
     2 for i in a:
     3     print(i)                #在for循环中也可以使用break、continue
     4 执行结果:
     5 1
     6 2
     7 3
     8 4
     9 5
    10 6
    11 7
    12 8
    13 9

    同时for循环还可以在字典、字符串、元组上使用,在字典中使用默认只输出key,我们可以使用 for i,k in a.items(): 来获取key和value,i为key,k为value,a是字典。

  8. enumrate:

    1 a = [11,22,33]
    2 for k,v in enumerate(a, 2):  #从数字2开始向后标记,默认从0开始
    3     print(k,v) 
    4 执行结果:
    5 2 11
    6 3 22
    7 4 33
  9. range和xrange:

    1 print range(1, 10)
    2 print range(1, 10, 2)
    3 print range(30, 0, -2)
    4 执行结果
    5 [1, 2, 3, 4, 5, 6, 7, 8, 9]
    6 [1, 3, 5, 7, 9]
    7 [30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]  

    在Python2.*中range(1,10)直接在内存中创建一个列表1-9的数字,在Python3.*之后为执行完range(1,10)只有在for循环的时候循环一次内存中创建个数字,如果想在Python2.*里使用for循环一次在内存中创建一个数字使用xrange()方法,该方法在Python3.*之后取消呗range()取代。

  10. 编码与解码

    假设我a的编码为utf-8
    a='你好'
    a.decode('utf-8') #将字节转为unicode a.encode('gbk) #将unicode转为gbk

     

  11. 总结:

    1. 元组一旦初始化后便不可更改。
    2. 字典中的key不能相同。
    3. 字典中索引只能以key来找,不和列表等其他的一样使用数字。
    4. 每一次使用一些方法的时候会出现True和False这是布尔值。
    5. 找到类的使用方法有三种:
      1. help(类名)
      2. dir(类名)
      3. type(类名)
    6. 只要可以for循环的都可以迭代
    7. 如果可以使用元组的情况下尽量不使用列表,因为元组比列表安全。
    8. 在Python3.*之后直接使用.encode便可以不需要转成unicode编码了。
  12. 练习:

    1. 元素分类
      • 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
      • 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
    2. 查找
      • 查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
            list = ["alec", " aric", "Alex", "Tony", "rain"]
            tup = ("alec", " aric", "Alex", "Tony", "rain") 
            dic = {'k1': "alex", 'k2': ' aric',  "k3": "Alex", "k4": "Tony"}
    3. 输出商品列表,用户输入序号,显示用户选中的商品
      • 商品 list = ["手机", "电脑", '鼠标垫', '游艇']
    4. 购物车
      • 功能要求:
        1. 要求用户输入总资产。
        2. 显示商品列表,让用户根据序号选择商品,加入购物车
        3. 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
        4. 附加:可充值、某商品移除购物车
          goods = [
              {"name": "电脑", "price": 1999},
              {"name": "鼠标", "price": 10},
              {"name": "游艇", "price": 20},
              {"name": "美女", "price": 998},
          ]

           

    5. 用户交互,显示省市县三级联动的选择
      dic = {
          "河北": {
              "石家庄": ["鹿泉", "藁城", "元氏"],
              "邯郸": ["永年", "涉县", "磁县"],
          },
          "北京": {
              "昌平区":["回龙观","沙河","朱辛庄"],
              "丰台区":["岳各庄","小屯村","郑常庄"]
          },
          "山西": {
              "太原":["小店区","杏花岭区","万柏林区"],
              "临汾":["尧都区","襄汾县","浮山县"]
          }
      }

       

练习解答在另一篇中出现。

 

转载于:https://www.cnblogs.com/WuYongQi/p/5446033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值