python 基础篇(一),,默认的python的



默认的python的文件为:文件名.py

#!/usr/bin/env python
# coding=utf-8 对中文的支持(切记:等号两边没有空格)
执行python 文件:
[root@localhost Desktop]# python test1.py

[root@localhost Desktop]# chmod +x test1.py
[root@localhost Desktop]# ./ test1.py

变量: 指向内存的一个符号
不同文件系统也是属于不同的类型 它作用于磁盘
python:强类型的动态语言 “变量可以替换,包括变量的类型”

数据类型分为两大类: 数字类型和字符串类型
>>> a = 1
>>> type(a)
<type ‘int‘>
>>> a= ‘str‘
>>> type(a)
<type ‘str‘>
不同的数据类型时不能做变换的
>>> a=‘hello‘
>>> b= ‘world‘
>>> a+b
‘helloworld‘
同其他语言一样 在python中: 从高精度向低精度转换时会存在数据损失,在低精度向高精度转换时不会存在

程序=数据结构+算法
优先级: 单目>双目 (单目:! 双目运算符:+ - * / ;在python里面没有三目运算符)
算术运算符 > 位操作运算符>比较运算符>逻辑运算符
算术运算符: + - * / %
位操作运算符: << >> & ^ | ~
比较运算符: < <= > >= !=
逻辑运算符:and or not 赋值=

() 优先级最大 赋值= 优先级最小

表达式:
除法运算:
>>> 10/3
3
>>> 10/3.0
3.3333333333333335
>>> 10.0/3
3.3333333333333335
幂运算:
>>> 2**4
16
除法取整运算:
>>> 10//3.0
3.0
>>> 10.0//3
3.0
>>> 10//3
3

取模运算:
>>> 10%3
1
>>> 10%3.0
1.0


按位与 & 全为真则为真
或 | 有一个为真则为真
异或 ^ 有两个不同时则为真,相同时则为假


>>> 2<<3 00010 左移3位 10 000=2**4
16
>>> 2>>3 00010 右移三位出界则为零
0
>>> 3&2 按位与:11&10 => 10
2
>>> 3^2 异或: 11^10 => 01
1
>>> 3|2 按位或:11|10 => 11
3



程序结构:
if 语句 只能进入一个分支执行且执行完跳出 在if语句中只有一个 else 可有多个 elif
分支语句只有一个if
循环语句:while for break continue 且循环里面可以有else
while condition:
expression
for item in 迭代器(列表,元组等)
expression
break 退出整个循环体
continue 退出当前循环

python的内置容器有: 列表,元组,字典,集和,

>>> a= [1,4,5,‘lo‘] ##定义一个列表 列表中的元素没有要求 且列表可以嵌套
>>> type(a) ##type() 查看类型
<type ‘list‘>

range() 得到一个列表
>>> range(0,2)
[0, 1]
>>> dir(a) ##可以查看对于一个容器能够执行的操作
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__delslice__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getslice__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__setslice__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]

>>> help (a.count) ##查看帮助
>>> a.append(3)
>>> a
[1, 3, [1, 2, 3], 3]
>>> a.count(3) ##统计某个数在列表中出现的次数
2
>>> a.extend(b) ##将两个容器追加到一个当中
>>> a
[1, 3, [1, 2, 3], 45, 3, ‘hello‘, ‘world‘, 23]

>>> b.insert(2,45) 在所个下标前插入所给的数
>>> b
[‘hello‘, ‘world‘, 45, 23]

>>> b.index(23) 返回该数的下标
3

>>> b.remove(45) 删除一个数
>>> b
[666, ‘hello‘, ‘world‘, 23]


>>> b=[1,23,24,13,24,44,] ##pop 删除一个下标为几的数 并将该数返回
>>> b.pop()
44
>>> b
[1, 23, 24, 13, 24]
>>> b.pop(2)
24
>>> b
[1, 23, 13, 24]

>>> b.reverse() 将列表中的数逆序显示出
>>> b
[24, 13, 23, 1]

>>> b.sort() 将列表中的数按照大小排列出来
>>> b
[1, 13, 23, 24]
>>> 23 in a ##判断某一个元素在列表中
True

