Python chapter 4 learning notes

简介: 版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243661 1.
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243661

1.      About loop

·        Indentation means these sentences or this sentence  belong(s) to this loop.Such as,

magicians=['david','joe','kite','john']

for magician in magicians:# loop

print(magician.title() + ', you have been invited.')

print("I can't wait to see your next trick, "+ magician.title() + '\n')

·        After loop, a non-indented sentence means it doesn't belongs to this loop.

·        DO NOT FORGET THE":" AFTER YOU WRITE THE FOR xxx

2.      About numerical list

·        Use range()

o   If you want to print one to five, you need to use range(1,6).DO NOT FORGET IT.

For example,

for value in range(1,6):

print(value)

range(a,b,m), a means the start point, b means the end point, and m means step length.

For example,

o  After you input range(1,6,2), you can see [1,3,5].

o  Use range() and list() to make up a list

For example,

numbers=list(range(1,11))

print(numbers)

If you use list(range(1,11,2)), you can see [1,3,5,7,9].

power operation just like a**2,using **.

·        Some simple functions

·        min(digits) means figure out the mnimum.

·        max(digits) means figure out the max number.

·        sum(digits) means figure out the sum.

·        List Comprehensions(列表解析)

For example,

data=[valu**3 for valu in range(2,12,2)]

print(data)

Using part of list: Slice

·        Some ways:

o   print str[0:3] #截取第一位到第三位的字符

o   print str[:] #截取字符串的全部字符

o   print str[6:] #截取第七个字符到结尾

o   print str[:-3] #截取从头开始到倒数第三个字符之前

o   print str[2] #截取第三个字符

o   print str[-1] #截取倒数第一个字符

o   print str[::-1] #创造一个与原字符串顺序相反的字符串

o   print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符

o   print str[-3:] #截取倒数第三位到结尾

o   print str[:-5:-3] #逆序截取

·        Copy list:

If we want to copy the elements of one list to another list, we need to do as followed:

my_foods = ['apple','banana','cake']

friend_foods = my_foods[:]#如果没有[:]那就意味着只是将my_foods的地址指向friend_foods,这样的话,如果更改前者元素后者也会更改

print(my_foods)

print(friend_foods)

my_foods.append("water melon")

friend_foods.append("monga")

print(my_foods)

print(friend_foods)

We can see that firend_foods is different from my_foods.

Tuple(元组)

list is unchangeable, so sometime we need a tuple. Tuple is changeable.

define a tuple like this: dimensions=(200,50). We do not use [].

The elements of tuple cannot be changed, but we can assign.

In addition , tuple is as same as list.

相关文章
|
7月前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
|
4月前
|
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)
|
7月前
|
Linux Apache Python
Python3 notes
Python3 notes
|
机器学习/深度学习 数据采集 数据挖掘
【Deep Learning 框架】Python中各类框架区别
当我们在学习深度学习的时候,往往会看到Sklearn、Pytorch、Tensorfloiw、Keras等各种各样的框架平台,看得我们眼花缭乱,这些框架平台究竟是干什么用的呢?🤔
142 0
|
索引 Python
|
安全 Python
Python3 notes
Python3 notes
|
Linux Python
Python3 notes
Python3 notes