20170924习题

ex.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‘ %number)

#same as abovefor fruit in fruits:    print(‘a fruit of type %r‘ %fruit)

#also we can go through mixed list 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 built 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(‘elements was %d‘ %i)---------------------------------------------------------------

dir(list)

[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘_
_gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘
, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘, ‘copy‘, ‘co
unt‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

dir(range)

[‘__bool__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__has
h__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘_
_setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘count‘, ‘index‘, ‘start‘, ‘step‘, ‘stop‘]

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

时间: 2024-10-20 15:07:06

20170924习题的相关文章

数据库经典习题,

/* 数据导入: Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Source Server Version : 50624 Source Host : localhost Source Database : sqlexam Target Server Type : MySQL Target Server Version : 50624 File Encoding : utf-8

C/C++算法竞赛入门经典Page15 习题1-1 平均数

题目:输入3个整数,输出他们的平均值,保留3位小数. 首先,声明三个整数a,b,c和一个浮点数d: int a,b,c; double d; 输入三个整数a,b,c: scanf("%d%d%d",&a,&b,&c); 将a,b,c取平均值以后复制给d: d=(double)(a+b+c)/3; 最后输出d: printf("%.3lf",d); %.3lf表示保留3位小数的long float. 注意:不能直接这样输出: printf(&q

问题 1018: C语言程序设计教程(第三版)课后习题6.8

/******************************************************************** @file Main.cpp @date 2017-05-12 @author Zoro_Tiger @brief 问题 1018: C语言程序设计教程(第三版)课后习题6.8 http://www.dotcpp.com/oj/problem1018.html *************************************************

SICP 习题 (1.46)解题总结

SICP 习题 1.46 要求我们写一个过程iterative-improve,它以两个过程为参数,其中一个参数用来检测猜测是否足够好,另一个参数用来改进猜测.过程iterative-improve应该返回另一个过程,所返回的过程接收一个参数作为初始猜测,然后不断改进猜测直到结果足够好.题目还要求我们使用iterative-improve重写1.1.7的sqrt过程和1.3.3节的fixed-point过程. 因为涉及到高阶函数,所以整个题目理解起来有一点点费劲.不过这道题作为第一章的收官题确实

C++ Primer 学习笔记_74_面向对象编程 --再谈文本查询示例[续/习题]

面向对象编程 --再谈文本查询示例[续/习题] //P522 习题15.41 //1 in TextQuery.h #ifndef TEXTQUERY_H_INCLUDED #define TEXTQUERY_H_INCLUDED #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <set> #include <map&g

pta 数据结构 习题2.4 递增的整数序列链表的插入(15 分)

习题2.4 递增的整数序列链表的插入(15 分) 本题要求实现一个函数,在递增的整数序列链表(带头结点)中插入一个新整数,并保持该序列的有序性. 函数接口定义: List Insert( List L, ElementType X ); 其中List结构定义如下: typedef struct Node *PtrToNode; struct Node { ElementType Data; /* 存储结点数据 */ PtrToNode Next; /* 指向下一个结点的指针 */ }; type

linux习题回顾

linux习题回顾 1.1 创建一个压缩包/etc,我想让压缩包上面有个日期/时间. [[email protected] ~]# tar zcf /tmp/etc-$(date+%F).tar.gz /etc [[email protected] ~]# ls -l /tmp -rw-r--r--. 1 root root 9731838 Aug  3 19:15 etc-2017-08-03.tar.gz 1.2 已知/oldboy/test.txt文件内容为: oldboy xizi xi

问题 1041: C语言程序设计教程(第三版)课后习题9.8

/******************************************************************** @file Main.cpp @date 2017-05-28 22:02:55 @author Zoro_Tiger @brief 问题 1041: C语言程序设计教程(第三版)课后习题9.8 http://www.dotcpp.com/oj/problem1041.html ****************************************

问题 1040: C语言程序设计教程(第三版)课后习题9.6

/******************************************************************** @file Main.cpp @date 2017-05-28 21:57:02 @author Zoro_Tiger @brief 问题 1040: C语言程序设计教程(第三版)课后习题9.6 http://www.dotcpp.com/oj/problem1040.html ****************************************