Python outputs all uppercase and lowercase letters, range (), and list slices

Source: Internet
Author: User
Tags function prototype ord

So when writing, just list them in ASCII and convert them into character Chr.

print [chr(i) for i in range(65,91)]#所有大写字母print [chr(i) for i in range(97,123)]#所有小写字母print [chr(i) for i in range(48,58)]#所有数字

Another way to do this is to use the string method in Python.

#-*- coding:utf-8 -*-import string #导入string这个模块print string.digits  #输出包含数字0~9的字符串print string.letters #包含所有字母(大写或小写)的字符串print string.lowercase #包含所有小写字母的字符串print string.uppercase #包含所有大写字母的字符串print string.punctuation #包含所有标点的字符串print string.ascii_letters #与string.letters一样

Output:

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&‘()*+,-./:;<=>[email protected][\]^_`{|}~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ





Range () and list slices

The range () function is handy for working with loops, so let's tidy up the main usage of this function today, and a few small examples

First look at the function prototype, very simple:
Range (Start,end [, step])
Or even start is omitted:
Range (end)

Where start indicates the start point, end represents the end point, the real end position is the previous value of end, and step represents the step.
For example:

print(list(range(1,5)))#list函数是将range的值列表化,这时候的输出为 1,2,3,4 (没有5!即不包括最后一个end的值)print(list(range(1,5,2)))#输出结果是 [1, 3] ,步长为2,而且小于5

If only one parameter is passed in:

print(list(range(5)))#输出结果是 [0, 1, 2, 3, 4] ,这时候默认从0开始

For these uses of rang, we can combine the traversal of the same list, such as traversing a list of accesses:

#首先我用一个range生成一个列表,其中包含0~9 10个元素list1 =list(range(10))# #使用range的方式进行遍历:for i in range(len(list1)):    print(list1[i])#当然这种情况我们一般都是直接使用 for value in list1 就可以了#如果使用切片,访问列表的一部分for i in range(int(len(list1)/2)): print(list1[i])#这样我们就可以得到列表中前一半元素,其他部分同理

Sometimes we use a list in reverse order, using range to do this:

list1 =list(range(10))for i in range(len(list1)-1,0,-1): print(list1[i])#注意这个地方开始的位置是len-1,因为我们使用的列表的下标,其实这里的使用与C语言是一致的,步长设置为-1#就相当于我们在for循环的 i--了

Using loops, we can do some work on the data in each loop, but if we just get this data, we can use the list of slices slide to do, slicing is very flexible, such as relatively simple:

list1 =list(range(10))print(list1[:])# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]print(list1[3:])# [3, 4, 5, 6, 7, 8, 9]print(list1[3:5])# [3, 4]print(list1[1:5:2])# [1, 3]

From here we can see that the use of slices and range is similar, are three parameters, respectively, control the start, end and step, in the range we can use-1, then in the same way, in the slice, the same, for each list item, not only a positive ordinal, there is a reverse ordinal, The last element can be expressed using list1[-1], the penultimate element is List1[-2] ....:

print(list1[1:-1])# 这里表示从第一个元素到最后一个元素
    • 1
    • 2

If output in reverse order:

print(list1[-1::-1])#或者简记为:print(list1[::-1])
    • 1
    • 2
    • 3

Here's a question I've encountered before, how do I save a-Z or z-a in a list?

list2 = []for i in range(ord(‘A‘),ord(‘A‘)+26): list2.append(chr(i))print(list2)list3 = [chr(ord(‘A‘)+x) for x in range(26)]print(list3)

Both of these methods can generate a A-Z list, and for this problem, if you have used other languages, you might first use INT (' a ') to get the ASCII of a, but in Python it will be an error because the Int () function is the default base and decimal, and for the letters to numbers, There is the function of Ord can be used, there are letters converted back to ASCII can use the Chr () function, the second way is to use the form of list generation, it is relatively simple to write, in addition to these methods, you can also use the map () function to the list of each value of the processing, no longer described here.
end~

 

Python outputs all uppercase and lowercase letters, range (), and list slices

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.