2022-06-21|208阅|作者:云代码会员|举报 摘要:新手代码
import os
import math,copy,random,time
from collections import Counter
import numpy as np
def hello_world():
yourname = input('你好,请输入你的名字:')
print('让我们开始学习吧~')
def hello_twice():
global yourname,yourheight,yourweight
yourname = input('请输入你的名字:')
yourheight = input('请输入你的身高:')
yourweight = input('请输入你的体重:')
#python中字符串的部分操作
def deviding_line():
word1 = 'i am line'
word2 = word1.upper()
word3 = word1.lower()
word4 = word1.title()
#以上三个函数仅为字符串的部分函数
words = [word1,word2,word3,word4]
line = '-' * 40
endReturn = line+words[random.randint(0,3)]+line
return endReturn
#学习python中的数字模型
def study_number():
num1 = input('请输入一个数字:')
print('你输入的是数字%s'%num1,'可它的类型为:',type(num1))
num2 = int(input('再输入一个数字:'))
print('你输入的是数字%s' % num2, '它的类型为:', type(num2))
num3 = float(input('再输入一个数字:'))
print('你输入的是数字%s' % num3, '它的类型为:', type(num3))
print('num1+num2={}'.format(int(num1)+num2))
print('num1-num2={}'.format(int(num1)-num2))
print('num1*num2={}'.format(num1*num2))
print('num1*num2={}'.format(int(num1) * num2))
print('num2//num3={:.3f}'.format(num2//num3))
print('num2/num3={:.4f}'.format(num2/num3))
print('num2%num3={:.4f}'.format(num2 % num3))
print('num2%num3={:.4%}'.format(num2%num3))
print('num1**num2={}'.format(int(num1)**num2))
print('This is the {a},and {b}'.format(a='numbers',b='some operations'))
one,two,three = True,True,False
print(one,two,three)
print('and运算符:',one and two,one and three)
print('or运算符:',one or two,one or three)
print('not运算符:',not one,not two,not three)
def study_list(length):
l1 = [1,2,3,4,5,9.0]
l2 = list(range(10,10+length))
print('l1的类型为:',type(l1))
print(l1[1],l2[1])
l3 = l2
print(id(l1),id(l2),id(l3))
l3[0]=99
print('l2==l3么?',l2==l3)
l4 = l2.copy()
l4[0]=999
print('l4==l2么?',l4==l2)
print('删除前',l4)
del l4[0]
print('删除后',l4)
l4.append(30)
l4.extend(l1)
print('添加l1后:',l4)
l4.reverse()
print('反转后:',l4)
l4.sort()
print('排序后:',l4)
#学习python中的元组模型
def study_tuple(length:int)->bool:
tuple1 = (1,2,3,4)
tuple2 = tuple(range(10,10+length))
print(tuple1.count(1))
print(tuple1.index(1))
try:
tuple1[0] = 9
except TypeError:
print('元组插入失败')
finally:
print('不管插入成不成功我都会执行')
try:
print(id(tuple1),id(tuple2))
except:
return False
else:
tuple3 = tuple1+tuple2
print(tuple3,id(tuple3))
return True
def study_dict(): #学习python中的字典模型,字典是 键->值 的映射
dict1 = {1:'一',2:'二',3:'三',4:'四'}
dict2 = dict(one=1,two=2,three=3)
dict3 = dict(zip([6,7,8,9],['Six','Seven','Eight','Nine']))
dict4 = dict([('One',1),('Two',2),('Three',3)])
dict5 = dict({1:'一',2:'二',3:'三',4:'四'})
print(type(dict1),dict1==dict5)
print(dict1[1],dict2['one'],dict3[6],dict4['One'],dict5[1])
print(dict1.get(4))
dict1[1] = '壹'
dict1[5] = '五'
print(dict1)
print(1 in dict1, 6 in dict1, 7 not in dict1)
dict6 = dict1.copy()
dict6[1] = 'One'
print(dict1,'',dict6)
dict1.clear()
print(dict1)
del dict1,dict2,dict3,dict4,dict5,dict6
def study_set(): #python中集合的学习,集合中不存在相等的值
set1 = set(['You','Are','Not','Beautiful'])
set2 = {'You','Are','So','Beautiful'}
set3 = set2.copy()
print(type(set1))
print(set1,set2)
print(set1|set2)
print(set1&set2)
print(set1^set2)
print(set1-set2)
print(set1<=set2,set3<=set2,set3',list2)
print(deviding_line())
list3 = [['_'] * 3 for i in range(3)]
print(list3)
fruits = ['Apple','Banana','Pear']
colors = ['Red','Yellow','Green']
suitcolor = [(color,fruit) for color,fruit in zip(colors,fruits)]
print(suitcolor)
cartesian = [(color,fruit) for color in colors for fruit in fruits]
print(cartesian)
dict1 = {fruit:color for fruit,color in suitcolor}
print(dict1)
def study_files():
filepath = input('请输入你的文件路径(输入quit退出):')
if filepath=='quit':
return True
try:
file = open(filepath,'w')
file.write('哈哈,现在开始写文件')
file.close()
file = open(filepath,'r')
print('从文件中读出的内容:\n',file.read())
except FileNotFoundError:
print('文件未找见请重新输入')
study_files()
except:
print('出现错误,请重新输入路径')
study_files()
class Users():
def __init__(self,name,height,weight):
self.name = name
self.height = height
self.weight = weight
self.yanzhi = 100
def display(self):
print('大家好,我是{},身高{},体重{},颜值超高{}'.format(self.name,self.height,self.weight,self.yanzhi))
if __name__=="__main__":
hello_world()
deviding_line()
try:
print(yourname)
except:
print(' 未能找见该变量 ')
deviding_line()
hello_twice()
user = Users(yourname,yourheight,yourweight)
user.display()
chooseinformation = '''Input the number of the function you want to Run(quit is exit):
1、study_number 2、study_list
3、study_tuple 4、study_dict
5、study_set 6、study_Some_functions
7、study_Slice 8、study_loop_select
9、study_expression_deduction
10、study_files
'''
deviding_line()
while True:
input('按键继续')
print(chooseinformation)
num = input('输入序号:')
if num=='quit':
break
elif num=='1':
study_number()
elif num=='2':
study_list(10)
elif num=='3':
study_tuple(10)
elif num=='4':
study_dict()
elif num=='5':
study_set()
elif num=='6':
study_Some_functions()
elif num=='7':
study_Slice()
elif num=='8':
study_loop_select()
elif num=='9':
study_expression_deduction()
elif num=='10':
study_files()
deviding_line()
print('哈哈,恭喜你,这个程序结束咯~')