python 一些魔法

1. range(object):

    """
range(stop) -> range object
range(start, stop[, step]) -> range object

Return an object that produces a sequence of integers from start (inclusive)
to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
These are exactly the valid indices for a list of 4 elements.
When step is given, it specifies the increment (or decrement).
a = 'TjkdFGklfsaYYY'
for i in range(0,len(a)): #range(0,len(a))效果一样
    print(i,a[i])
___________________
0 T
1 j
2 k
3 d
4 F
5 G
6 k
7 l
8 f
9 s
10 a
11 Y
12 Y
13 Y

 加入range()步长

a = input(".....")
for i in range(0,len(a)):
    print(i,a[i])
————————
.....djklajs k
0 d
1 j
2 k
3 l
4 a
5 j
6 s
7  
8 k
a = input(".....")
for i in range(0,len(a),3):
    print(i,a[i])
______________
.....jdkfskjkfdsl
0 j
3 f
6 j
9 d

  

  

2.  len(*args, **kwargs): # real signature unknown

    """ Return the number of items in a container. 
a1 = 'TjkdFGklfsaYYY'
a2 = [1,2,3,4,5,'jflkas']
b1 = len(a1)
b2 = len(a2)
print(b1,b2)
------------------
14 6

3. 脚标

a = 'TjkdFGklfsaYYY'
index = 0
while index < len(a):
    b = a[index]
    print(b)
    index += 1
____________________
T
j
k
d
F
G
k
l
f
s
a
Y
Y
Y

  

a1 = 'TjkdFGklfsaYYY'
a2 = [1,2,3,4,5,'jflkas']
print(a1[2], a2[3])

————————————
k 4

猜你喜欢

转载自www.cnblogs.com/jijianhu/p/10307952.html