Day2 Python学习 数组,循环,字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Day2 Python学习 数组,循环,字符串相关的知识,希望对你有一定的参考价值。

一、学习的内容

1、命令 sys.path()

显示当前环境变量

2、命令os.system("dir")

显示当前目录内的文件,只显示,不保存结果

3、命令text = os.popen("dir").read()

显示当前目录内的文件,可保存结果

4、命令 os.mkdir("new_dir")

创建一个叫“new_dir”的文件夹

5、三元运算

a,b,c = 1,2,3

d = a if a> b else c

结果d =c

6、数据类型转换

字符串转二进制

‘你好’.encode("utf=8")

二进制转字符串

‘\xe4\xbd\xa0\xe5\xa5\xbd‘.decode("utf-8")

7、数组

name = [“aaa”,"bbb","ccc"]

print(name[0,2]),就是显示,aaa,bbb

print(name[-1]),就是显示,ccc

print(name[-2,-1]),就是显示,bbb,ccc

print(name[:3]),就是显示,aaa,bbb,ccc

name.append("444"),在最后插入

name.insert(1,"555"),插入到1

name.clear(),清空

name.reverse(),反转排序

name.sort(),按字母排序

name.remove("aaa"),删除aaa

name.delete[0],删除0

name.extene([1,2,3,4]),添加数组

name2 = name.copy(),复制,不是引用,比如多层数组,对多层数组只COPY内存地址(指针),要导入 import copy

数组复制的其他写法 name2 = name[:]    name2 = list(name)

name2 = name.deepcopy(),深层复制,也能完全复制多层数组

 

 

作业:

1、让用户输入工资

2、根据编号购买商品

3、选择商品后,看余额是否购,够就扣钱,不够重选

4、按Q退出,并打出购买过的商品和余额

 1 product_list = [
 2     (Iphone,5800),
 3     (Mac Pro,9800),
 4     (Bike,800),
 5     (Watch,10600),
 6     (Coffee,31),
 7     (Alex Python,120),
 8 ]
 9 shoping_list = []
10 gongzi = input("输入工资")
11 if gongzi.isdigit():   #判断是否为数字
12     gongzi = int(gongzi)
13     for index,i in enumerate(product_list):   #输出数组位置
14             print(index,i)
15     while True:
16         shangpings = input("输入要购买商品")
17         if shangpings.isdigit():
18             shangpings = int(shangpings)
19             if shangpings < 6 and shangpings >= 0:
20                 aa = product_list[shangpings]
21                 if aa[1] <= gongzi:
22                     gongzi -= aa[1]
23                     print("购买的商品是%s,余额是%s"%(aa[0],gongzi))
24                     shoping_list.append(aa)
25 
26 
27                 else:
28                     print("余额不足,只有:",gongzi)
29             else:
30                 print("输入错误")
31         elif shangpings == "q":
32             print("-----购买的商品列表------")
33             print(shoping_list)
34             print("余额:", gongzi)
35             exit()
36         else:
37             print("需要输入数字")

 

 

 

字符串操作

 1 name.capitalize()  首字母大写
 2 name.casefold()   大写全部变小写
 3 name.center(50,"-")  输出 ---------------------Alex Li----------------------
 4 name.count(lex) 统计 lex出现次数
 5 name.encode()  将字符串编码成bytes格式
 6 name.endswith("Li")  判断字符串是否以 Li结尾
 7  "Alex\tLi".expandtabs(10) 输出Alex      Li, 将\t转换成多长的空格 
 8  name.find(A)  查找A,找到返回其索引, 找不到返回-1 
 9 
10 format :
11     >>> msg = "my name is {}, and age is {}"
12     >>> msg.format("alex",22)
13     my name is alex, and age is 22
14     >>> msg = "my name is {1}, and age is {0}"
15     >>> msg.format("alex",22)
16     my name is 22, and age is alex
17     >>> msg = "my name is {name}, and age is {age}"
18     >>> msg.format(age=22,name="ale")
19     my name is ale, and age is 22
20 format_map
21     >>> msg.format_map({name:alex,age:22})
22     my name is alex, and age is 22
23 
24 
25 msg.index(a)  返回a所在字符串的索引
26 9aA.isalnum()   True
27 
28 9.isdigit() 是否整数
29 name.isnumeric  
30 name.isprintable
31 name.isspace
32 name.istitle
33 name.isupper
34  "|".join([alex,jack,rain])
35 alex|jack|rain
36 
37 
38 maketrans
39     >>> intab = "aeiou"  #This is the string having actual characters. 
40     >>> outtab = "12345" #This is the string having corresponding mapping character
41     >>> trantab = str.maketrans(intab, outtab)
42     >>> 
43     >>> str = "this is string example....wow!!!"
44     >>> str.translate(trantab)
45     th3s 3s str3ng 2x1mpl2....w4w!!!
46 
47  msg.partition(is)   输出 (my name , is,  {name}, and age is {age}) 
48 
49  >>> "alex li, chinese name is lijie".replace("li","LI",1)
50      alex LI, chinese name is lijie
51 
52  msg.swapcase 大小写互换
53 
54 
55  >>> msg.zfill(40)
56 00000my name is {name}, and age is {age}
57 
58 
59 
60 >>> n4.ljust(40,"-")
61 Hello 2orld-----------------------------
62 >>> n4.rjust(40,"-")
63 -----------------------------Hello 2orld
64 
65 
66 >>> b="ddefdsdff_哈哈" 
67 >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
68 True

 

以上是关于Day2 Python学习 数组,循环,字符串的主要内容,如果未能解决你的问题,请参考以下文章

python 学习 day2

Python学习[day2]while循环以及执行流程格式化输出运算符

Day2-python基础2

python学习笔记Day2

python学习Day2之数据结构

python学习day2(字符串的一些小魔法)