Python常用文法(上)
14196 ワード
【要旨】Pythonはシンプルな言語として、プログラミング業界でも一席を占めています.C++を勉強した後、Pythonのよくある文法を適切に勉強するのもいいです.今、最近学んだPythonの知識点を以下にまとめてみます.多いかもしれませんが、ゆっくり見てください.これは主にもっと練習しなければなりません.
オペレータ'/'は従来の除算法であり、2つの整数が除算されても結果は整数であり、そのうちの1つの数が浮動小数点数である場合、得られた結果も浮動小数点数 である.'//'床除は結果の小数点以下を に切り捨てる. from__future__import division精密除算 **乗算 Pythonのサイズ比較で得られたbool値 文字列間の比較は、ディクショナリ順序 に依存する.
printf
raw_input =
name = raw_input("Enter name:")
raw_input , , int
オペレータ
, ,
- , , ,
- KV
- , ,
- ‘+’ ,‘*’
-
- type(a)
- * 100**100 100 100
- python bool
- ” “
- ,
- 100, ,
- python
- if elif else if
-
- for
- range , for
- pass , python ,
squared = [x ** 2 for x in range(4)]
print squared [0,4]
evens = [x for x in range(0,8)if x%2 == 1] (0,8)
- python , ,
- python ,
- python (unpack),
- _,y
- , , ,
handle = open(file_name,access_mode = 'r') //file_name
handle , , for ,handle , close , ( )
, .py
- import , .py
- , ” “
- , .py ,
- , , python
python
- ,
- : ( )
- : ,
x,y = 1,2
x,y =10,20
x,y = y,x
- python ,def,class,lamda
- if,else,elif,while,for,try/catch
- globals() , locals()
- python ,python ,
- python (_) ,
- _xxx ‘ ’, from module import *
- xxx( ) xxx( ) , ,
- #
- , ,
- doc (print Add.doc)
- help
- / , doc help
- ,
- ,
- ,
- , ,
- print type(a) == types.IntType
- print isinstance(a,type(100))
python
- char byte: 1 ,
- :python , id() ,
- int/short/long:python ,
- divmod : ,
- str :
- round: ,round , ,
- :oct(),hex() ,
- math/cmath : ,math ,camath
smaller = x if x < y else y
- , , ,
- ,
- python , , ,
- , . ,
- , , ,
- ,
- , , , sorted( ),
sorted(iterable[,cmp[,key[,reverse]]])
, ( , , ), ,
def Cmp(x, y):
if abs(x) < abs(y):
return -1
elif abs(x) > abs(y):
return 1
else:
return 0
a = [1, -3, 4, 2]
print sorted(a, cmp = Cmp)
a = ['aaaa', 'bbb', 'cc', 'd']
print sorted(a, key = len)
, , . " "
* , * .
\t , linux . cut, sort, awk .
**, .
- Python , , .
- , .
- , , C++ .
- return1
- return , None
- N , ( )
- , dir
- ,
python
- : , , , ,Python
- , ,
- in/not in: ,
- (+):
- ( , ), , extend , , join
a = "adsafsf"
print a[::-1]
len max min sorted
enumerate:
def Find(input_list, x):
for i in range(0, len(input_list)):
if input_list[i] == x:
return i
else:
return None
- zip
- dict
- , 1 , t R ,
- repr str
- repr , Python , Python ,
- join,
//
a = 'aa bb cc dd'
print a.split(' ')
//
a = 'hello world'
print a.startswith('hello')
print a.endswith('world')
// /
a = ' hello world'
print a.strip()
//
a = 'hello world'
print '[' + a.ljust(30) + ']'
print '[' + a.rjust(30) + ']'
print '[' + a.center(30) + ']'
//
a = 'hello world'
print a.find('world')
//
a = 'hello world'
print a.replace('world', 'python')
// /
a = 'hello world'
print a.isalpha()
a = '1234'
print a.isdigit()
//
a = 'Hello World'
print a.lower()
print a.upper()
-
-
- append: ,del: , :remove,extend:
- sort , O(N*logN)
-
- (=), ([:]), list , .
- , , ,
.
- , , , ,
.
- copy.deepcopy
- , ( ),
- {}, dict, fromkeys
- [] / , key , , ,
- clear ,
- pop , ,
- hash hash, hash, hash hashnode
(set)
- , ,
a = set([1,2,3])
b = set([1,2,3,4])
print a & b //
print a | b //
print b - a //
print a ^ b //
a = [1,2,1,2,3,4,4]
b = set(a)
print b