Python chapter 2&3 learning notes

方法是Python对数据执行的操作。

For example,

Upper() 全部大写

Lower() 全部小写

Title() 首字母大写

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

当在字符串中需要使用数字的时候,应在该数字的变量名前使用str()以避免错误

For example,

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

And your code will be

a=22# assumption

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

There are the correct codes.

 

About Python List

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.

·        add element:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值