python基础第二天

列表切片

new_list = [1,2,3,3.45,'asd',['asd','qwe',123]]
 print('--------->原始数据',new_list)
 print(new_list[5])
 print(new_list[-1])
 print(new_list[0:3])
 print(new_list[::2])
 print(new_list[1::2])
 for  item in new_list:
     print(item)
 print(len(new_list))

 i = 0
 while i<len(new_list) :
     print(new_list[i])
     # i = i+1
     i += 1
列表的增删改查
增加
 new_list[6] = 'asd123'
 new_list.append('asd123')
 new_list.append(['asd123',123])
 new_list.extend(['asd123',123])
 new_list.extend('phfbjsdjf')
 new_list.extend(123)
new_list.insert(0,'开始')

删除

 ret1 = new_list.pop(3)
 ret1 = new_list.remove('asdqwe')
 print(ret1)
new_list.clear()
 del new_list[1]
 new_list[0] = '1'
 ret = new_list.count('asd')
 ret = new_list.index('asd')
 print(ret)
 print('增加数据之后的列表',new_list)
 new_list2 = [1,2,11,4,5,6,22,13,12,67]
 print('------------原始的列表',new_list2)
 new_list2.sort(reverse=True)
 def func1(a):
    print('------>',a)
     return int(a)
     # return -int(a)

列表排序

sorted

ret =[int(item)  for item in   sorted('68123',key=func1)]
 print(ret)

reverse

print('------------排序之后的列表',new_list2)
 print(new_list[::-1])
 ret = new_list.reverse()
 print(ret)
 ret = list(reversed(new_list))
 print(ret)
 print('------>操作之后',new_list)

列表删除

new_list = [1,2,3,4,6,7,3,4,4,4,5]
 index_list = []

 将第二个4删除
 if 4 in new_list:
     print('有4在的')

     #  todo 找出所有的4对应的下标
     i = 0
     while i<len(new_list):

         #  todo 挨个取出元素,与4进行对比,如果是4,记录i的值,
         if new_list[i] == 4:
             print('找到数据')
             index_list.append(i)
         # todo  如果不是4,则任何操作没有
         else:
             pass

         i+= 1
 else:
     print('4不存在')

 print(index_list)

例题

用户输入月份, 判断这个月是哪个季节
 分析:
 345 ------春季
 678 - ----夏季
 91011 - --秋季
 1212 - ---冬季

 spring = [3,4,5]
 summer = [6,7,8]
 au = [9,10,11]
 win = [12,1,2]

 try:
     mon = int(input('请输入需要判定的月份'))
 except Exception  as e:
     print(e)

 else:
     print('else')

     if mon in spring:
         print('spr')
     elif mon in summer:
         print(summer)

 finally:
     print('fina')
给出两个整数n和k,返回从1到n中取k个数字的所有可能的组合
例如:

如果n=4,k=2,结果为

n = 4
 k = 2
 new_list = list(range(1,n+1))
 print(new_list)
 import itertools
 for item in itertools.permutations(new_list,k):
     print(item)

 print(eval('1+2'))
 import random

todo 需要一个变量控制生成题目的个数

count = 0
 while count <10:
     # todo  随机生成 数字1
     num1 = random.randint(0,100)
     # todo  随机生成 数字2

     num2 = random.randint(0,100)
     # todo  随机生成 运算符
     op = random.choice(list('+-*/'))

     # todo 将题目显示
     print('当前的题目是 %d   %s  %d = '%(num1,op,num2))
     # todo 与答题者交互,获取答题者的答案
     an = int(input('请输入您的答案:'))
     # todo 自动判断答题者的答案是否正确
     true_an =  eval('%d   %s  %d  '%(num1,op,num2))
     print('正确的答案',true_an)
     if true_an == an:
         print('答案是正确的')
     else:
         print('答案是错误的')
     # todo 显示相关信息
     count += 1
 for i in range(0,10):

     # todo  随机生成 数字1
     num1 = random.randint(0,100)
     # todo  随机生成 数字2

     num2 = random.randint(0,100)
     # todo  随机生成 运算符
     op = random.choice(list('+-*/'))
     # todo 将题目显示
     print('当前的题目是 %d   %s  %d = '%(num1,op,num2))
     # todo 与答题者交互,获取答题者的答案
     an = int(input('请输入您的答案:'))
     # todo 自动判断答题者的答案是否正确
     true_an =  eval('%d   %s  %d  '%(num1,op,num2))
     print('正确的答案',true_an)
     if true_an == an:
         print('答案是正确的')
     else:
         print('答案是错误的')
     # todo 显示相关信息
