• 《Python编程:从入门到实践》第三章 列表简介 习题答案


    #3.1
    names=['lpr','tjl','gnl','by','dqy'];
    print(names[0]);
    print(names[1]);
    print(names[2]);
    print(names[3]);
    print(names[4]);
    
    #3.2
    print(names[0]+",Hello!");
    print(names[1]+",Hello!");
    print(names[2]+",Hello!");
    print(names[3]+",Hello!");
    print(names[4]+",Hello!");
    
    #3.3
    transport=['bike','high-way','bus','taxi','plane'];
    print("I would like to own a "+transport[0]);
    print("I would like to own a "+transport[1]);
    print("I would like to own a "+transport[2]);
    print("I would like to own a "+transport[3]);
    print("I would like to own a "+transport[4]);
    
    #3.4
    friends=['by','xdy','lyx'];
    print("Hello,"+friends[0]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[1]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[2]+"! I would like to invent you to have dinner with me!");
    
    #3.5
    print(friends[1]+" cannot come here for dinner!");
    friends[1]='dqy';
    print("Hello,"+friends[0]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[1]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[2]+"! I would like to invent you to have dinner with me!");
    
    #3.6
    print("Hello,my friends!I have got a bigger desk for dinner just now,let's invent more 3 fridens!");
    friends.insert(0,'lpr');
    friends.insert(2,'nsx');
    friends.append('gj');
    print("Hello,"+friends[0]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[1]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[2]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[3]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[4]+"! I would like to invent you to have dinner with me!");
    print("Hello,"+friends[5]+"! I would like to invent you to have dinner with me!");
    
    #3.7
    print("Sorry,I could only invent 2 friends for dinner!");
    sorry=friends.pop();
    print(sorry+",I am so sorry that you can't have dinner with me tonight!");
    sorry=friends.pop();
    print(sorry+",I am so sorry that you can't have dinner with me tonight!");
    sorry=friends.pop();
    print(sorry+",I am so sorry that you can't have dinner with me tonight!");
    sorry=friends.pop();
    print(sorry+",I am so sorry that you can't have dinner with me tonight!");
    print("-----------------------------------------");
    print(friends[-1]+",you can have dinner here,enjoy yourselves!");
    print(friends[-2]+",you can have dinner here,enjoy yourselves!");
    print("-----------------------------------------");
    del friends[0];
    del friends[0];
    print(friends);
    #3.8
    dreamPlace=['Tokyo','Xiamen','Linzhi','London','Paris'];
    print(dreamPlace);
     #此处注意,sorted()函数的调用方式是把列表名作为参数传给sorted
    print(sorted(dreamPlace));
    print(sorted(dreamPlace,reverse=True));
     #注意此处的reverse()函数的返回值不可以用变量接收
    dreamPlace.reverse();
    print(dreamPlace);
    dreamPlace.reverse();
    print(dreamPlace);
    
    dreamPlace.sort();
    print(dreamPlace);
    dreamPlace.sort(reverse=True);
    print(dreamPlace);
    
    #3.9
    print(dreamPlace[10]);
  • 相关阅读:
    [php代码]从svn获取指定版本,并同步到ftp上。
    java程序用pid重启
    Gearman安装,测试笔记
    ant编译android项目
    jquery代码收藏
    [读书笔记]读《code complete》有感
    无法解析的外部符号_main,该符号在函数_tmainCRTStartup中被引用
    4路电梯调度——pair program总结
    阅读作业2
    必应缤纷桌面的使用测试
  • 原文地址:https://www.cnblogs.com/zijeak/p/11430234.html
Copyright © 2020-2023  润新知