zoukankan      html  css  js  c++  java
  • day2

    day2学习笔记

    第二次上课alex复习了上节课讲的东西,例如for、while循环,还分享了些经典的美剧,alex语录,看美剧不是打发时间,而是为了拓展视野,原来电影还能这样拍,废话不多说,直接进主题

    变量

    声明变量
    age = 18
    name = "huihuang"
    变量作用:保存状态(程序的运行本质是一系列的状态的变化,变量的母的就是用来保存状态,变量值得变化就构成了程序运行的不同结果 )
    例如:CF枪战,一个的生命可以表示Life=active表示存活,当满足某种条件后修改变量life=inactive表示死亡。

    二、数据类型

    大三的时候去面试java,第一个问题就是问java有几种数据类型,没回答上来,然后就没有然后了。在Python中,除了有整型、浮点型、长整形、bool值等外,还有其他语言没有的数据类型,复数型,工作中貌似用的不多,下面来具体聊聊python中常用的数据类型。

    int整数

    • 在32位系统中,整数的位数是32位,所有取值范围为-2**31~2**31-1
    • 在64位系统中,整数的位置是64位,所有取值范围为-2**63~2**63-1
    • 说实话,我到现在也没明白为啥是这样的。

    长整型

    • 长整型通俗的讲就是,可以存放更多位的整型,但由于机器内存有限,使用到的长整数值不可能无限大,平常工作中你也用不到那么长的整型数据。
    • 自从python2.2起,如果整数发生溢出,python会自动将整数数据转换为长整数,所以可以不用在长整数数据后面加字母L了。

    float(浮点型)

    • 浮点型就是带有小数的数据,形如3.14就是浮点型。

    复数

    • 复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的虚数部分,这里的x和y都是实数。
    • 复数在python存在小数字池:-5 ~ 257

    布尔值

    • 数字1代表真,0代表假,这个在工作中时常用到。

    查看数据类型

    • >>> a = 3
    • >>> type(a)
    • <type 'int'>
    • >>> a = 3.14
    • >>> type(a)
    • <type 'float'>
    • >>> a= 3.............3
    • >>> type(a)
    • <type 'long'>

    数据运算

    举几个工作中常用的几个
    '/',取余数,例如:5/3 = 2
    '%',取模,例如:5/3 = 1
    '==',判断两个值是否相等,相等则条件为真,反之为反
    '!=',判断两个值是否相等,不相等则条件为真,反之为假
    '>',判断左边的数是否大于右边的数,大于则真,小于则假
    '<',判断左边的数是否小于右边的数,小于则真,大于则假
    '>=',判断左边的数是否大于或等于右边的数,大于或等于则真,小于则假
    '<=',判断左边的数是否小于或等于左边的数,小于或等于为真,大于则假

    逻辑运算

    说白了就是数学中的与或非
    a || b ,a为真,b为真,则为真,只要有一方不为真,那就是假。
    a | b,a为真,b为真,则为真,a为真,b为假,则为真,a为假,b为真,为真,a为假,b为假,则为假,说明只要有一方为真,就为真,双方都为假,则为假

    第二天的重点

    • 元组
    • 列表
    • 字典

     

    元组

    元组是不可变的,一经定义了,就无法改变其值了,在实际工作中常用作存一些固定的值。

    定义元组
    fruit = ("apple","grape","pear")
    元组常用操作有
    索引,切片,循环,长度

     

    元组的索引

    fruit[0] = apple,fruit[1] = grape,fruit[2] = pear,说明索引的从0开始

     

    元组的切片

    • >>> fruit[1:]
    • ('grape', 'pear') #索引从0开始,所以1就代表grape,':'代表到最后一个元素
    • >>> fruit[0:]
    • ('apple', 'grape', 'pear')
    • >>> fruit[2:]
    • ('pear',) #2代表grape,因为后面再无元素,所以用","表示
    • >>> fruit[::]
    • ('apple', 'grape', 'pear') #就是代表元组全部元素
    • >>> fruit[:-1]
    • ('apple', 'grape') #"-1"代表最后一个元素,但有个规则,取头不取尾
    • >>> fruit[:-2]
    • ('apple',) #从开头取到倒数第二个元素,因为取头不取尾,所以为apple
    • >>> fruit[:-3]
    • () #无元素,所以返回为空
    • >>> fruit[0:1]
    • ('apple',) #取头不取尾
    • >>> fruit[0:2]
    • ('apple', 'grape')

    列表

    列表是我们最常用的数据类型,通过列表可以对数据实现最方便的存储、修改等操作(顾头不顾尾
    定义列表
    fruit = ["apple","grape","pear"]
    列表常用操作
    跟元组差不多,只是一个是不可变的,一个是可变的,列表很灵活,所以工作中到处是列表

     

    列表增加

    fruit = ["apple","grape","pear"]

    • >>> fruit = ["apple","grape","pear"]
    • >>> fruit.append("cherry") #增加列表元素
    • >>> print fruit
    • ['apple', 'grape', 'pear', 'cherry']
    • >>> fruit.insert(2,"banana") #在索引2的前面插入元素banana
    • >>> print fruit
    • ['apple', 'grape', 'banana', 'pear', 'cherry']
    • >>> fruit = ["apple","grape","pear"]
    • >>> print fruit.index("grape") #查询grape的索引
    • 1
    • >>> print fruit.index("apple") #查询apple的索引
    • 0
    • >>> print fruit.index("pear") #查询pear的索引
    • 2
    • >>> fruit = ["apple","grape","pear"]
    • >>> fruit.sort() #对列表排序
    • >>> print fruit
    • ['apple', 'grape', 'pear']
    • >>> fruit = ["apple","grape","pear","pear"]
    • >>> fruit.count("apple") #统计列表中apple出现的次数
    • 1
    • >>> fruit.count("grape") #统计列表中grape出现的次数
    • 1
    • >>> fruit.count("pear") #统计列表中pear出现的次数
    • 2
    • >>> fruit = ["apple","grape","pear","pear"]
    • >>> fruit.reverse() #列表反转
    • >>> print fruit
    • ['pear', 'pear', 'grape', 'apple']
    • >>> fruit = ["apple","grape","pear","pear"]
    • >>> number = [1,2,3,4]
    • >>> fruit.extend(number) #列表扩展,列表套列表
    • >>> print fruit
    • ['apple', 'grape', 'pear', 'pear', 1, 2, 3, 4]
    • >>> fruit = ["apple","grape","pear","pear"]
    • >>> for i in fruit:
    • ... print i
    • 执行结果
    • apple
    • grape
    • pear
    • pear

     

    列表删除的三种方法

    • >>> fruit = ["apple","grape","pear","pear"]
    • >>> del fruit[3]
    • >>> print fruit
    • ['apple', 'grape', 'pear']
    • >>> fruit = ["apple","grape","pear","pear"]
    • >>> fruit.remove("apple")
    • >>> print fruit
    • ['grape', 'pear', 'pear']
    • >>> fruit = ["apple","grape","pear","pear"]
    • >>> fruit.pop(2) #不指定下标,随机删除
    • 'pear'
    • >>> print fruit
    • ['apple', 'grape', 'pear']

    清除列表

    • fruit = ["apple","grape","pear","pear"]
    • fruit.clear()
    • print fruit
    • 执行结果为:[]

     

    字典

    key,value的方式储存数据,根据key来查找对应的value,就好比查字典查偏旁,速度相对来说,比元组和列表要快,而且字典可存放多个值,可修改指定key对应的值,所以也是可变的,还有就是字典无序。

    字典无序显示

    • >>> example = {"age":18,"name": "huihuang","salary":20000}
    • >>> print example
    • {'salary': 20000, 'age': 18, 'name': 'huihuang'}

    字典的其他操作和列表差不多,这里举几个例子

    查询

    • >>> example = {"age":18,"name":"huihuang","salary":20000}
    • >>> print example["age"]
    • 18
    • >>> print example["name"]
    • huihuang
    • >>> print example["salary"]
    • 20000

    增加

    • >>> example["job"] = "teacher"
    • >>> print example #没有就添加
    • {'salary': 20000, 'job': 'teacher', 'age': 18, 'name': 'huihuang'}
    • >>> example["salary"] = 20000
    • >>> print example
    • {'salary': 20000, 'job': 'teacher', 'age': 18, 'name': 'huihuang'}

    删除

    • >>> example = {"age":18,"name":"huihuang","salary":20000}
    • >>> del example["age"]
    • >>> print example
    • {'salary': 20000, 'name': 'huihuang'}

    作业

    需求:

    1、输入工资,并判断输入工资的合法性,输入三次不对就提出程序

    2、输入物品选项,并判断输入选项的合法性,并有相应的提示

    3、判断工资是否可以购买商品,可以购买,打印商品详情,无法购买,打印钱不够

    4、在输入选项的中输入quit退出程序,并打印商品详情和数量,剩余多少钱,花了多少钱  

    #!/usr/bin/env python
    #coding=utf8
    import pickle
    count = 0
    Shopping_List =[]
    Shoppling_Products = [
        ["Iphone",5800],
        ["MAC",15800],
        ["Coffee",30],
    ]
    with open("E:\\Information.txt") as f:
        text = f.readlines()
    
    while count < 3:
        user = input("please input you username:")
        passwd = input("please input your passwd:")
        if user == text[0].strip() and passwd == text[1].strip():
            print("欢迎来到购物车系统")
            break
        else:
            print("用户或者密码不对,请重启输入")
            count += 1
    else:
        print("你已用完3次输入的机会,账户锁定")
    
    while count < 3:
        Salary = input("please input your salary:")
        if Salary.isdigit():
            Salary = int(Salary)
            break
        else:
            print("请重新输入正确格式的工资")
            count += 1
    else:
        print("输入工资格式错误次数超过3次,退出程序")
        exit()
    while True:
        for index,item in enumerate(Shoppling_Products):
            print(index,item)
        User_Choice = input("please input your choice[quit]:")
        if User_Choice.isdigit():
            User_Choice = int(User_Choice)
            if User_Choice < len(Shoppling_Products) and User_Choice >= 0:
                Shop_Item = Shoppling_Products[User_Choice]
                if Shop_Item[1] <= Salary:
                    Salary -= Shop_Item[1]
                    Shopping_List.append(Shop_Item)
                    print("Added [%s] into your shopping cars" %Shop_Item,"你还剩[%s]元" %Salary)
                else:
                    print("钱不够,你只有人民币%s元" %Salary)
            elif User_Choice > 2:
                print("范围太大,请输入0-2的数字")
        elif User_Choice == "quit":
            print("------------商品如下---------")
            pre_list = []
            total_money = 0
            for i in Shopping_List:
                num = Shopping_List.count(i)
                money = int(i[1]) * int(num)
                if i in pre_list:
                    continue
                else:
                    print("物品:%s,个数:%s,共%s元" %(i[0],num,money))
                pre_list.append(i)
                total_money += money
                LeftMoney = []
                LeftMoney.append(LeftMoney)
                LeftMoney = pickle.dumps(LeftMoney)
                LeftMoney = pickle.loads()
    
            print("您总共花了%s元,还剩%s元" % (total_money,Salary))
            break
        else:
            print("错误的选项,请重新输入0-2之间的数字")
    View Code
    天天向上,空杯心态。
  • 相关阅读:
    Sharding-JDBC多数据源动态切换
    U 盘安装 CentOS 7 时出现 No Caching mode page found 问题的解决
    sudo 密码直接添加到命令行以方便实现脚本自动化
    Python3 Windows 虚拟环境的若干问题
    20 张图让你彻底弄懂 HTTPS 原理!
    全网写得最好的分库分表之 Sharding-JDBC 中间件介绍
    以为线程池很简单,结果第一道题就被干趴下了!
    以为线程池很简单,没想到第一问就被干趴下了
    分布式事务,看这篇就够了!
    我是一个线程池
  • 原文地址:https://www.cnblogs.com/uglyliu/p/5983213.html
Copyright © 2011-2022 走看看