Pycharm使用技巧:
1.tab批量换space:
Edit-->Convert Indents--> to spaces/tabs
2.tab键补全
File-->power sava mode 不勾选
3批量注释代码
同时按住ctl+/

range介绍
Range(2)
[0,1]
实际上是一个列表
Rang(1,4)
[1,2,3]
Range(1,4,2)
[1,3]

列表
a=[”a”,”b”,”c”,”d”]
a是一个对象,才能调用方法

增删改查
增加:

a.append("f")
print(a)
a.insert(1,"o")
print(a)

['a', 'b', 'c', 'd', 'e', 'f']
['a', 'o', 'b', 'c', 'd', 'e', 'f']

查找:
切片

a=['a','b','c','d','e']
print(a[1])
print(a[1:])
print(a[1:-1])
print(a[1:4:2])
print(a[1::-1])

b
['b', 'c', 'd', 'e']
['b', 'c', 'd']
['b', 'd']
['b', 'a']
Print(“a” in a)

a[1]="1"
print(a)

['a', '1', 'b', 'c', 'd', 'e', 'f']

a[1:3]=[2,3]
print(a)

['a', 2, 3, 'c', 'd', 'e', 'f']

删除:
4种方法:
Remove,pop,del,clear
Pop可以返回删除的值

a.remove("f")
print(a)
n=a.pop(1)
print(a)
print(n)
del(a[0])
print(a)
a.clear()
print(a)
print(type(a))
del a
print(a)

['a', 2, 3, 'c', 'd', 'e']
['a', 3, 'c', 'd', 'e']
2
[3, 'c', 'd', 'e']
[]
<class 'list'>
报错

如果要删除的元素有多个,remove只能删第一个元素

a=["w","a","w"]
print(a)
a.remove("w")
print(a)

['w', 'a', 'w']
['a', 'w']

列表的其他操作:
Extend 把其他列表加入列表

print(a)
b=[4,5,6]
a.extend(b)
print(a)
print(b)

['a', 3, 'c', 'd', 'e']
['a', 3, 'c', 'd', 'e', 4, 5, 6]
[4, 5, 6]

Index通过内容找索引
print(a.index("a"))
0
Reverse
Sort 只支持全数字或全字符串

实例:购物车程序
输入工资
商品列表

  1. mobile 5000
  2. book 100
  3. cup 3
  4. hair 500
    算钱,买完结账,显示商品和价格,余额。余额不足提示。
# -*-coding:utf-8 -*-
__date__ = '2018/2/6 17:56'
__author__ = 'xiaojiaxin'
__file_name__ = 'shopping_car'

salary=int(input("input your salary:"))
counter=0
remaining_sum=salary
shopping_sum=[]
shopping={"mobile":5000,"book":128,"cup":3,"hair":70}
print('''The product lists:
mobile:%d
book:%d
cup:%d
hair:%d
'''%(shopping["mobile"],shopping["book"],shopping["cup"],shopping["hair"]))
while True:
    shopping_car=input("enter the product's name:")

    if shopping_car in shopping:
        counter+=shopping[shopping_car]
        remaining_sum=salary-counter
        shopping_sum.append(shopping_car)
        flag=input("bug more?[y/n]")
        if flag=="y":
            continue
        else:
            if remaining_sum>=0:
                print("the remaining sum is %d"%remaining_sum)
                print("you have bug %s"%(shopping_sum))
                break

            else:
                print("the remaining sum is not enough to pay for it!")
                break

    else:
        print("The product do not exist!")