Python练习题-1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python练习题-1相关的知识,希望对你有一定的参考价值。

最近这几天的学习总练习:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2017/6/9
#练习一/1:实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则失败.
# d = {seven:123}
# while True:
#     user = input(Enter your username:)
#     passwd = input(Enter your password:)
#     if not user and not passwd:
#         continue
#     elif user in d and passwd == d[user]:
#         print(login ok)
#         break
#     else:
#         print(login fail)
# 练习一/2:实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则失败.允许失败是重复输入三次
# d = {seven:123}
# n = 0
# while n < 3:
#     user = input(Enter your username:)
#     passwd = input(Enter your password:)
#     if not user and not passwd:
#         continue
#     elif user in d and passwd == d[user]:
#         print(login ok)
#         break
#     else:
#         print(login fail)
#         n += 1
# 练习一/3:实现用户输入用户名和密码,当用户名为seven或alex且密码为123时,显示登录成功,否则失败.允许失败是重复输入三次
# d = {seven:123,alex:123}
# n = 3
# while n > 0 :
#     user = input(Enter your username:)
#     passwd = input(Enter your password:)
#     if not user and not passwd:
#         continue
#     elif user in d and passwd == d[user]:
#         print(login ok)
#         break
#     else:
#         n -= 1
#         print(You can also try to log in,n,times)

#练习二/1:使用while循环实现输出2-3+4-5+6...+100的和
# n = 2
# jsum = 0
# osum = 0
# while n < 101:
#     if n % 2 == 1:
#         jsum += n
#         n += 1
#     else:
#         osum += n
#         n += 1
# print(osum-jsum)
#练习二/2:使用for循环实现输出1-2+3-4+5-6..+99的和
# jsum = 0
# osum = 0
# for x in range(1,100):
#     if x % 2 == 1:
#         jsum += x
#     else:
#         osum += x
# print(osum-jsum)
#练习二/3:使用while循环实现输出1,2,3,4,5,7,8,9,11,12
# n = 1
# while n < 13:
#     if n == 6 or n ==10:
#         n += 1
#         continue
#     else:
#         print(n,end =  )
#         n += 1
#练习二/3:使用while循环实现输出1-100内的所有奇数
# n = 1
# while n < 101:
#     if n % 2 == 1:
#         print(n)
#         n += 1
#     else:
#         n += 1
#练习二/3:使用while循环实现输出1-100内的所有偶数
# n = 1
# while n < 101:
#     if n % 2 == 1:
#         n += 1
#     else:
#         print(n)
#         n += 1

# 练习三:
# name = " alex "
# a.移除name变量对应的值的两边的空格,并输入移除有的内容
# print(name.strip())
# b.判断name变量对应的值是否以"al"开头,并输出结果
# print(name.startswith(a1))
# c.判断name变量对应的值是否以"x"结尾,并输出结果
# print(name.endswith(x))
# d.将name变量对应的值中的"l"替换为"p",并输出结果
# print(name.replace(l,p))
# e.将name变量对应的值根据"l"分割,并输出结果
# print(name.split(l))
# f.请问,上一题e分割之后得到的值是什么类型
# print(type(name.split(e)))
# g.将name变量对应的值中变大写,并输出结果
# print(name.upper())
# h.将name变量对应的值中变大小写,并输出结果
# print(name.title())
# i.请输出name变量对应的值的第2个字符?
# print(name[1])
# j.请输出name变量对应的值的前3个字符?
# print(name[0:3])
# k.请输出name变量对应的值的后2个字符?
# print(name[0:-2])
# l.请输出name变量对应的值中"e"所在的索引位置?
# print(name.index(e))

# 练习四:
# 写代码,有如下列表,按照要求实现每一个功能
# li = [alex,eric,rain,abc,123,aaa]
# a.计算列表长度并输出
# print(len(li))
# b.列表中追加元素"seven",并输出添加后的列表
# li.append(seven)
# print(li)
# c.请在列表的第1个位置插入元素"Tony",并输出添加后的列表
# li.insert(1,Tony)
# print(li)
# d.请修改列表第2个位置的元素为"Kelly",并输出修改后的列表
# li[2]=Kelly
# print(li)
# e.请删除列表中的元素"eric",并输出修改后的列表
# li.remove(eric)
# print(li)
# f.请删除列表中的第2个元素,并输出删除元素的值和删除元素后的列表
# print(li.pop(1))
# print(li)
# g.请删除列表中的第3个元素,并输出删除元素后的列表
# li.pop(2)
# print(li)
# h.请删除列表中的第2至4个元素,并输出删除元素后的列表
# del li[2:5]
# print(li)
# i.请将列表所有的元素反转,并输出反转后的列表
# li.reverse()
# print(li)

