列表练习

练习3.实现栈的入栈出栈等功能
#!/usr/bin/env python
#coding:utf-8
__author__ = "hahaha"

'''
@author:hahaha
@file:haha.py
@contact
769218703@qq.com
@time:7/01/10:18 AM
@desc:
'''


stack = []

def pushstack():
        item = raw_input("newitem:")
        stack.append(item)


def popstack():
        if len(stack) == 0:
                print "can not popfrom empty stack."
        else:
                print "%s pop fromstack" % stack.pop()

def viewstack():
        print stack

def showmenu():
        str = 'welcome to stackmanage'
       

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是三个Python列表练习题的解答: 1. 计算所有偶数的和: ```python list1 = [11, 53, 40, 45, 27, 16, 28, 99] list = [] for x in list1: if x % 2 == 0: list.append(x) print(sum(list)) ``` 这段代码遍历了列表中的每个数字,如果数字是偶数,则将其添加到新的列表中。最后,使用`sum()`函数计算新列表中所有数字的和,并打印出结果。 2. 打印列表中所有的奇数: ```python list1 = [11, 53, 40, 45, 27, 16, 28, 99] list = [] for x in list1: if x % 2 != 0: list.append(x) print(list) ``` 这段代码遍历了列表中的每个数字,如果数字是奇数,则将其添加到新的列表中。最后,打印出新列表中的所有数字。 3. 打印列表中所有能被3整除但是不能被2整除的数: ```python list1 = [11, 53, 40, 45, 27, 16, 28, 99] list = [] for x in list1: if x % 3 == 0 and x % 2 != 0: list.append(x) print(list) ``` 这段代码遍历了列表中的每个数字,如果数字能被3整除且不能被2整除,则将其添加到新的列表中。最后,打印出新列表中的所有数字。 希望这些代码可以帮助你完成Python列表练习题!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【练习题】python列表](https://blog.csdn.net/weixin_47005749/article/details/128967352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值