Python str list tuple dict内蔵関数の使い方
6300 ワード
1. str
2. list
3. tuple
4. dict
5. math
Python
,python (\) 。 :
\( )
\\
\'
\"
\a
\b (Backspace)
\e
\000
\v
\t
\r
\f
\oyy ,yy , :\o12
\xyy ,yy , :\x0a
\other
Python
a "Hello",b "Python":
+
>>>a + b
'HelloPython'
*
>>>a * 2
'HelloHello'
[]
>>>a[1]
'e'
[ : ]
>>>a[1:4]
'ell'
in - True
>>>"H" in a
True
not in - True
>>>"M" not in a
True
r/R - : , 。 "r"( ) , 。
>>>print r'
'
>>> print R'
'
%
2. list
Python :
1 cmp(list1, list2)
2 len(list)
3 max(list)
4 min(list)
5 list(seq)
Python :
1 list.append(obj)
2 list.count(obj)
3 list.extend(seq)
( )
4 list.index(obj)
5 list.insert(index, obj)
6 list.pop(obj=list[-1])
( ),
7 list.remove(obj)
8 list.reverse()
9 list.sort([func]) sorted()
3. tuple
, + * 。 , 。
Python
len((1, 2, 3)) 3
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6)
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!')
3 in (1, 2, 3) True
for x in (1, 2, 3): print x, 1 2 3
Python
1 cmp(tuple1, tuple2)
。
2 len(tuple)
。
3 max(tuple)
。
4 min(tuple)
。
5 tuple(seq)
。
4. dict
&
Python :
1 cmp(dict1, dict2)
。
2 len(dict)
, 。
3 str(dict)
。
4 type(variable)
, 。
Python :
1 dict.clear()
2 dict.copy()
3 dict.fromkeys(seq[, val]))
, seq ,val
4 dict.get(key, default=None)
, default
5 dict.has_key(key)
dict true, false
6 dict.items()
( , )
7 dict.keys()
8 dict.setdefault(key, default=None)
get() , , default
9 dict.update(dict2)
dict2 / dict
10 dict.values()
11 pop(key[,default])
key , 。key 。 , default 。
12 popitem()
。
5. math
1.math
:
>>> import math
>>>dir(math) #
>>>help(math) # 0
2.
:
ceil(x)
floor(x)
fabs(x)
factorial (x)
hypot(x,y) sqrt(x*x+y*y)
pow(x,y) x y
sqrt(x)
log(x)
log10(x)
trunc(x)
isnan (x) NaN(not a number)
degree (x)
radians(x)
:
:
e = 2.718281828459045
pi = 3.141592653589793
random
1.
random ,
:
import random
2.
random.random()
:range[0.0,1.0)
:
>>> import random
>>> random.random()
0.999410896951364
random.uniform(a,b)
,a,b
a!=b, , a=b, a
:
>>> random.uniform(10,20)
13.224754825064881
>>> random.uniform(20,10)
14.104410713376437
>>> random.uniform(10,10)
10.0
random.randint(a,b)
,a ,b , a<=n<=b;
a=b, n=a; a>b,
:
>>> random.uniform(10,10)
10.0
>>> random.randint(10,20)
15
>>> random.randint(10,10)
10
>>> random.randint(20,10)
Traceback (most recent call last):
……
ValueError: empty range for randrange() (20,11, -9)
random.randrange([start], stop, [,step])
, , 1
:
>>> random.randrange(10,100,5)
95
>>> random.randrange(10,100,5)
45
random.choice(sequence)
, sequence , , list,tuple,
:
>>> random.choice([1,2,3,4])
1
>>> random.choice([1,2,3,4])
3
>>> random.choice('hello')
'e'
random.shuffle(x[, random])
:
>>> a = [1,2,3,4,5]
>>> random.shuffle(a)
>>> a
[4, 5, 2, 1, 3]
>>> random.shuffle(a)
>>> a
[3, 2, 5, 1, 4]
random.sample(sequence, k)
k ,sample
:
>>> a = [1,2,3,4,5]
>>> random.sample(a,3)
[1, 4, 5]
>>> random.sample(a,3)
[1, 2, 5]
>>> a
[1, 2, 3, 4, 5]
decimal
1.
,
decimal Decimal 。 float
,
,
,
, 。
Decimal , 。 Decimal 。
2.
:
>>> from decimal import Decimal
>>> Decimal('0.1') / Decimal('0.3')
Decimal('0.3333333333333333333333333333')
>>> from decimal import getcontext
>>> getcontext().prec = 4 #
>>> Decimal('0.1') / Decimal('0.3')
Decimal('0.3333')
fractions
:
>>> from fractions import Fraction
>>> Fraction(16, -10) #
Fraction(-8, 5)
>>> Fraction(123) #
Fraction(123, 1)
>>> Fraction('3/7') #
Fraction(3, 7)
>>> Fraction('-.125') #
Fraction(-1, 8)
>>> Fraction(2.25) #
Fraction(9, 4)
>>> from decimal import Decimal
>>> Fraction(Decimal('1.1')) #Decimal
Fraction(11, 10)
:
>>> from fractions import Fraction
>>> a = Fraction(1,2)
>>> a
Fraction(1, 2)
>>> b = Fraction('1/3')
>>> b
Fraction(1, 3)
>>> a + b
Fraction(5, 6)
>>> a - b
Fraction(1, 6)