>>> a
[1, 3, ‘lo‘, [1, 2, 3]]
>>> len(a) ##得到列表长度
4
>>> del a[2] ##del 删除列表中下表为几的元素
>>> a
[1, 3, [1, 2, 3]]


列表切片: ##列表切片不会改变列表的本身,
li[a:b:c] 从下标a开始,到下标b结束,但不会包含b,c表示为布长
>>> a
[1, 3, [1, 2, 3], 45, 3, ‘hello‘, ‘world‘, 23]

>>> a[1:7]
[3, [1, 2, 3], 45, 3, ‘hello‘, ‘world‘]

>>> a[:6]
[1, 3, [1, 2, 3], 45, 3, ‘hello‘]

>>> a[:: -1] ##实现逆序显示一个列表:
[23, ‘world‘, ‘hello‘, 3, 45, [1, 2, 3], 3, 1]

###
>>> a
[1, 3, [1, 2, 3], 45, 3, ‘hello‘, ‘world‘, 23]
>>> a1=a[:]
>>> a[2][1]=100
>>> a
[1, 3, [1, 100, 3], 45, 3, ‘hello‘, ‘world‘, 23]
>>> a1
[1, 3, [1, 100, 3], 45, 3, ‘hello‘, ‘world‘, 23]
#######注意: 列表赋值相当于引用 它与切片赋值不一样
>>> l2
[4, 5, 6]
>>> l5=l2
>>> id(l2)
11948832
>>> id(l5)
11948832
>>> l6=l2[:]
>>> id(l6)
11948976




元组: 元组是不可改变的对象,不能对元组的内容作修改。
>>> c=(1,23,45)
>>> type(c)
<type ‘tuple‘>

根据元组的不可改变的性质,元组只能执行:count,index 的操作,且元组支持下标和切片。
>>> c
([1.23, 34, ‘hello‘], [34, 54])
>>> c[0][2]=230
>>> c
([1.23, 34, 230], [34, 54])
表明了元组的不可改变是相对的。

集和:
>>> c
set([32, 1, 2, ‘kitty‘, 21]) ##定义一个集和
>>> type(c)
<type ‘set‘>

>> a = set() ##定义一个空集和
>>> type(a)
<type ‘set‘>

>>> c={} ##定义出来的为字典
>>> type(c)
<type ‘dict‘>


集和保持着数学中的集和性质: 无序性, 唯一性,
>>> d={1,3,1,2,‘kitty‘,23,1,‘hello‘}
>>> d
set([1, 2, 3, ‘kitty‘, 23, ‘hello‘])
集和不支持切片
>>> c.add(100) ##集和元素的添加
>>> c
set([32, 1, 2, 100, ‘kitty‘, 21])
>>> c.update([12,‘world‘,78]) ##增加一个集和
>>> c
set([32, 1, 2, 100, 12, ‘kitty‘, 78, 21, ‘world‘])


>> c.remove(12) ##删除集和中一个元素 但该元素在集和中不存在时报错
>>> c
set([32, 1, 2, 100, ‘kitty‘, 78, 21, ‘world‘])
>>> c.remove(43)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 43

>>> c.discard(43) ##删除一个元素,没有该元素时不执行任何操作。
>>> c
set([32, 1, 2, 100, ‘kitty‘, 78, 21, ‘world‘])

>>> c.pop() ##随机的删除集和中的某个元素。
2
>>> c ##清除某个集和
set([100, ‘kitty‘, 78, 21, ‘world‘])
>>> c.clear()
>>> c
set([])
集和的运算:
差集:difference ##求出差集 a-b
difference_update ##求出差集,但修改了原来的集和,不返回值
>>> c
set([‘world‘, 2, 1, 45, 23])
>>> d
set([1, 2, 3, ‘kitty‘, 23, ‘hello‘])
>>> c.difference(d)
set([‘world‘, 45])
>>> c.difference_update(d)
>>> c
set([‘world‘, 45])

交集:intersection
intersection_update
&
>>> c.intersection(d)
set([1, 2, 23])
>>> c.intersection_update(d)
>>> c
set([1, 2, 23])
>>> c&d
set([1, 2, 23])