假定有下面的列表:
names = [‘fentiao’,‘fendai’,‘fensi’,‘apple’]
输出结果为:‘I have fentiao, fendai, fensi and apple.’
names = ['fentiao','fendai','fensi','apple']
 print(','.join(names[:3]))
 print('i have '+','.join(names[:3]) + ' and '+names[-1])
#系统里面有多个用户,用户的信息目前保存在列表里面
users = [‘root’,‘westos’]
passwd = [‘123’,‘456’]
2.用户登陆(判断用户登陆是否成功)
1).判断用户是否存在
2).如果存在
1).判断用户密码是否正确
如果正确,登陆成功,推出循环
如果密码不正确,重新登陆,总共有三次机会登陆
3).如果用户不存在
重新登陆,总共有三次机会
 users = ['root','westos']
 pwd = ['123','456']

 todo 设置count用来控制登录次数,不能超过3次
 count = 0

 while count < 3:

     count += 1
     # todo  判断用户是否存在
     #  todo  通过交互,获取用户输入的用户名和密码
     username = input('请输入用户名:')
     passwd = input('请输入密码:')


     # todo  如果用户存在
     if username in users:

         # todo  判断用户的密码是否正确
         # todo 找到当前用户名对应的密码

         true_pwd_index = users.index(username)
         true_pwd = pwd[true_pwd_index]

         # todo  用户的密码正确,登录成功,退出循环
         if true_pwd == passwd:
             print('------------->登录成功')
             break
         else:
             # todo  用户的密码失败,重新登录
             print('---------->用户登录失败,用户名或者密码错误')
     # todo  1.2 用户不存在
     else:
         print('---------->用户登录失败,用户名或者密码错误')

stack = []

 while True:
     print('--------------------> 1 入栈')
     print('--------------------> 2 出栈')
     print('--------------------> 3 栈顶元素')
     print('--------------------> 4 栈的长度')
     print('--------------------> 5 栈是否为空 ')
     print('--------------------> 6 退出栈的操作 ')
todo 获取用户的选择

choice = input(‘请输入您要完成的操作’)

todo 入栈

if choice == ‘1’:
# todo 获取用户需要入栈的数据
input_data = input(‘请输入需要入栈的数据’)
# todo 将数据放入栈
stack.append(input_data)
print(‘入栈成功’.center(60,’-’))

todo ‘--------------------> 2 出栈’

elif choice == ‘2’:
# todo 需要判断是否是空栈
if len(stack) == 0:
print(‘是空栈’.center(60,’-’))

 # todo 不是空栈,直接抛出数据
 else:
     out_data = stack.pop()
     print(('取出的数据是%s'%out_data).center(60,'-'))
todo ‘--------------------> 3 栈顶元素’

elif choice == ‘3’:
# todo 判断是否是空栈
if len(stack) == 0:
print(‘空栈’.center(60,’-’))
# todo 展示数据
else:
print((‘栈顶元素是%s’%stack[-1]).center(60,’-’))

todo ‘--------------------> 4 栈的长度’

elif choice == ‘4’:
print(‘栈的长度是%s’%(len(stack)))

todo '--------------------> 5 栈是否为空 ’

elif choice == ‘5’:
if len(stack) == 0:
print(‘是空栈’.center(60,’-’))

  # todo '--------------------> 6 退出栈的操作 '
     elif choice == '6':
         break

    else:
        pass
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值