Life is short, I used Python (2)

1, for loop iterates string:

String = " Life is short, I used Python " 
Print ( String )
 for CH in  String : 
    Print (CH)
        

  

  It may also be used for loop iteration (traversing) a list of tuples, sets and dictionaries.

2, the nested loop:

  (1) while circulating apply formatting while loop:

while Conditional Expression 1:
     while the conditional expression 2: 
        loop 2 
    loop 1

  (2) apply for loop format for loop:

for iteration variable 1 in target 1:
     for the iteration variable 2 in target 2: 
        loop 2 
    loop 1

  (3) while loop for loop format applies:

while conditional expression:
     for the iteration variable in the object: 
        the loop 2 
    loop 1

  (4) for applying the format loop while loop:

for the iteration variable in the object:
     the while conditional expression: 
        loop 2 
    loop 1

3, for circulation to achieve 9 * 9 multiplication table:

for i in range(1,10):
    for j in range(1,I+1):
        print(str(j)+"*"+str(i)+"="+str(i*j)+"\t",end=' ')
    print()

  Cycle process:

4, a jump statement:

  If you want a for loop or while loop is repeated until the end to find out of the loop until the end of the condition, there are two ways:

  The next iteration (1) continue to use the statement to jump directly to the loop.

  (2) the use of completely break off cycle.

  The break statement can terminate the current cycle, including while and for including all control statements. break statement is usually carried out in conjunction with the use of an if statement, expressed under certain conditions, out of the loop. If a nested loop, break out of the innermost loop statement.

5, break statement while statement and for the use of:

  Break statement is used in the form of (1) while statement:

while Conditional Expression 1: 
   executing code 
    if the conditional expression 2: # 2 The conditional expression for determining when to invoke the break statement out of the loop.
        break  

  (2) use the break statement for statement form:

for the iteration variable in the object:
     IF conditional expression:
         BREAK

  

6, continue in a while and for statements of use:

  (1) while using the statement continue statement of the form:

while Conditional Expression 1: 
    executing code 
    if the conditional expression 2: # 2 The conditional expression for determining when to invoke the continue statement out of the loop.
        continue

  (2) use continue statement for statement form:

for the iteration variable in the object:
     IF conditional expression:
         the Continue

 

7, Pass empty statement

  pass represents the empty statement, it does not do anything, generally played the role of placeholder.

for i in range(1,10):
    if i%2 ==0:
        print(i,end=' ')
    else:
        pass

第四章:序列的应用

  1、序列在数学中成为数列。在Python中序列是最基本的数据结构。它是一块用于存放多个值的连续内存空间。并且按一定顺序排列,每一个值(称为元素)部分配一个数字,成为索引或位置,通过索引可以取出相应的值。Python中内置了5个常用的序列结构,分别是列表、元组、集合、字典和字符串。其中,集合和字典不支持索引、切片、相加和相乘操作。

  2、序列的应用:

(1)序列概述:
            索引
            切片
            序列相加
            乘法
            检查某个元素是否是序列的成员(元素)
            计算序列的长度、最大值和最小值
(2)列表(list)
            列表的创建和删除
            访问列表元素
            遍历列表
            添加、修改和删除列表元素
            对列表进行统计计算
            对列表进行排序
            列表推导式
            二位列表
(3)元组(tuple)
            元组的创建和删除
            访问元组元素
            修改元组
            元组推导式
            元组与列表的区别
(4)字典(dictionary)
            字典的创建和删除
            访问字典
            遍历字典
            添加、修改和删除字典元素
            字典推导式
(5)列表、元组、字段和集合的区别
(6)集合(set)
            创建集合
            集合的添加和删除        
            集合的交集、并集和差集运算

  3、序列中的每一个元素都有一个编号,也称为索引,这个索引是从0开始递增的,即下标为0表示第一个元素,下标为1表示第2个元素,以此类推。

    Python中的索引可以是负数,从右向左计数,从 -1 开始而不是从 0 开始。即最后一个元素的索引值是 -1