并集:union |
>>> c.union(d)
set([1, 2, 3, ‘kitty‘, 45, 23, ‘world‘, ‘hello‘])
>>> c|d
set([1, 2, 3, ‘kitty‘, 45, 23, ‘world‘, ‘hello‘])

判断是否有交集:isdisjoint 查看两个集和是否有交集,返回的为布尔值
>>> c
set([‘world‘, 2, 1, 45, 23])
>>> d
set([1, 2, 3, ‘kitty‘, 23, ‘hello‘])
>>> c.isdisjoint(d)
False
>>> e={‘lll‘,666}
>>> c.isdisjoint(e)
True

容器之间的转换:
元组与列表:
>>> t=(1,2,3)
>>> type(t)
<type ‘tuple‘>
>>> list(t)
[1, 2, 3]
>>> t1.append(24)
>>> t1
[1, 2, 3, ‘hello‘, 24]
>>> t
(1, 2, 3, ‘hello‘)



>>> t=tuple(t1)
>>> t1
[1, 2, 3, ‘hello‘, 24]


列表与集和的转换
>>> l=[1,22,23,‘hello‘]
>>> a=set(l)
>>> a
set([1, ‘hello‘, 22, 23])
>>> a.add(45)
>>> a
set([1, 45, ‘hello‘, 22, 23])
>>> l
[1, 22, 23, ‘hello‘]
>>> l=list(a)
>>> l
[1, 45, ‘hello‘, 22, 23]
>>> p=set(t)
>>> p
set([24, 1, 2, 3, ‘hello‘])

set,list,tuple,三个函数可以转换,但是由于集和的性质,在list或tuple转换成set时,有重复的元素,则进行删除。

字典:
d=dict(); d=("key":"value")
d 中key值是可hash 的。key是唯一的
>>> d
{‘age‘: 100, ‘name‘: ‘tom‘}

>>> d.keys()
[‘age‘, ‘name‘]

>>> d.values()
[100, ‘tom‘]

>>> it= d.iterkeys() iterkeys 将字典的所有key作为一个迭代器返回
>>> it.next()
‘age‘
>>> it.next()
‘name‘

>>> it2=d.items() items返回一个列表, 并用元组的形式成对输出
>>> it2
[(‘age‘, ‘23‘), (‘name‘, ‘tom‘)]

>>> for i in d.keys():
... print i,d[i]
...
age 23
name tom

>> for k,v in d.items():
... print k, v
...
age 23
name tom
>>> d[‘sex‘]=2 ###给字典增加一个值
>>> d
{‘age‘: ‘23‘, ‘name‘: ‘tom‘, ‘sex‘: 2}

#############注意:引用传值与复制的区别:
>>> d
{‘age‘: ‘23‘, ‘name‘: ‘tom‘, ‘sex‘: 2}
>>> d1=d
>>> d1[‘age‘]=66
>>> d1
{‘age‘: 66, ‘name‘: ‘tom‘, ‘sex‘: 2}
>>> d
{‘age‘: 66, ‘name‘: ‘tom‘, ‘sex‘: 2}
>>> d2=d.copy()
>>> d2
{‘age‘: 66, ‘name‘: ‘tom‘, ‘sex‘: 2}
>>> d2[‘age‘]=99
>>> d2
{‘age‘: 99, ‘name‘: ‘tom‘, ‘sex‘: 2}
>>> d
{‘age‘: 66, ‘name‘: ‘tom‘, ‘sex‘: 2}
>>> id(d)
9777488
>>> id(d1)
9777488
>>> id(d2)
10605344

列表解析:
>>> li=[1, 2, 3, 4, 5]
>>> li1=[ i+1 for i in li]
>>> print li1
[2, 3, 4, 5, 6]


>>> l1=[1,2,3]
>>> l2=[4,5,6]
>>> l3=(i+1 for i in l1)
>>> l3
<generator object <genexpr> at 0x9b60f0> ##返回的是一个迭代器
>>> l3.next()
2
>>> l3.next()
3
>>> l3.next()
4
迭代器(iterator):执行一次给一次结果《被动的》(他不相当于循环《主动的》) 它是一个惰性的



本文出自 “11715322” 博客,请务必保留此出处http://11725322.blog.51cto.com/11715322/1788970

python 基础篇(一)

评论关闭