第二章内容及作业
第二章课后作业:
your_cart=[] product_list=[['iphone',5000],['book',50],['bike',1500],['coffee',30],['ticket',80]] salary = input('pls input your salary:') if salary.isdigit(): salary = int(salary) # 如果输入的是0-9字符串类型的数字;(它是数字,只不过是字符串形式)那么认为是真!然后转int类型; while True: for i in enumerate(product_list): print(i) your_choice = input("pls pick your favorite stuff:") if your_choice.isdigit(): your_choice = int(your_choice) if your_choice <len(product_list) and your_choice >=0: if product_list[your_choice][1]<=salary: your_cart.append(product_list[your_choice][0]) salary = salary - product_list[your_choice][1] print("in your_cart:%s,and money left:%s"%(your_cart,salary)) else: print("not enough money left!") else: print("your choice is not exit.") elif your_choice == 'q': print("what are in your cart:%s"%your_cart) break
import getpass
username = input("username:")
password = getpass.getpass("password:")
print (username,password)
name = "alex" 引号,代表字符串;
print(type(name)) <class 'str'>
input 接收的是字符串;
22
str(22) '22'
依赖关系未满足安装使用办法:
sudo apt --fix-broken install
字符编码
数据类型;
for ..else
ascII 码(512 256 128 64 32 16 8 4 2 1)----unicode 万国码;bit=1Byte=一个字节;
1024KB=1MB=100万字符
每个字符占8位;
unicode 4个字节;
utf-8 = 8bits 可变长编码
英文一个字节;中文3个字节;
欧洲2个字节;
'''
多行内容
'''
'''内容不能执行,但是可以被打印; '''内容''' 变成字符串了;
但引号''和"" 没有任何区别;适用于单行;
name = "\t Alex Li;Rain wang;Jack john \t"
\t4个空格(tab健)
\n 换行
print(name.strip())
name.strip() #把字符串前后去掉空格;
name.split() #把字符串按空格分割成列表;
name.split(";")#把字符串按;分割成列表;
len(name)#计算字符串长度;
b_list.index("wuxiao") #查找元素的索引(下标);
b_list.count("wuxiao") #统计wuxiao有几个;
b_list.append("wuxiao") #增加;
b_list[b_list.index('liuxiao')]='徐雨' #改,根据index更改元素
b_list.insert(4,'chentao') #在特定位置插入;
b_list.pop(3) #删除指定index值;返回被删除的值;
eg. >>> b_list.index('wuxiao')
3
>>> b_list.pop(3)
'wuxiao'
del b_list[1] #删除下标对应的元素;
b_list.remove('zhouxiao') #默认删除'zhouxiao' 列表里面第一个出现的元素;
切片法则:顾头不顾尾 a = ['a','b','c','d','e']
a[0] = 'a'
a[1:] = ['b','c','d','e']
a[-1] = 'e'
a[-3:] = ['c','d','e']
a[-3] = 'c'
a[:-1] = ['a','b','c','d']
a[0::2] = ['a','c','e'] #2跳过一个取值;步长=2
a[0::3] = ['a','d'] #跳过2个取值;步长=3
#####################################################################
b="zhouxiao,zhangxiao,feixiao,wuxiao,liuxiao,fenxiao,waixiao"
b_list=b.split(",")
>>>b_list
['zhouxiao', 'zhangxiao', 'feixiao', 'wuxiao', 'liuxiao', 'fenxiao', 'waixiao'
]
>>>print(len(b_list))
7
>>>print(b_list.index("wuxiao"))
3
>>>b_list[3]
'wuxiao'
#####################################################################
for i in range(10):
if i <=5:
print(i)
else:
#break 有break跳出本层循环;
print("------")
***********************************
for i in range(10):
if i > 5:
print(i)
else:
continue 跳出本次循环;print没有执行;
print("------")
############################################
for i in range(10):
print(i)
if i==5:
break
else: for 循环 带else 结束;
print("done") #当for 循环正常结束else被执行;否则else不执行;
###################################################
count = 0
while count <100:
print("...",count)
count +=1
if count ==50:
break
else:
print("done..") #while 也可以有else语句,正常执行完可执行else
###################################################
list2=(3,) 必须有逗号;元组类型;
if salary.isdigit(): 如果输入的是0-9字符串类型的数字;(它是数字,只不过是字符串形式)那么认为是真!然后转int类型;
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
***************************************************************
a=[['a',500],['b',50],['c',600]]
>>> for i in enumerate(a):print(i)
...
(0, ['a', 500])
(1, ['b', 50])
(2, ['c', 600])
######################################################################
格式化字符串时,Python使用一个字符串作为模板。模板中有格式符,这些格式符为真实值预留位置,并说明真实数值应该呈现的格式。Python用一个tuple将多个值传递给模板,每个值对应一个格式符。
比如下面的例子:
print("I'm %s. I'm %d year old" % ('Vamei', 99))