zoukankan      html  css  js  c++  java
  • Python学习笔记(二)

    Python学习笔记(二):

    1. 多行注释也可用于多行打印
    2. Python中单双引号意义相同
    3. 格式化输出
    4. 数据类型
    5. for循环
    6. 列表(一)

    1. 电影推荐

    1. 被解救的姜戈
    2. 华尔街之狼
    3. 阿甘正传
    4. 辛德勒的名单
    5. 上帝之城
    6. 焦土之城
    7. 绝美之城

    2. 格式化输出

    • 占位符

      1. %s string 字符串
      2. %d digit 整数
        3. %ffloat浮点数
    • e.g.:

    name = Han
    msg = Name : %s %(name)
    

    3. 数据类型

    1. 整型
    2. 长整型(Python不区分整型和长整型)
    3. 浮点型
    4. 布尔型
    5. 字符串

    4. for循环

    • e.g:
    for i in range(3):
    	print("i")
    

    5. 列表★

    • e.g:
    a = ['1','2','3','4','5']
    
    # 增删改查
    
    # 查询-切片
    print(a[1:])		#取到最后
    print(a[1:-1])		#取到倒数第二个(顾头不顾尾)
    print(a[1:-1:2])	#步长默认为一
    print(a[3::-2])		#步长的正负代表方向
    
    # count-计算元素出现次数
    ['to','be','or','not','to','be'].count('to')
    
    # index-查找某一内容的索引
    
    print(a.index('1'))	#结果:0
    
    # 增加 append insert
    
    a.append('6')		#默认插入到最后一个位置
    a.insert(1,'x')		#插入到任意位置
    
    # extend-将一个列表的内容添加到另一个列表中
    
    a=[1,2,3]
    b=[4,5,6]
    a.extend(b)			#结果: a = [1,2,3,4,5,6] , b = [4,5,6]
    
    # 修改-取值->赋值
    
    a[1] = 'y'
    a[1:3] = ['a','b']
    
    # 删除 remove pop del
    
    a.remove('1')		#删除固定内容
    a.pop(1)			#根据索引删除内容并返回删除的值
    del a[0]
    
    # 排序 reserve sort
    
    a.reserve()
    print(a)			#结果为a的倒序
    
    a.sort()
    print(a)			#按照编码从小到大排序 
    
    

    6. 作业

    • 购物车程序-题干

    题干

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @File  : shopping.py
    # @Author: Ryoma
    # @Date  : 2017/9/9
    # @Desc  : 
    # @license : Copyright(C), blanc.site 
    # @Contact : ryomahan1996@gmail.com 
    # @Software : PyCharm Community Edition
    
    # 制定商品价格
    name = ['iphone6','mac book','coffee','books','bicyle']
    price = [5800,9000,32,80,1500]
    # 购物车
    shopping_cart = []
    print("Welcome to JD")
    # 输入工资
    salary = int(input("Please tell me your salary:"))
    # 打印商品列表
    print("Now you can buy :")
    for i in range(len(name)):
        print("%s . %s---%s" %(i+1,name[i],price[i]))
    print("Input 0 to move")
    # 购买
    while salary:
        id = int(input("What did you want to buy:(1-5)"))
        if id == 0:
            break
        elif 0<id<6 :
            if salary - price[id-1] >= 0:
                salary -= price[id-1]
                print("You buy a %s | balance is %s" %(name[id-1],salary))
                shopping_cart.append(id)
                continue
            else:
                print("Sorry , you didn't have enough money!")
                break
        else:
            print("You can input 1-5 to buy!")
    # 退出
    if len(shopping_cart)>0:
        print("You have purchased these goods:")
        for i in range(len(shopping_cart)):
            print("%s---%s" %(name[shopping_cart[i]-1],price[shopping_cart[i]-1]))
        print("Your balance is %s" %salary)
        print("Looking forward to your next visit!")
    else:
        print("You have purchased nothing!")
    
    以此为趣,乐在其中。
  • 相关阅读:
    kubernetes概述
    pygame--图形模块
    iPhone 上你可能还不知道的小技巧
    使用 JdbcTemplate 查询数据时报错:列名无效(已解决)
    【分享】怎样做架构师?
    关于 oh-my-zsh 插件的使用(以 Sublime Text 为例)
    VI/VIM 无法使用系统剪贴板(clipboard)?(Ubuntu&Mac OS X已解决)
    Ubuntu 下 Sublime 无法输入中文?(已解决)
    VIM 的帮助文档在哪里?看这里。
    推荐一款好用的文件/文件夹对比工具 —— Beyond Compare
  • 原文地址:https://www.cnblogs.com/ryomahan/p/7506542.html
Copyright © 2011-2022 走看看