# 练习五:
#正左直角
# for i in range(10):
#     for j in range(0,i):
#         print(*,end =  )
#     print(‘‘)
#倒左直角
# for i in range(10):
#     for j in range(0,10-i):
#         print(*,end =  )
#     print(‘‘)
#倒三角
# for i in range(10):
#     for j in range(0,i):
#         print(end =  )
#     for k in range(0,10 - i):
#         print(*,end =  )
#     print(‘‘)
#正三角
# for i in range(10):
#     for j in range(0,10 - i):
#         print(end =  )
#     for k in range(0,i):
#         print(*,end =  )
#     print(‘‘)
#九九乘法表
# for i in range(1,10):
#     for j in range(1,i+1):
#         print(%s*%s=%s%(i,j,i*j),end =  )
#     print(‘‘)

# 练习六
#取出列表中的名字,年龄,出生的年,月,日
# data=[alex,49,[1900,3,18]]
# name,age,birthday = data=[alex,49,[1900,3,18]]
# print(name)
# print(age)
# print(birthday)

#去掉重复
# names=[egon,alex,egon,wupeiqi]
# name = set(names)
# print(name)

#去掉重复,且保证列表顺序与原来保持一致
# names=[egon,alex,egon,wupeiqi]
# tmp = []
# for n in names:
#     if n not in tmp:
#         tmp.append(n)
# print(tmp)

#去掉重复,且保证列表顺序与原来保持一致
# names=[[1,2],3,[1,2],4]
# tmp = []
# for n in names:
#     if n not in tmp:
#         tmp.append(n)
# print(tmp)

#统计s=hello alex alex say hello sb sb中每个单词的个数
# s=hello alex alex say hello sb sb
# l = s.split()
# st = set(l)
# for x in st:
#     print(x,的个数为,l.count(x))


# for x in s:
#     print(len(x))

#练习七
#not and or 的优先级是  not>and>or  既优先判断 not 没有not判断 and 没有and 最后判断 or,如果有括号先判断括号内的
# if True or False and False:
#     print(yes)
# else:
#     print(no)
#输出结果为?为什么?  True or False ===> True   结果为:yes

# if (True or False) and False:
#     print(yes)
# else:
#     print(no)

#输出结果为?为什么?  True and False ===> False   结果为:no

#练习八
#编写if多分支,猜老男孩的年纪
# age = int(input(Please enter your age:))
# tag = True
# while tag:
#     user_input = input(Please enter your answer:)
#     if user_input.isdigit():
#         user_input = int(user_input)
#     else:
#         continue
#     if int(user_input) > age:
#         print(Guess big!)
#     elif int(user_input) < age:
#         print(Guess small!)
#     elif int(user_input) == age:
#         print(Congratulations,you guessed it!)
#         tag = False

#练习九
#用户输入用户名密码验证,验证通过后进入子循环,输入命令,命令若为q,则退出所有循环
# d = {abc:123}
# tag = True
# while tag:
#     user = input(Please enter your username:)
#     passwd = input(Please enter your password:)
#     if user in d and passwd  == d[user]:
#         while tag:
#             cmd  = input(>>:)
#             if cmd == q:
#                 tag = False
#                 print(exit)
#             else:
#                 print(cmd)

#练习十
#循环取出元组中所有元素:方式一:while和for(按照索引),方式二:不按照索引的方式
# t=(1,2,3,4,5,6,7,8,9)
# n = 0
# while n < len(t):
#     print(t[n])
#     n += 1
#
# for x in range(len(t)):
#     print(t[x])
#
# for x in t:
#     print(x)

#练习十一
#循环读取列表以及子列表中所有元素
# l=[1,2,[3,4],[5,6]]
# for l in l:
#     if type(l) == list:
#         for s in l:
#             print(s)
#     else:
#         print(l)

#练习十二#数据类型练习题:
# 有十进制数n=10
# 转成二进制
# print(bin(10))
# 转成八进制
# print(oct(10))
# 转成十六进制
# print(hex(10))

