Exercise 32: Loops And Lists

the_count = [1, 2, 3, 4, 5]
fruits = [‘apples‘, ‘oranges‘, ‘pears‘, ‘apricots‘]
change = [1, ‘pennies‘, 2, ‘dimes‘, 3, ‘quarters‘]
# this first kind of for-loop goes through a list
for number in the_count:
  print "This is count %d" % number
# same as above
for fruit in fruits:
  print "A fruit of type: %s" % fruit
# also we can go through mixed lists too
# notice we have to use %r since we don‘t know what‘s in it
for i in change:
  print "I got %r" % i
# we can also build lists, first start with an empty one
elements = []
# then use the range function to do 0 to 5 counts
for i in range(0, 6):
  print "Adding %d to the list." % i # append is a function that lists understand
  elements.append(i)
# now we can print them out too
for i in elements:
  print "Element was: %d" % i
时间: 2024-07-29 17:47:59

Exercise 32: Loops And Lists的相关文章

Exercise 32 - for-loop

the_count = [1,2,3,4,5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list for number in the_count: print("This is count %d" % number) # same a

Python Index

Table Of ContentsThe Hard Way Is EasierExercise 0: The SetupExercise 1: A Good First ProgramExercise 2: Comments And Pound CharactersExercise 3: Numbers And MathExercise 4: Variables And NamesExercise 5: More Variables And PrintingExercise 6: Strings

reviews of learn python3 the hard way

Almost every time,I try my best to write a long review of the book I have read. But this time I want to have a short one. learn python3 the hard way is a book which will teach you how to start to write codes. It has many exercises,through which you c

笨方法学Python,Lesson 32 - Lesson 34

Exercise 32 代码 the_count = [1, 2, 3, 4, 5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list for number in the_count:     print "This is count %d&q

Exercise 3.2 Display a date

无法让我不汗颜: Exercise 3-2. Write a program that prompts the user to enter the date as three integer values for the month, the day in the month, and the year. the program should then output the date in the form 31st December 2003 when the user enters 12 3

mongodb数据库添加权限及简单数据库命令操作笔记

加固mongodb建议:修改数据库默认端口,添加数据库访问权限: 启动数据库(裸奔):C:\mongodb\bin>mongod --dbpath C:\MongoDB\data(同时用--dbpath指定数据存放地点为"db"文件夹.) 数据库管理:mongo.exe 新版的MongoDB已经不支持addUser方法了,改成createUser了. 启动数据库的注意事项: 指定端口启动数据库(不需要认证):E:\mongodb\bin>mongod --dbpath E:

机器学习 1 linear regression 作业(二)

机器学习 1 linear regression 作业(二) 这个线性回归的作业需要上传到https://inclass.kaggle.com/c/ml2016-pm2-5-prediction 上面,这是一个kaggle比赛的网站.第一次接触听说这个东西,恰好在京东上有一本刚出来的关于这个的书<Python机器学习及实践:从零开始通往Kaggle竞赛之路>.把我自己写的代码运行保存的结果提交上去后发现,损失函数值很大,baseline是6,而我的却是8,于是很不心甘,尝试了其他方法无果后,准

思科资料的所有资料目录

1.20140819单臂路由.pdf 2.CCNA 实验手册之Packet_Tracer使用教程.pdf 3.CCNA_Lab_Workbook_Sample_Labs(CDP,静态路由,端口安全).pdf 4.CCNA.CCNP.CCIE案例实战手册(交换部分).pdf 5.CCNA.CCNP.CCIE案例实战手册(路由部分).pdf 6.CCNA帧中继解法.pdf 7.CCNA级别的实际企业环境 综合实验.ppt 10.hsrp.pkt 11.CCNA_2010年5月(23个实验)最新实验总

Guava 3: 集合Collections

一.引子 Guava 对JDK集合的拓展,是最成熟且最受欢迎的部分.本文属于Guava的核心,需要仔细看. 二.Guava 集合 2.1 Immutable Collections不可变集合 1.作用 用不变的集合进行防御性编程和性能提升. 2.简单使用 1 package guava.collect; 2 3 import com.google.common.collect.ImmutableSet; 4 5 /** 6 * @author denny 7 * @Description 不可变