小白学python之练习记录1
本篇文章包含了python基础的大部分练习,也是我学习python的练习记录,我是小白,所以我更懂得小白该怎么学习,勤加练习是成为大牛的必经之路,希望我的练习能对你学习python有所帮助。
#coding=UTF-8
lists=[1,5,28,35,4]
res=lists[:: -1]#颠倒顺序
print('颠倒后:',res)
'''函数'''
import math
def length(a,b):
c=(a*a+b*b)
return math.sqrt(c) #调用math库
# return c**(1/2) 直接开方
x=length(3,4)
print(x)
'''文件的读取'''
file = open('E:/1/1.txt','w')
file.write('Hello World')
'''文件'''
def text_create(name, msg):
desktop_path = 'E:/1/'
full_path = desktop_path + str(name) + '.txt'
file = open(full_path,'w')
file.write(msg)
file.close()
text_create(12,"hello")
'''in包含'''
album = ['Black Star','David Bowie',25,True]
print('Black Star' in album)
'''if ...else...'''
def account_login():
password = input('Password:')
if password == '12345':
print('Login success!')
else:
print('Wrong password or invalid input!')
account_login()#运行函数
account_login()#调用函数
'''if···elif···else···重置密码'''
password_list = ['*#*#','12345']
def account_login():
password = input('Password:')
password_correct = password == password_list[-1]
password_reset = password == password_list[0]
if password_correct:
print('Login success!')
elif password_reset:
new_password = input('Enter a new password:')
password_list.append(new_password)
print('Your password has changed successfully!')
account_login()
else:
print('Wrong password or invalid input!')
account_login()
account_login()
'''for循环'''
for every_letter in 'Hello world':
print(every_letter)
for num in range(1, 11): # 内置函数range,从1到10,不包含11
print(str(num) + ' + 1 =', num + 1)
'''嵌套循环 乘法口诀'''
for i in range(1, 10):
for j in range(1, 10):
print('{} X {} = {}'.format(i, j, i * j))
"""猜大小"""
import random
def touzi(numbers=3):
chose=["big","small"]
print("<<<---GAME START!!!--->>>")
f=True
while f:
points = []
while numbers>0:
point=random.randint(1,6)
points.append(point)
numbers=numbers-1
total=sum(points)
start=input("big or small:")
if start ==chose[0]:
if 11 <= total <= 18:
print("true", points)
else:
print("false", points)
elif start ==chose[1]:
if 3 <= total <= 10:
print("true", points)
else:
print("false", points)
else:
print("<<<---GAME OVER!!!--->>>")
f=False
touzi()
'''增删改查'''
#1 插入
fruit = ['pineapple','pear']
fruit.insert(1,'grape')
print(fruit)
fruit[1:1] = ['Orange']
print(fruit)
#2 删除
fruits = ['pinapple','pear','grape']
fruits.remove('grape')
print(fruits)
del fruits[0:2]
print(fruits)
#3 修改
fruit[0] = 'Grapefruit'
"""字典的增删改查"""
dirc={"zhagnd":12,"liu":11}
#1增加 key值不能重复,value值可以重复
dirc["huang"]=10#增加单个元素
print(dirc)
dirc.update({"xi":10,"sun":9})# 增加多个元素
print(dirc)
#2 修改
dirc["zhagnd"]=8
print(dirc)
#3 删除
del dirc["liu"]
print(dirc)
#4 查找
print(dirc["huang"])
"""元组,set"""
#1 元组
letters = ('a','b','c','d','e','f','g')
print(letter[0])
#2 set
set = {1,2,3,7,3,4}#set集合内元素不重复
print(set)
set.add(5)#增加
print(set)
set.discard(5)#删除
print(set)
这里附上一张索引顺序图