python之路03

 一、模块初始

import sys

print(sys.path)#打印python的环境

print(sys.argv)#打印相对路径

print(sys.argv【2】)#打印对应的变量

import os

cmd = os.system("dir")

print(cmd)

os.mkdir('new_dir')

#第三方库

在同一目录下或在lib/site-package下可直接import,其他情况下需写路径

import 

数据运算:

3/2 ==1

5%2 ==1返回余数

!=

<>

8bit =byte

1024byte=1kbyte

1024kbyte=1mbyte

1024mb=1gb

 2014gb=1T

&   |     ^(异或运算)    ~(反转)  >>右移      <<左移

str和二进制的转换

列表的操作

names = ['zhangyang','guyun','xiangpeng','xuliangchen']

print(names)
print(names[0],names[2])
print(names[0:2])#切片,顾头不顾尾
print(names[0:])
print(names[-1])
print(names[-3:])

names.append("leihaidong")
names.insert(1,'chenronghua')
names.insert(3,'xinzhiyun')
names[2]='xiedi'#names.pop(2)
names.remove('chenronghua')
del names[1]
names.pop()#删除最后一个

print(names.index('xiedi'))

print(names[names.index('xiedi')])

print(names.count('xiedi'))
names.clear()
names.reverse()#反转
names.sort()#排序

names1=[1,2,3,4]
names.extend(names1)

names2 = names.copy()#浅copy
names[1]= 'sd'
print(names,names2)#names变了,names2不变

#但是names还有一层的话,都变

import copy
names3 = copy.copy(names)
names3 = copy.deepcopy(names)

for i in range(1,10,2):
print(i)

 对浅copy的补充

import copy
person = ['name',['saving',100]]
p1= copy.copy(person)
p2= person[:]#也是浅copy
p3= list(person)


p1=person[:]
p2=person[:]
p1[0]='alex'
p2[1]='angal'

#tuple
#它只有两个方法:count、index


shopping_car.py
#! usr/bin/env python
# encoding:utf-8
# __author__="Macal"

product_list = [
('iphone', 5800),
("Starbuck Latte", 31),
("Alex python", 20),
('bike', 800),
('coffee', 30)
]
shopping_list=[]
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
user_choice = input("选择要买什么?")
if user_choice.isdigit() :
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >=0:
P_item = product_list[user_choice]
if P_item[1] <=salary:#买得起
shopping_list.append(P_item)
salary -= P_item[1]
print("Add %s into shopping cart,your current balance is 33[31;1m%s33[0m" %(P_item,salary))
else:
print('your current balance is not enough!')
else:
print('product code %s is not exist' %user_choice)
elif user_choice=='q':
print('----------shopping list----------')
for p in shopping_list:
print(p)
print('Your balance:',salary)
exit()
原文地址:https://www.cnblogs.com/Macal/p/6754661.html