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 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)

output

This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got ‘pennies‘
I got 2
I got ‘dimes‘
I got 3
I got ‘quarters‘
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

2019-09-30

17:13:35

原文地址:https://www.cnblogs.com/petitherisson/p/11612085.html

时间: 2024-08-30 02:02:49

Exercise 32 - for-loop的相关文章

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 # sam

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

笨方法学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

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 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

利用存储过程进行表数据分离的案例分享

某客户有个需求,有2张业务表,数据量比较大,有2000W行,现在的需求是把这2张表中的一部分记录,根据一定的where条件分离出去,创建到另外的归档表中,即做表记录的迁移操作.最后得到的结果是:未满足筛选条件的记录留在原表中,满足筛选条件的表要插入到归档表中,并且要在原表中删除这些插入到归档表中的全部记录,最后满足:新表记录+归档记录=原表记录数 下面我来模拟一下这个过程: 由于没有拿到具体的建表语句,这里把表的内容最简化,只留2个列,作为最基本的演示 --连接到测试用户,创建测试表 SQL>

Oracle数据库之开发PL/SQL子程序和包

Oracle数据库之开发PL/SQL子程序和包 PL/SQL块分为匿名块与命名块,命名块又包含子程序.包和触发器. 过程和函数统称为PL/SQL子程序,我们可以将商业逻辑.企业规则写成过程或函数保存到数据库中,以便共享. 过程和函数均存储在数据库中,并通过参数与其调用者交换信息.过程和函数的唯一区别是函数总向调用者返回数据,而过程不返回数据. 1. 存储过程概念 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL语句集,存储在数据库中.经过第一次编译后

超炫的粒子效果!代码,就应该这么写!

最近瞎逛的时候发现了一个超炫的粒子进度效果,有多炫呢?请擦亮眼镜! 粗略一看真的被惊艳到了,而且很实用啊有木有!这是 Jack Rugile 写的一个小效果,源码当然是有的.聪慧如你,肯定觉得这个东西so easy 要看啥源码,给我3分钟我就写出来了吧.所以你的思路可能是: 1)进度条的实现没什么好说的,简单的一个 fillRect(0,0,long,20),long和20是分别进度条的长宽.然后每帧动画调用前将画布清除clearRect(0,0,canvas.width,canvas.heig

Android 线程与消息 机制 15问15答

1.handler,looper,messagequeue三者之间的关系以及各自的角色? 答:MessageQueue就是存储消息的载体,Looper就是无限循环查找这个载体里是否还有消息.Handler就是创建的时候 会使用looper来构建这个消息循环. handler的主要功能就是 将一个任务切换到某个指定的线程中去执行. 2.为何android 无法在子线程更新ui? 答:都知道 更新ui 实际工作都是在viewrootimpl这个类里面去做的.他的源码里有下面这样一个函数: 1 voi