Python chapter 2&3 learning notes

简介: 版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243401 方法是Python对数据执行的操作。
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243401

方法是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.

相关文章
|
6月前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
|
3月前
|
SQL 机器学习/深度学习 开发工具
【机器学习 Azure Machine Learning】Azure Machine Learning 访问SQL Server 无法写入问题 (使用微软Python AML Core SDK)
【机器学习 Azure Machine Learning】Azure Machine Learning 访问SQL Server 无法写入问题 (使用微软Python AML Core SDK)
|
6月前
|
Linux Apache Python
Python3 notes
Python3 notes
|
机器学习/深度学习 数据采集 数据挖掘
【Deep Learning 框架】Python中各类框架区别
当我们在学习深度学习的时候,往往会看到Sklearn、Pytorch、Tensorfloiw、Keras等各种各样的框架平台,看得我们眼花缭乱,这些框架平台究竟是干什么用的呢?🤔
132 0
|
索引 Python
|
安全 Python
Python3 notes
Python3 notes
|
Linux Python
Python3 notes
Python3 notes