python编程:从入门到实践----基础知识>第4章练习
pizza = ["Zunbao","Bishengke","Bipizza"]
for i in pizza:
print(i)
print("I like pepperoni pizza\n")
print("I really love pizza")
animals = ["tiger","rabbit","panda"]
for i in animals:
print(i)
print("A dog would make a great pet\n")
print("Any of these animals would make a great pet!")
for i in range(1,20+1):
print(i)
millions = list(range(1,1000000)) #用list把数字转换成列表格式
for i in millions:
print(i)
millions = list(range(1,1000000))
print(min(millions))
print(max(millions))
print(sum(millions))
numbers= list(range(1,20,2)) #通过range的第三个参数,也就是步长为2,来获取奇数
for i in numbers:
print(i)
numbers= list(range(3,30,3))
for i in numbers:
print(i)
squares=[]
for value in range(1,10+1):
square = value**3
squares.append(square)
print(squares)
square=[value**3 for value in range(1,10+1)]
print(square)
4-10 切片:选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
a. 打印消息“The first three items in the lis tare:”,再使用切片来打印列表的前三个元素。
b. 打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素
c. 打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三个元素
my_foods = ['pizza', 'falafel', 'carrot cake','fruit','coffee','rice','meat']
print("The first three items in the list are:")
print(my_foods[:3])
print("\nThree items from the middle of the list are:")
print(my_foods[2:5])
print("\nThe last three items in the list are:")
print(my_foods[4:]) #输出结果:
The first three items in the list are:
['pizza', 'falafel', 'carrot cake'] Three items from the middle of the list are:
['carrot cake', 'fruit', 'coffee'] The last three items in the list are:
['coffee', 'rice', 'meat']
pizza = ["Zunbao","Bishengke","Bipizza"]
friend_pizzas = pizza[:]
pizza.append("Niu")
friend_pizzas.append("Liang") print("My favorite pizzasare:")
for i in pizza:
print(i) print("\nMy friend's favorite pizzasare:")
for j in friend_pizzas:
print(j)
#输出结果:
My favorite pizzasare:
Zunbao
Bishengke
Bipizza
Niu My friend's favorite pizzasare:
Zunbao
Bishengke
Bipizza
Liang
my_foods=['pizza','falafel','carrot','cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
for my_food in my_foods:
print(my_food)
print("\nMy friend's favorite foods are:")
for friend_food in friend_foods:
print(friend_food) #输出结果:
My favorite foods are:
pizza
falafel
carrot
cake
cannoli My friend's favorite foods are:
pizza
falafel
carrot
cake
ice cream
foods =('rice','meat','seafood','vegetable','fruit')
print("The original foods are:")
for food in foods:
print(food) #foods[0]='coffee'
#print(foods[0]) foods=('coffee','meat','seafood','vegetable','tea')
print("\nThe modify foods are:")
for food in foods:
print(food) #输出结果:
The original foods are:
rice
meat
seafood
vegetable
fruit The modify foods are:
coffee
meat
seafood
vegetable
tea
python编程:从入门到实践----基础知识>第4章练习的更多相关文章
- 《Python编程从入门到实践》_第六章_字典
一个简单的字典 #用户信息 user = {','city':'shanghai'} print(user['name']) print(user['age']) print(user['city'] ...
- 《Python编程从入门到实践》_第五章_if语句
条件测试 每条if语句的核心都是一个值为Ture或False的表达式,这种表达式被称为为条件测试.Python根据条件测试的值为Ture还是False来决定是否执行if语句中的代码.如果条件测试的值为 ...
- 《Python编程从入门到实践》_第四章_操作列表
for循环遍历整个列表 pizzas = ['pizzahut','dicos','KFC'] for pizza in pizzas: print ("I like "+ piz ...
- 《Python编程从入门到实践》_第七章_用户输入和whlie循环
函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量中,以方便你使用. #输入用户名 username = input( ...
- 《Python编程从入门到实践》_第三章_列表简介
什么是列表呢? 官方说明就是由一些列按特点顺序排列的元素组成.其实可以看出很多个字符串的有序组合吧,里面的内容可以随时的删除,增加,修改. 下面这个就是一个列表,python打印列表的时候会将中括号和 ...
- 《python编程从入门到实践》2.3字符串
书籍<python编程从入门到实践> 2.3字符串 知识模块 print()函数,函数名称突出为蓝色,输出括号内的变量或者字符创. 变量名的命名:尽量小写字母加下划线并且具有良好的描述性, ...
- 《python编程从入门到实践》读书实践笔记(一)
本文是<python编程从入门到实践>读书实践笔记1~10章的内容,主要包含安装.基础类型.函数.类.文件读写及异常的内容. 1 起步 1.1 搭建环境 1.1.1 Python 版本选择 ...
- Python编程从入门到实践笔记——异常和存储数据
Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...
- Python编程从入门到实践笔记——文件
Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...
随机推荐
- ES6 之 Reflect 的方法总结
1. 概述 将 Object 对象的一些明显属于语言内部的方法(比如 Object.defineProperty ),放到 Reflect 对象上. 修改某些 Object 方法的返回结果,让其变得更 ...
- SciKit-Learn 教程
本教程力求做到简单易懂.深入浅出,帮助你快速掌握机器学习通用库 SciKit-Learn. 机器学习是计算机科学的一个分支,研究的是无需人类干预,能够自己学习的算法. 与TensorFlow不同,Sc ...
- junit基础学习之-简介(1)
JUnit介绍 JUnit是一个开源的Java单元测试框架,由 Erich Gamma 和 Kent Beck 开发完成. 1 JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试, ...
- 08 SSM整合案例(企业权限管理系统):11.AOP日志
04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 11.AOP日志 1.数据库与 ...
- WIN10怎么查看端口,并杀死进程
在命令行执行一下命令 netstat -ano | findstr "
- ESP32-DOWDQ6蓝牙&Wifi 两个可单独控制的 CPU 内核
乐鑫(Espressif Systems)研发设计IoT业内集成度最高.性能最稳定.功耗最低的无线系统级芯片. ESP32-D0WDQ6集成无线2.4 GHz Wi-Fi和蓝牙双模的单芯片方案,具有两 ...
- Arduino --structure
The elements of Arduino (C++) code. Sketch loop() setup() Control Structure break continue do...whil ...
- URAL_1018 Binary Apple Tree 树形DP+背包
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...
- 如何搞定Critical Thinking写作?
受中国传统教育模式与国外一流大学之间的差异的影响,在海外留学的学子们常常会在新的学习生活中面临许多难题,Critical Thinking就是其中之一.国内的教育方法常常以灌输式的教育模式为主,忽略了 ...
- django数据库读写分离
django数据库读写分离 1. 配置数据库 settings.py文件中 用SQLite: DATABASES = { 'default': { 'ENGINE': 'django.db.backe ...