verse=["python","Linux","Java","MySQL"]
print(verse[0])
print(verse[1])
print(verse[-1])

  -1 表示的是最后一个元素,即输出最后的MySQL。

 4、切片:

  切片操作时访问序列中元素的另一种方法,它可以访问一定范围内的元素。通过切片操作可以生成一个新的序列,切片操作的语法格式如下:

sname[start:end:step]
#sname:表示序列的名称。
#start:表示切片的开始位置(包括该位置),如果不指定,则默认为0。
#end:表示切片的截至位置(不包括该位置),如果不指定,则默认为序列的长度。
#step:表示切片的步长,如果省略,则默认为1,当省略步长时,最后一个冒号也可以省略。

  在切片操作中,如果指定了步长,那么将按照该步长遍历序列的元素,否则将一个一个遍历序列。

nba=["科比","乔丹","詹姆斯","奥尼尔","伯德"]
print(nba[0:4:2])

  输出真个序列,start和end都可以省略,但是要保留一个冒号---- nba[:]

5、序列相加:

  在Python中,支持两种相同类型的序列相加操作,即将两个字序列进行连接,不会去除重复的元素,使用  +  运算符实现。

nba1=["麦迪","韦德","詹姆送"]
nba2=["科比","乔治","字母哥"]
print(nba1+nba2)

  在进行序列相加时, 相同类型的序列是指同为列表,元组,集合等,序列中的元素类型可以不同。

nba1=["麦迪","韦德","詹姆送"]
nba2=["科比","乔治","字母哥"]
num=[12,23,45]
print(nba1+nba2+num)

   但是不能将列表和元组相加,也不能将列表和字符串相加。

 6、乘法:

  在Python中,使用数字 n 乘以一个序列会生成新的序列。新序列的内容为原来序列被重复 n 次的结果。

phone=["Mate10","Vivo X21"]
print(phone * 3)
phone=["Mate10","Vivo X21"]*3
print(phone)

7、检查某个元素是否是序列的成员:

  在Python中,可以使用 in 关键字检查某个元素是否为序列的成员,即检查某个元素是否包含在某个序列中。语法格式如下:

value in sequence
#value表示要检查的元素
#sequence表示指定的序列
phone=["mate","ipad","vivo"]
print("oppo" in phone)
print("vivo" in phone)

  也可以使用  not  in 来判断元素是否不包含在指定的序列中。

phone=["mate","ipad","vivo"]
print("oppo" not in phone)

8、计算序列的长度、最大值和最小值

  在Python中,提供了内置函数计算序列的长度、最大值和最小值。

使用len()函数计算序列的长度,即返回序列包含多少个元素;

使用max()函数返回序列中的最大元素;

使用min()函数返回序列中的最小函数。

