小白学python之练习记录1

小白学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)

这里附上一张索引顺序图在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值