1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
import os import math,copy,random,time from collections import Counter import numpy as np
def hello_world(): yourname = input('你好,请输入你的名字:') print('欢迎来到Python的世界,',yourname) print('让我们开始学习吧~')
def hello_twice(): global yourname,yourheight,yourweight yourname = input('请输入你的名字:') yourheight = input('请输入你的身高:') yourweight = input('请输入你的体重:')
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
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)
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(): 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,'<dict1------------dict6>',dict6)
dict1.clear() print(dict1) del dict1,dict2,dict3,dict4,dict5,dict6
def study_set(): 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<set2)
set1.add('Me too') print('is语句用法',set3==set2,set3 is set2,set1 is not set2) set3.clear() print(set3) del set3
def study_Some_functions(): list1 = [1,2,3,4,5,6] tuple1 = (11,12,13,14,15,16) set1 = set(list1) dict1 = dict(zip([1,2,3,4,5],['one','Two','Three','Four','Five']))
print(max(list1),max(tuple1),max(set1),max(dict1)) print(min(list1),min(tuple1),min(set1),min(dict1)) print(sum(list1),sum(tuple1),sum(set1),sum(dict1)) print(len(list1),len(tuple1),len(set1),len(dict1)) print(divmod(list1[0],tuple1[0])) print(list(enumerate(tuple1)))
list2 = list(tuple1) list3 = list(set1) list4 = list(dict1) tuple2 = tuple(list1)
print(list2,list3,list4)
for i in range(len(list1)): print(list1[i],end=' ') print() for i in dict1: print(i,dict1[i],end=' ')
list5 = list(reversed(list1)) print('\n',list5)
testStr = "The mountains and rivers are different, the wind and the moon are the same" words = testStr.split(' ') print(words) words.sort(key=len) print('以长度排序:',words) words.sort(key=len, reverse=True) print('以长度排序并且反转:', words) words.sort(key=str) print('以字典序排序:',words)
ct = Counter(testStr) print(ct) ct.update('eeeexxxxxlllll') print(ct) print(ct.most_common(5))
def study_Slice(): str1 = 'I hope one day, I can find you, my sweet dream' list1 = list(range(10)) tuple1 = tuple(list1)
print(str1[:]) print(str1[::-1]) print(str1[:15]) print(str1[15:]) print(str1[::2]) print(str1[1::2])
print(list1[:]) print(list1[2:]) print(list1[:2]) print(list1[::-1])
list1[1:5] = [10] print(list1)
def study_loop_select(): list1 = [1,2,3,4,5] num = int(input('while循环,输入你想要循环的次数:')) i = 1 while i<=num: if i<5: print('我打印了',i,'次') elif i<10: print('打印了',i,'次,真累啊') elif i<15: print('打印太多啦,再打印我就要停止了...') elif i<20: print('continue...') i+=1 continue print('我想我可能输出不了了') else: print('累死我了,休息。都',i,'次了~_~') break i+=1 time.sleep(0.5) else: print('while结束了')
for i in list1: print(i,end=' ') print() for i in range(5): print(i)
def study_expression_deduction(): list1 = [i for i in range(10)] list2 = [x for x in range(20) if x%2==0] print(list1,'<list1--------------list2>',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('哈哈,恭喜你,这个程序结束咯~')
|