num=[12,23,43,45,54,67]
print("序列num的长度为",len(num))
print("序列num的最大值为",max(num)
print("序列num的最小值为",min(num))

9、Python提供的内置函数及其作用

list()              将序列转换为列表
str()               将序列转换为字符串
sum()               计算元素和
sorted()            对元素进行排序
reversed()          反向序列中的元素
enumerate()         将序列组合为一个索引序列,多用在for循环中

10、列表:

  在内容上可以将整数、实数、字符串、元组等任何类型的内容放入列表中,并且同一个列表中,元素的类型可以不同,因为它们之间没有任何关系。

  (1)使用赋值运算符直接创建列表。

  同其他类型的Python变量一样,创建列表时,也可以使用赋值运算符"="直接将一个列表赋值给变量。

num=[7,14,21,28,35]
python=["人生苦短","我用Python"]

  (2)创建空列表。

  在Python中,可以创建空列表,例如创建一个名称为 emptylist 的空列表。

emptylist=[]

  (3)创建数值列表。

  在Python中,数值列表很常用。使用list()函数直接将range()函数循环出来的结果转换为列表。

  list()函数的基本语法如下。

list(data)  #data表示可以转换为列表的数据,其类型可以是range对象、字符串、元组或者其他可迭代类型的数据。

  例如:创建一个10~20之间(不包括20)所用偶数的列表。

print(list(range(10,20,2)))

11、删除列表:

  对于已经创建的列表,不再使用时可以使用del语句将其删除。语法格式如下:

del listname    #listname为要删除列表的名称

12、访问列表元素:

  在输出列表时,左右两边时带着中括号的,如果不想输出全部元素,可以通过列表的索引获取指定的元素。

 13、输出今天是星期几。

import datetime                              #导入时间日期类
mot=["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
day=datetime.datetime.now().weekday()                 #获取当前星期
print(mot[day])                              #输出今天是星期几

   在上面的代码中,datetime.datetime.now()方法用于获取当前日期,而weekday()方法则是从日期时间对象中获取日期,其值为 0~6 中的一个,

  0 代表星期一,1 代表星期二,以此类推,6 代表星期日。

14、遍历列表:

  遍历列表中的所有元素是常用的一种操作,在遍历的过程中可以完成查询、处理等功能。

  (1)直接使用for循环实现:

  直接使用for循环遍历列表,只能输出元素的值。语法格式如下。

for item in listname:
    #输出item

  item用于保存获取到的元素值,要输出元素内容时,直接输出该变量即可;

  listname为列表名称。

team=["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
for item in team:
print(item)

  (2)使用for循环和enumerate()函数实现

  使用for循环和enumerate()函数可以实现同时输出索引值和元素内容,语法格式如下。

for index,item in enumerate(listname)
    #输出index和item
#index:用于保存元素的索引。
#item:用于保存获取到的元素值,要输出元素内容时,直接输出该变量即可。
#listname 为列表名称。
team=["火箭","勇士","开拓者","爵士","马刺","雷霆"]
for index,item in enumerate(team):
    print(index+1,item)

 15、添加元素:

  添加、修改和删除元素也称为更新列表,在实际开发时,经常需要对列表进行更新。

  (1)添加元素:

listname.append(obj)
  #listname为要添加元素的列表名称,obj为要添加到列表末尾的对象。

  (2)向一个列表中追加另一个列表中的元素。

listname.extend(seq)
  #listname为原列表
  #seq为要添加的列表
  #执行语句后,seq的内容将追加到listname后面。

16、修改元素:

  修改列表中的元素只需要通过索引获取该元素,然后再为其重新赋值即可。

17、删除元素:

  删除元素主要有两种情况,一种是根据索引删除,另一种是根据元素值进行删除。

  (1)根据索引删除:

  删除列表中的指定元素和删除列表类似,也可以使用del语句实现,所不同的就是在指定列表名称时,换为列表元素。

team=["科比","邓肯","加内特"]
del team[-1]  #删除team列表中,倒数第一个元素。
print(team)

  (2)根据元素值删除:

team=["科比","邓肯","加内特"]
team.remove("加内特")    #根据元素值进行删除。
print(team)

   在使用remove()方法删除元素前,最好先判断该元素是否存在。

team=["科比","邓肯","加内特"]
value="詹姆斯"              #指定要删除的元素
if team.count(value)>0:    #判断要删除的元素是否存在
    team.remove(value)     #如果存在移除指定的元素
print(team)

  列表对象的count()方法用于判断指定元素出现的次数,返回结果为 0 时,表示不存在该元素。

18、对列表进行统计和计算:

listname.count(obj)
  #listname   表示列表的名称
  #obj        表示要判断是否存在的对象
  #返回值      元素在列表中出现的次数
team=["湖人","快船","开拓者","湖人","雷霆"]
sum=team.count("湖人")
print(sum)

 19、统计数值列表的元素和:

  在Python中使用sum()函数用于统计数值列表中各元素的和。语法格式如下。

sum(iterable[,start])
  #iterable  表示要统计的列表
  #start    表示统计结果是从哪个数开始(即将统计结果加上start所指定的数),是可选参数,如果没有指定,默认值为0。
统计总成绩:
grade=[76,43,85,45,87,98]
total=sum(grade)
print(total)

 

 

 

 

 

 

  

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/wqs-Time/p/12069545.html