Python自学:第四章 复制列表(1)】的更多相关文章

# -*- coding: GBK -*- my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] print("My favorite foods are:") print(my_foods) print("\nMy friend's favorite foods are:") print(friend_foods) 输出为: My favorite foods are:…
# -*- coding: GBK -*- players = ['charles', 'martina', 'michael', 'florence', 'eli'] print(players[0:3]) 输出为: ['charles', 'martina', 'michael'] 2.中间提取 # -*- coding: GBK -*- players = ['charles', 'martina', 'michael', 'florence', 'eli'] print(players[…
>>> cars = ["bmw", "audi", "toyota", "subaru"] >>> len(cars) 4…
motorcycles = ['honda', 'yamaha' ,'suzuki'] motorcycles.insert(0, "ducati") print(motorcycles) 输出为: ['ducati', 'honda', 'yamaha', 'suzuki'] 2: motorcycles = [] motorcycles.append("honda") motorcycles.append("yamaha") motorcyc…
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) motorcycles[0] = 'ducati' print(motorcycles) 输出为: ['honda', 'yamaha', 'suzuki'] ['ducati', 'yamaha', 'suzuki']…
bicycles = ['trek','cannondale','redline','specialized'] message = "My first bicycle was a " + bicycles[0].title() + "." print(message) 输出为: My first bicycle was a Trek.…
#输出并首字母大写 bicycles = ['trek','cannondale','redline','specialized'] print(bicycles[0].title()) 输出为: Trek…
# -*- coding: GBK -*- players = ['charles', 'martina', 'michael', 'florence', 'eli'] print("Here are the first three players on my team:") for player in players[:3]: print (player.title()) 输出为: Here are the first three players on my team: Charle…
# -*- coding: GBK -*- magicians = ['alice', 'david', 'carolina'] for magician in magicians: print(magician.title() + ", that was a great trick!") print("I can't wait to seee your next trick," + magician.title() + ".\n") print…
# -*- coding: GBK -*- magicians = ['alice', 'david', 'carolina'] for magician in magicians: print(magician.title() + ", that was a great trick!") print("I can't wait to seee your next trick," + magician.title() + ".\n") 输出为:…
# -*- coding: GBK -*- magicians = ['alice', 'david', 'carolina'] for magician in magicians: print(magician.title() + ", that was a great trick!") 输出为: Alice, that was a great trick! David, that was a great trick! Carolina, that was a great trick…
一.遍历整个列表 1-1.假设有一个魔术师名单,需要将其中每个魔术师的名字都打印出来. # 用for循环来打印魔术师名单中的名字 magicians=['alice','david','carolina'] for magician in magicians: #这里面的magician和magicians是便于让读者得知for循环在哪个列表中进行遍历 print(magician) #输出结果: alice david carolina 解析以上代码: a. 先定义一个列表 b. 定义一个fo…
2016/1/28学习内容 第四章 Python字符串与正则表达式之正则表达式 正则表达式是字符串处理的有力工具和技术,正则表达式使用预定义的特定模式去匹配一类具有共同特征的字符串,主要用于字符串处理,可以快速,准确地完成复杂的查找,替换等处理要求. Python中,re模块提供了正则表达式操作所需要的基本功能 正则表达式元字符 元字符: . 匹配除换行符意外的任意单个字符 元字符: * 匹配位于*之前的0个或多个字符 元字符: + 匹配位于+之前的1个或多个字符 元字符: | 匹配位于|之前或…
第四章. 控制流 控制语句后面要加冒号: 1)    if语句 if guess == number: print 'Congratulations, you guessed it.' # New block starts here elif guess < number: print 'No, it is a little higher than that' # Another block else: print 'No, it is a little lower than that' if…
复制列表的方法: lst = [1,2,3] lst1 = lst[:] # one way lst2 = list(lst) # another 删除数据的正确方法: num_list = [1, 2, 3, 4, 5] print(num_list) for item in num_list[:]: if item == 2: num_list.remove(item) else: print(item) print(num_list) 作者:方小圆 链接:https://www.zhihu…
# -*- coding: GBK -*- cars = ["bmw", "audi", "toyota", "subaru"] print("这是以前的列表:") print(cars) print("\n这是排序后的列表:") print(sorted(cars)) print("\n再次核对是否改变以前的列表:") print(cars) 输出为: 这是以前的列…
1.列表是什么? 列表[list]:列表是由一系列特定顺序排列的元素组成. 列表由[]表示 eg: a = ['jackal','jax';'jack','jeef','jacky'] print(a) ['jackal', 'jax', 'jack', 'jeef', 'jacky'] 2.列表的索引 索引:索引是为了加速对表中数据行的检索而创建的一种分散的存储结构.(百度百科) 我个人理解:索引就是给一组元素中每个元素的单独的特定名称,方便可以快速找出其中的一个元素. 索引是从0开始的 eg…
# -*- coding: GBK -*- #reverse: 相反的 cars = ["bmw", "audi", "toyota", "subaru"] print(cars) cars.reverse() print(cars) 输出为: ['bmw', 'audi', 'toyota', 'subaru'] ['subaru', 'toyota', 'audi', 'bmw']…
cars = ["bmw", "audi", "toyota", "subaru"] cars.sort() print(cars) 输出为: ['audi', 'bmw', 'subaru', 'toyota'] 反向排序: cars = ["bmw", "audi", "toyota", "subaru"] cars.sort(reverse…
motorcycles = ["honda", "yamaha", "suzuki"] first_owned = motorcycles.pop(0) print("The first motorcycle I owned was a " + first_owned.title() + ".") 输出为: The first motorcycle I owned was a Honda.…
#4.1 pizzas = ['KFC','MDL','DKS'] ''' for pizza in pizzas: print(pizza); ''' for pizza in pizzas: print("I like "+ pizza +" pizza!") ''' print("My favourite pizza is KFC and I really love pizza!") #4.2 animals = ['cat','tiger…
# -*- coding: GBK -*- squares = [value**2 for value in range(1,11)] print(squares) 输出为: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]…
>>>digits = [1,2,3,4,5,6,7,8,9,0] >>>mid(digits) 0 >>>max(digits) 9 >>>sum(digits) 45…
# -*- coding: GBK -*- number = list(range(1,6)) print(number) 输出为: [1, 2, 3, 4, 5] 2. # -*- coding: GBK -*- even_numbers = list(range(2,11,2)) print(even_numbers) 输出为: [2, 4, 6, 8, 10] 3. # -*- coding: GBK -*- squares = [] for value in range(1,11): s…
Python的高级应用(一) 本章内容: 内置函数 生成器 迭代器 装饰器 JSON和PICKLE的简单用法 软件目录结构规范 一.内置函数 1.数学运算类 abs(x) 求绝对值1.参数可以是整型,也可以是复数2.若参数是复数,则返回复数的模 complex([real[, imag]]) 创建一个复数 divmod(a, b) 分别取商和余数注意:整型.浮点型都可以 float([x]) 将一个字符串或数转换为浮点数.如果无参数将返回0.0 int([x[, base]])  将一个字符转换…
1. clear 方法clear删除所有的字典项,就地执行,什么都不返回(或者说返回None) d = {} d['name'] = 'Gumby' d['age'] = 42 print(d) return_value = d.clear() print(d) print(return_value) 结果: {'name': 'Gumby', 'age': 42} {} None 2. copy 方法copy返回一个新字典,其包含的键-值对与原来的字典相同(该方法属于浅复制,因为值本身是原件,…
ocket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Unix,而Unix/Linux基本哲学之一就是"一切皆文件",对于文件用[打开][读写][关闭]模式来操作.socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO.打开.关闭) socket和file的区别: file模块是针对某个…
1.列表 names=[] #这就是一个空列表 names=[1,5,2,3,4,5]#列表是用的中括号,每个元素是用逗号分开的.列表里面的元素是可以重复的. names[-1]#表示的是取列表的最后一个元素 names[0]#表示的是取列表的第一个元素,取列表的元素是以下标以0开始 #切片 names[1:3]表示取得列表元素是从第二个元素到第三个元素 .所谓顾头不顾尾. names[1:-1]表示取得列表的从第二个元素到倒数第二个元素 names[-3:-1]表示的取的列表是从倒数第二个元素…
在python中,所有集合都可以迭代,在python语言内部,迭代器用于支持 for循环 构建和扩展集合类型 逐行遍历文本文件 列表推导,字典推导和集合推导 元组拆包 调用函数时,使用*拆包实参 本章涵盖的话题 语言内部使用 iter(...) 内置函数处理可迭代对象的方式如何使用 Python 实现经典的迭代器模式详细说明生成器函数的工作原理如何使用生成器函数或生成器表达式代替经典的迭代器如何使用标准库中通用的生成器函数如何使用 yield from 语句合并生成器案例分析:在一个数据库转换工…
一.数据类型的种类及主要功能 1.数字类型 数字类型主要是用来计算,它分为整数类型int和浮点类型float 2.布尔类型 布尔类型主要是用于判断,它分为真True和False两种 3.字符串类型 字符串类型是以引号引用的内容,它主要是为了方便存储以及传送一些简单的数据 4.列表类型 列表主要用来存储大量的数据内容,格式为 list = [a,b,c,d,e] 它可以存储任意数据类型 5.元组类型 元组跟列表类似,只是它的内容不能被修改,格式为(a,b,c,d,e) 6.字典类型 字典类型主要是…