#练习十三
#与用户交互,要求用户输入年龄和薪资,将用户输入的年龄转成整形,将用户输入的薪资转成浮点型
# age = int(input(Plese enter your age:))
# salary = float(input(Please enter your salary:))
# print(age:,age,salary:,salary)

#练习十四
# 用户输入用户名,年纪,工作,爱好,格式化输出如下内容(使用%s和format两种方式)
# ------------ info of Alex Li -----------
# Name  : Alex Li
# Age   : 22
# job   : Teacher
# Hobbie: girl
# ------------------ end -----------------

# name = input(Please enter your name:)
# age = input(Please enter your age:)
# job = input(Please enter your job:)
# hobby = input(Please enter your hobbies:)
# print(------------ info of %s -----------%name)
# print(‘‘‘Name:%s
# Age:%s
# Job:%s
# Hobby:%s
# ‘‘‘%(name,age,job,hobby),end = ‘‘)
# print(------------------ end -----------------)

# print(------------ info of %s -----------%name)
# info = ‘‘‘Name:{a}
# Age:{b}
# Job:{c}
# Hobby:{d}
# ‘‘‘
# print(info.format(a = name,b = age,c = job,d = hobby),end = ‘‘)
# print(------------------ end -----------------)

#练习十五
# s=alex say hello
# 切片取出第say
# s = s.split()
# print(s[1])
# 切片取出倒数后两个字符
# s = s.split()
# print(s[1:3])

#练习十六
# 编写循环,让用户输入年纪,如果输入为空,或者不为数字,则重新输入
# tag = True
# while tag:
#     age = input(Please enter your age:)
#     if age.isdigit():
#         print(age:,age)
#         tag = False

# 练习十七
# 用列表模拟上电梯的流程(队列)
#     循环生成一个1000个值的列表(入队)
#     循环取走这个1000个值(出队)
# l = []
# n = 0
# for x in range(1000):
#     l.append(x)
# while n < 1000:
#  print(l.pop(0))
#  n += 1

# 用列表模拟把衣服放箱子里,然后取衣服的流程(堆栈)
#     循环生成一个1000个值的列表(入栈)
#     循环取走这个1000个值(出栈)
# l = []
# n = 0
# for x in range(1000):
#     l.append(x)
# while n < 1000:
#  print(l.pop())
#  n += 1

#练习十八
# dicta={a:1,b:2,c:3,d:hello}
# dictb={b:3,c:2,d:world,f:10}
#两字典相加,不同的key对应的值保留,相同的key对应的值相加后保留,如果是字符串就拼接(字符串拼接hello+worldhelloworld)
# {a: 1, b: 5, c: 5, d: helloworld, f: 10}
# d = {}
# d.update(dicta)
# for k2,v2 in dictb.items():
#     if k2 not in d:
#         d[k2] = v2
#     elif k2 in d:
#         d[k2] = d[k2]+v2
# print(d)

#练习十九   #此题egon老师未出完.
#alex发表了朋友圈:
# alex_msg=‘‘‘
#     我想淫一首小诗
#     歌颂我的同桌
#     她乌黑的头发
#     好像一直炸毛鸡
# ‘‘‘
# 
#eogn发表了朋友圈:
# egon_msg=‘‘‘
#     总有文盲装文艺
# ‘‘‘
# comments={
#     alex_msg:{
#         author:alex,
#         comments:[
#             (wupeiqi,真傻比),
#             (元昊,确实傻比),
#             (egon,同上)
#         ],
#         thumb_counts:3,
#     },
#     egon_msg:{
#         author:egon,
#         comments:[
#             (alex,说谁呢),
#             (元昊,你被开除了),
#             (wupeiqi,好刺激)
#         ],
#         thumb_counts:2,
#     },
# }
# 
#循环打印出朋友圈内容
# tag = True
# while tag:
#     cmd = input(查看朋友圈?(y/n))
#     if cmd == y:
#         for k1,v1 in comments.items():
#             print(k1)
#             for k2,v2 in v1.items():
#                 if type(v2) == list:
#                     for u,s in v2:
#                         print(%s:%s%(u,s))
#                 elif type(v2) != list:
#                     print(%s:%s%(k2,v2))
#                     tag = False

 

以上是关于Python练习题-1的主要内容,如果未能解决你的问题,请参考以下文章

Python matplotlib 基础练习:画出正弦曲线等

如何在 python 中并行化以下代码片段?

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

python+spark程序代码片段

13 个非常有用的 Python 代码片段

常用python日期日志获取内容循环的代码片段