【python之旅】python的基础一

一、关于模块那些事

python的强大之处在于他有着丰富且强大的标准库和第三方库,很对功能都有相应的python库支持

例如:

sys模块:

 # Author :GU
import sys
print(sys.path) ###打印环境变量
print(sys.argv) ###打印相对路径

执行结果:

 ['C:\\Users\\Administrator\\PycharmProjects\\s14\\day2', 'C:\\Users\\Administrator\\PycharmProjects\\s14', 'C:\\Python35\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35\\lib', 'C:\\Python35', 'C:\\Python35\\lib\\site-packages']
###################
python sys1.py helo world
['sys1.py', 'helo', 'world'] ####把执行脚本时传递的参数获取到了

 os模块:

 # Author :GU
import os
cmd_res = os.system("dir") ###执行命令不报存结果
print(" >",cmd_res)
执行结果
C:\Users\Administrator\PycharmProjects\s14\day2 ��Ŀ¼
2016/08/01 ��һ ���� 09:46 <DIR> .
2016/08/01 ��һ ���� 09:46 <DIR> ..
2016/07/30 ���� ���� 12:49 137 login.py
2016/07/30 ���� ���� 10:36 <DIR> new_dir
2016/08/01 ��һ ���� 09:46 134 os_tesy.py
  > 0 返回为0,则成功
因为字符集的问题所有乱码,属于正常
# Author :GU
import os
#cmd_res = os.system("dir")
cmd_res = os.popen("dir").read() ###执行这个命令之后,结果存在内存中,通过read读取出来
#print(" >",cmd_res)
os.mkdir("new_dir")
创建一个文件夹以new_dir命名的

二、什么是.pyc

执行Python代码时,如果导入了其他的 .py 文件,那么,执行过程中会先自动生成一个与其同名的 .pyc 文件,第二次运行的时候程序会先找这个pyc的文件,该文件就是Python解释器编译之后产生的字节码。

代码经过编译可以产生字节码;字节码通过反编译也可以得到代码。(任何字节码通过反编译都可以得到代码)

三、关于数据类型的那些事

 1、数字

int (整型)   

   在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
  在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
 long(长整型)python3变为整型
   跟C语言不同,Python的长整数没有指定位宽,
  即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
  注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。

float(浮点型)

 浮点数用来处理实数,即带有小数的数字。
浮点表示的形式是小数,但小数不一定都是浮点型
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
   complex(复数)
   复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
  注:Python中存在小数字池:-5 ~ 257

 2、布尔值  

 真或假
0或1
>>> a = 0
>>> if a :print("a")
...
>>> a = 1
>>> if a :print("a")
...
a

四、数据运算

算术运算:

操作符 描述符 例子
+ 加法 - 对操作符的两侧增加值 a + b = 30
- 减法 - 减去从左侧操作数右侧操作数 a - b = -10
* 乘法 - 相乘的运算符两侧的值 a * b = 200
/ 除 - 由右侧操作数除以左侧操作数 b / a = 2
% 模 - 由右侧操作数和余返回除以左侧操作数 b % a = 0
** 指数- 执行对操作指数(幂)的计算 a**b = 10 的幂 20
// 取整除 - 操作数的除法,其中结果是将小数点后的位数被除去的商。 9//2 =  4 而 9.0//2.0 = 4.0

比较运算:

运算符 描述 示例
== 检查,两个操作数的值是否相等,如果是则条件变为真。 (a == b) 不为 true.
!= 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 (a != b) 为 true.
<> 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 (a <> b) 为 true。这个类似于 != 运算符
> 检查左操作数的值是否大于右操作数的值,如果是,则条件成立。 (a > b) 不为 true.
< 检查左操作数的值是否小于右操作数的值,如果是,则条件成立。 (a < b) 为 true.
>= 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立。 (a >= b) 不为 true.
<= 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立。 (a <= b) 为 true.

赋值运算:

运算符 描述 示例
= 简单的赋值运算符,赋值从右侧操作数左侧操作数 c = a + b将指定的值 a + b 到  c
+= 加法AND赋值操作符,它增加了右操作数左操作数和结果赋给左操作数 c += a 相当于 c = c + a
-= 减AND赋值操作符,它减去右边的操作数从左边操作数,并将结果赋给左操作数 c -= a 相当于 c = c - a
*= 乘法AND赋值操作符,它乘以右边的操作数与左操作数,并将结果赋给左操作数 c *= a 相当于 c = c * a
/= 除法AND赋值操作符,它把左操作数与正确的操作数,并将结果赋给左操作数 c /= a 相当于= c / a
%= 模量AND赋值操作符,它需要使用两个操作数的模量和分配结果左操作数 c %= a is equivalent to c = c % a
**= 指数AND赋值运算符,执行指数(功率)计算操作符和赋值给左操作数 c **= a 相当于 c = c ** a
//= 地板除,并分配一个值,执行地板除对操作和赋值给左操作数 c //= a 相当于 c = c // a

