Len (), range () function

Source: Internet
Author: User

The Len () function returns the length of a string, a list, a dictionary, a tuple, and so on

EG1: Calculates the length of the string:

>>>s= ' Hello good boy Doiido '

>>>len (s)

>>>21

EG2: Count the number of elements in the list:

name=[' Zhu ', ' Han ', ' shi '] >>>l=[' h ', ' e ', ' l ', ' l ', ' O ']

AGE=[21,22,23] >>>len (l)

For I in range (len (name)): 5

Print Name[i], ' is ', age[i], ' years '

Output:

Zhu is yaer old

Han is-yaer old

Shi is at Yaer old

EG3: Calculates the total length of the Dictionary: (calculates the total number of key-value pairs)

d={' x ': 1, ' Y ': 2, ' Z ': 3}

Len (d)

3

EG4: Calculating the number of tuple elements

t= (' g ', ' o ', ' o ', ' d ')

Len (t)

4

Range () function:

Eg1:range (1,5) #从1到5 (not including 5)

[1,2,3,4,5]

Eg2:range (1,5,2) #从1到5, Interval 2 (not including 5)

[1,3]

Eg3:range (5) #从0到5 (not including 5)

[0,1,2,3,4]

Operation of List:

array=[1,2,5,3,6,8,4]

#array的顺序标志是:

(0,1,2,3,4,5,6)

( -7,-6,-5,-4,-,-2,-1)

Then: >>>array[0:] #列出0以后得

[1,2,5,3,6,8,4]

>>>array[1:] #列出1以后得

[2,5,3,6,8,4]

>>>ARRAY[:-1] #列出-1 ago

[1,2,5,3,6,8]

>>>array[3:-3] #列出3到-3

[3]

Len (), range () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.