Python学习(一)

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

  8. 被解救的姜戈
  9. 华尔街之狼
  10. 阿甘正传
  11. 辛德勒的名单
  12. 上帝之城
  13. 焦土之城
  14. 绝美之城

2. 格式化输出

  • 占位符
  1. %s string 字符串
    1. %d digit 整数
    2. %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:
    ```python
    a = ['1','2','3','4','5']

增删改查

查询-切片

print(a[1:]) #取到最后
print(a[1:-1]) #取到倒数第二个(顾头不顾尾)
print(a[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!")

多行注释也可用于多行打印
Python中单双引号意义相同
格式化输出
数据类型
for循环
列表(一)
1. 电影推荐
被解救的姜戈
华尔街之狼
阿甘正传
辛德勒的名单
上帝之城
焦土之城
绝美之城
2. 格式化输出
占位符
%s string 字符串
%d digit 整数
%ffloat浮点数
e.g.:
name = Han
msg = Name : %s %(name)
3. 数据类型
整型
长整型(Python不区分整型和长整型)
浮点型
布尔型
字符串
4. for循环
e.g:
for i in range(3):
print("i")
5. 列表★
e.g:
```python
a = ['1','2','3','4','5']
增删改查
查询-切片
print(a[1:]) #取到最后
print(a[1:-1]) #取到倒数第二个(顾头不顾尾)
print(a[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!")

转载于:https://www.cnblogs.com/Jansens520/p/7510460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值