位运算

操作符 描述 示例
& 二进制和复制操作了一下,结果,如果它存在于两个操作数。 (a & b) = 12 即 0000 1100
| 二进制或复制操作了一个比特,如果它存在一个操作数中。 (a | b) = 61 即 0011 1101
^ 二进制异或运算符的副本,如果它被设置在一个操作数而不是两个比特。 (a ^ b) =  49 即  0011 0001
~ 二进制的补运算符是一元的,并有“翻转”位的效果。 (~a ) =  -61 即 1100 0011以2的补码形式由于带符号二进制数。
<< 二进位向左移位运算符。左操作数的值左移由右操作数指定的位数。 a << 2 = 240 即 1111 0000
>> 二进位向右移位运算符。左操作数的值是由右操作数指定的位数向右移动。

a >> 2 = 15 即 0000 1111

 a = 60            # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0 c = a & b; # 12 = 0000 1100
print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101
print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001
print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011
print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000
print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111
print "Line 6 - Value of c is ", c

逻辑运算:

运算符 描述 示例
and 所谓逻辑与运算符。如果两个操作数都是真的,那么则条件成立。 (a and b) 为 true.
or 所谓逻辑OR运算符。如果有两个操作数都是非零然后再条件变为真。 (a or b) 为 true.
not 所谓逻辑非运算符。用于反转操作数的逻辑状态。如果一个条件为真,则逻辑非运算符将返回false。

not(a and b) 为 false.

 例子:
and运算,都为真时为真
>>> 9 > 8 and 9 < 10
True
>>> 9 > 8 and 9 > 10
False or运算,有一个为真的时候即可为真
>>> 9 > 8 or 9 > 10
True
>>> 9 < 8 or 9 > 10
False
>>> not运算,假为真真为假
>>> not 6.2 <= 6
True
>>> not 6.2 >= 6
False
>>>

五、三元运算:

1 result = 值1 if 条件 else 值2

  例子:

1 name = raw_input("please input your name: ")
2 if name = "tianshuai":
3 print "you are so shuai!!!"
4 else:
5 print "you are ok"

六、进制

  • 二进制,01
  • 八进制,01234567
  • 十进制,0123456789
  • 十六进制,0123456789ABCDEF

七、bytes类型

 msg = "顾云"
print(msg)
print(msg.encode(encoding="utf-8"))
print(msg.encode(encoding="utf-8").decode(encoding="utf-8")) 执行结果:
顾云
b'\xe9\xa1\xbe\xe4\xba\x91'
顾云

八、列表的操作

  列表是我们最常用的数据类型,通过列表可以对数据实现最方便的存储、修改等操作

  如何定义列表?

 names = ["Zhangyang","Guyun","Xiangpeng","Xuliangchen"]

  切片

 names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
print(names[0],names[2])
执行结果:
zhangyang xiangpeng
==============================================================
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
print(names[1:3]) ####切片
执行结果:
['guyun', 'xiangpeng']
=============================================================
取出最后一个值
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
print(names[-1])
执行结果:
xuliangchen
=============================================================
取最后两个值
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
print(names[-2:])
执行结果:
['xiangpeng', 'xuliangchen']
=============================================================
从0取到3
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
print(names[:3])
执行结果:
['zhangyang', 'guyun', 'xiangpeng']

  追加:

 追加到末尾:
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.append("leihaidong")
print(names)
执行结果:
['zhangyang', 'guyun', 'xiangpeng', 'xuliangchen', 'leihaidong']
=======================================================================
追加到guyn的前面
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.insert(1,"chenronghua")
print(names)
执行结果:
['zhangyang', 'chenronghua', 'guyun', 'xiangpeng', 'xuliangchen']
=======================================================================
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
print(names)
执行结果:
['zhangyang', 'chenronghua', 'guyun', 'xinzhiyu', 'xiangpeng', 'xuliangchen']

  修改:

 将guyun改为xiedi
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
print(names)
执行结果:
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng', 'xuliangchen']

  删除:

 删除chenronghua
方法一:
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
names.remove("chenronghua")
print(names)
执行结果:
['zhangyang', 'xiedi', 'xinzhiyu', 'xiangpeng', 'xuliangchen']
方法二:
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
del names[1]
print(names)
执行结果:
['zhangyang', 'xiedi', 'xinzhiyu', 'xiangpeng', 'xuliangchen']
==================================================
删除最后一个
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
names.pop() ##如果写上下标就可以指定删除
print(names)
执行结果:
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng']

  获取下标:

 # Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
