# Python chapter 2&amp;3 learning notes

+关注继续查看

For example,

Upper() 全部大写

Lower() 全部小写

Title() 首字母大写

rstrip() 删除多余空格（此时的删除都是暂时的，非永久。如需永久需要重新幅值给原变量）

For example,

If you want output this sentence "I am 22 years old."

a=22# assumption

print("I am " + str(a) + "years old.")#Either "" or '' is ok.

There are the correct codes.

Some skills:

1.      if you want to output the last element in the list, you can use this code:

list[-1]#in this place, list means the name of this list

Similarly, if you want to input the second to last number, you can use:

list[-2]

2.      some methods to modify, add or delete elements.

·        modify the element:

sights[0]='Beijing' means change the first element in the list sights to Beijing.

sights.append('Beijing') means add Beijing to the last of the list sights, sights.

insert(1, 'Beijing') means add Beijing to the place between the first element and the third element.

·        delete element:

del sights[0] means delete the first element  from the list.

poped_sight=sights.pop() means we can still use it when we delete the last element.

One thing to be noted, if we use poped_sight=sights.pop(1), it means that we still use the second element when we delete it.

sights.remove('Beijing') means delete the Beijing from the list. (If there are many Beijing in the list, this method only can delete the first Beijing.)

3.      Sort:

·        sort & alphabetically forever: use sort()

For example, sight.sort().

If we use sight.sort(reverse =True), it means reverse order.

·        temporary sort: use sorted() function.

For example, print(sorted(sights)).We can also use sorted(sights, reverse = True) to reverse order.

·        reverse order print: sights.reverse()

·        How to know the length of the list? The answer is use len() function.

9417 0

2915 0

11161 0

10813 0

12016 0
windows server 2008阿里云ECS服务器安全设置

9027 0

4616 0

6876 0

21870 0
+关注

27

0

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》