PYTHON学习笔记-DAY-2

本节内容

  • 标准库和第三方库
# Author AlleyYu
import sys
print(sys.path)
print(sys.argv)



import os
print (os.system('dir')) #调用系统命令
cmd_res=os.system('dir')
print (cmd_res)

cmd_res2=os.popen('dir')
cmd_res1=os.popen('dir').read()
print ("MARK1---------->",cmd_res1)
print ("MARK2---------->",cmd_res2)

os.mkdir('newfilehere')#当前目录新建文件

 

  • ***数据类型:

1、数字
2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子,其中-5,4为实数,j为虚数,数学中表示复数是什么?。

int(整型)

  在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
  在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
long(长整型)
  跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
  注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float(浮点型)
  浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。
complex(复数)
  复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
注:Python中存在小数字池:-5 ~ 257

2、布尔值
  真或假
  1 或 0
3、字符串

 

  • 数据运算 

 

PyCodeObject和pyc文件:

PyCodeObject则是Python编译器真正编译成的结果。

当python程序运行时,编译的结果则是保存在位于内存中的PyCodeObject中,当Python程序运行结束时,Python解释器则将PyCodeObject写回到pyc文件中。

当python程序第二次运行时,首先程序会在硬盘中寻找pyc文件,如果找到,则直接载入,否则就重复上面的过程。

所以我们应该这样来定位PyCodeObject和pyc文件,我们说pyc文件其实是PyCodeObject的一种持久化保存方式。

算数运算:

比较运算:

赋值运算:

逻辑运算:

成员运算:

身份运算:

位运算:

运算符优先级:

 

# Author AlleyYu
# import os
# a = 60
# # b=13
# c = a>>2
# print (c )
#
alley=input('username:')

if type(alley) is str:
     print('yes')
#
# if 'a' in alley:
#     print ('include')

# mes='中文字符'
# teststring=mes.encode(encoding='UTF-8').decode(encoding='UTF-8')
# print (teststring)

 

  • 列表、元组操作 

通过列表可以对数据实现最方便的存储、修改等操作,列表是有序的因此可以通过index对其进行操作[]

元组--只读列表,不可更改 ()

 

列表常见操作有如下几类:

 

 1 # Author AlleyYu
 2 names=['alley1','test1','test23','test4','Black','Tom','Jerry','Fiona']
 3 
 4 # print(names[0:3]) # 打印index为0~2的数据
 5 # print(names[3:])
 6 # print(names[-3:])
 7 # names.insert(2,'InsertName')
 8 # names.append('AppendName')#新增
 9 names2=['Extemdname','extendName2']
10 names.extend(names2)#合并
11 print(names)
12 
13 #del 三种方法
14 #names.remove('Fiona')
15 # names.pop()
16 # names.pop(1)
17 # del names[0]
18 # print(names)
19 
20 
21 Index=names.index('test4')
22 print(names[names.index('test4')])
23 print(Index)

>>> names[0::2] #后面的2是代表,每隔一个元素,就取一个
>>> names.sort() #排序

>>> names.reverse() #反转
 


 

  • 字符串操作

 

>>> n3_arg
{'name': 'alex', 'age': 33}
>>> n3
'my name is {name} and age is {age}'
>>> n3.format_map(n3_arg)
'my name is alex and age is 33'


>>> n4.ljust(40,"-")
'Hello 2orld-----------------------------'
>>> n4.rjust(40,"-")
'-----------------------------Hello 2orld'


>>> s = "Hello World!"
>>> p = str.maketrans("abcdefg","3!@#$%^")
>>> s.translate(p)
'H$llo Worl#!


>>> b="ddefdsdff_哈哈" 
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True

 

  • 字典操作

字典的特性:dict是无序的;key必须是唯一的,so 天生去重

# Author AlleyYu

info={
    'CBD': ['Cisco', 'F5', 'a10'],
    'jiuxianqiao': ['Ericsson', 'Zhaowei', 'Telcome'],
    'wangjing': ['Moto', 'Angel', 'Cici']
}


# info['NANtong']=['A','B','C']#新增

# print(info.keys()) # print(info.values()) # print(info.items()) # print(info.setdefault('Guomao',['Morden','Hydrex'])) # print(info) #del info['Guomao']#标准删除 #info.pop('Guomao')# #随机删掉一个 # info.popitem() # print(info) b={ 'Najing':['Panda','TCL'], 2:3, 4:6 } info.update(b)# update 合并 print(info)

#循环Dictionary
#方法1
for key in info:
    print(key,info[key])

#方法2
for k,v in info.items(): #会先把dict转成list,数据里大时莫用
    print(k,v)
  • 购物车程序
# Author AlleyYu
#课上写的,希望以后看到后会觉得烂~~~
List=[['IPHONE',5000],['Bike',800],['coffe',40],['cookie',5]]

# info='''
#
# Here is the List for you
# 0.IPHONE',5000],
# 1.['Bike',800],
# 2.['coffe',40],
# 3['cookie',5
#
#
# '''
savings=int(input('Please input your salary>>>'))
#print (info)
Cart=[]
while True:
        for item,num in enumerate(List):
            print (item,num)

        Choice=input('please input the good you want to add>>>>')
   #     print (type(Choice))
        if Choice.isdigit():
            Choice=int(Choice)
            if Choice >=0 and Choice< len(List):
                print (List[Choice][1])
                flag=savings - List[Choice][1]
                if flag>0:
                    savings = flag
                    print ('you have add ',List[Choice][0])
                    print('Your current balance ', savings)
                    Cart.append(List[Choice])
                    continue
                else:
                    print('No enough money left')
                    continue
            else:
                print('product not exits')
                print(Cart[:])
                print('you balance is :', savings)
                continue

        elif Choice!='q':
             print(Cart[:])
             print('you balance is :', savings)
             exit()


        else:
             print('invalid input')
             continue

 

posted on 2016-08-01 10:47  AlleyYu  阅读(195)  评论(1编辑  收藏  举报