names.pop()
print(names)
print(names.index("xiedi"))
执行结果:
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng']
2
=============================================
#取出xiedi
# Author :GU
names = ["zhangyang","guyun","xiangpeng","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
names.pop()
print(names)
print(names.index("xiedi"))
print(names[names.index("xiedi")])
执行结果:
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng']
2
xiedi

  统计:

# Author :GU
names = ["zhangyang","guyun","xiangpeng","chenronghua","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
print(names)
print(names.count("chenronghua"))
执行结果:
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng', 'chenronghua', 'xuliangchen']
2

  清除:

# Author :GU
names = ["zhangyang","guyun","xiangpeng","chenronghua","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
names.clear()
print(names)
执行结果:
[]

  反转:

 # Author :GU
names = ["zhangyang","guyun","xiangpeng","chenronghua","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
print(names)
names.reverse()
print(names)
执行结果:
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng', 'chenronghua', 'xuliangchen']
['xuliangchen', 'chenronghua', 'xiangpeng', 'xinzhiyu', 'xiedi', 'chenronghua', 'zhangyang']

  排序:

 # Author :GU
names = ["zhangyang","guyun","xiangpeng","chenronghua","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
print(names)
names.sort()
print(names)
执行结果:
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng', 'chenronghua', 'xuliangchen']
['chenronghua', 'chenronghua', 'xiangpeng', 'xiedi', 'xinzhiyu', 'xuliangchen', 'zhangyang']

  扩展:

 # Author :GU
names = ["zhangyang","guyun","xiangpeng","chenronghua","xuliangchen"]
names.insert(1,"chenronghua")
names.insert(3,"xinzhiyu")
names[2] = "xiedi"
print(names)
names2 = [1,2,3,4]
names.extend(names2)
print(names)
执行结果:
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng', 'chenronghua', 'xuliangchen']
['zhangyang', 'chenronghua', 'xiedi', 'xinzhiyu', 'xiangpeng', 'chenronghua', 'xuliangchen', 1, 2, 3, 4]

  拷贝:

# Author :GU
names = ["zhangyang","guyun","xiangpeng","chenronghua","xuliangchen"]
name2 = names.copy()
print(names)
print(name2)
names[3] ="向鹏"
print(names)
执行结果:
['zhangyang', 'guyun', 'xiangpeng', 'chenronghua', 'xuliangchen']
['zhangyang', 'guyun', 'xiangpeng', 'chenronghua', 'xuliangchen']
['zhangyang', 'guyun', 'xiangpeng', '向鹏', 'xuliangchen']
================================================

  循环:

 # Author :GU
names = ["zhangyang","guyun","xiangpeng",["alex","jack"],"chenronghua","xuliangchen"]
for i in names:
print(i)
执行结果:
zhangyang
guyun
xiangpeng
['alex', 'jack']
chenronghua
xuliangchen
=========================================

  元组:

  元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表

 names = ("alex","jack","eric")
 只有两个方法:1、count#统计 2、index#获取下

九、字符串的常用操作

  首字母大写capitalize()

 # Author :GU
name = "guyun"
print(name.capitalize())
执行结果:
Guyun

  统计重复的字符count:

 # Author :GU
name = "guyun"
print(name.count("u"))
执行结果:
2

  center的用法:  

  一共打印50个字符,不够的用=补上字符放中间center的用法

 # Author :GU
name = "guyun a "
print(name.center(50,"="))
执行结果
=====================guyun a =====================

  以什么结尾endswith:

 # Author :GU
name = "guyun"
print(name.endswith("un"))
执行结果:
True

  找到索引:

 # Author :GU
name = "guyun"
print(name.find("y"))
执行结果:
2

  判断是否是纯英文字符:

 # Author :GU
name = "guyun"
print(name.isalpha())
执行结果:
True

十、字典的操作

  编写字典:

 # Author :GU
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}

  如果存在则修改,不存在则创建:

 >>> info['stu1101'] = "武藤兰"
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}

  查找:

 # Author :GU
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
print(info["stu1101"])
 执行结果:
 TengLan Wu
 >>> info.get("stu1102") #获取
 'LongZe Luola'

  删除:

 # Author :GU
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
info.pop("stu1101")
print(info)
执行结果:
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

  多级字典嵌套及操作:

 av_catalog = {
"欧美":{
"www.youporn.com": ["很多免费的,世界最大的","质量一般"],
"www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
"letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
"x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
},
"日韩":{
"tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
},
"大陆":{
"":["全部免费,真好,好人一生平安","服务器在国外,慢"]
}
} av_catalog["大陆"][""][1] += ",可以用爬虫爬下来"
print(av_catalog["大陆"][""])
#ouput
['全部免费,真好,好人一生平安', '服务器在国外,慢,可以用爬虫爬下来']
上一篇:Qt 下快速读写Excel指南(尘中远)


下一篇:Optimistic concurrency control