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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
| print("Hello World")
""" 本代码演示了: - 各类字面量的写法 - 通过print语句输出各类字面量 """
666
13.14
"马"
print(666) print(13.14) print("马")
money = 50 print("钱包还有:",money)
money = money - 10 print("买了冰激凌花费10元,还剩余:",money,"元")
print("现在是下午1点,钱包余额是:",money,"元") print("现在是下午2点,钱包余额是:",money,"元") print("现在是下午3点,钱包余额是:",money,"元") print("现在是下午4点,钱包余额是:",money,"元")
money = 50 print("钱包余额为:",money)
money = money - 10 print("剩余:",money)
money = money - 5 print("剩余:",money)
print(type("马")) print(type(666)) print(type(13.14))
string_type = type("马") int_type = type(666) float_type = type(13.14) print(string_type) print(int_type) print(float_type)
name = "马" name_type = type(name) print(name_type)
num_str = str(11) print(type(num_str),num_str)
float_str = str(13.14) print(type(float_str),float_str)
num = int("11") print(type(num),num)
num2 = float("13.14") print(type(num2),num2)
num4 = float(11) print(type(num4),num4)
int_num = int(13.14) print(type(int_num),int_num)
name_ = "张三" _name = "张三" name_1 = "张三"
Itheima = "马" itheima = 666 print(Itheima) print(itheima)
Class = 1
""" 演示Python中的各种运算符 """
print("1 + 1 =",1 + 1) print("2 - 1 =",2 - 1) print("3 * 3 =",3 * 3) print("4 / 2 =",4 / 2) print("11 // 2 =",11 // 2) print("9 % 2 =",9 % 2) print("2 ** 2 =",2 ** 2)
num = 1 + 2 * 3
num = 1 num += 1 print("num += 1:",num) num -= 1 print("num -= 1:",num) num *= 1 print("num *= 1:",num) num /= 1 print("num /= 1:",num)
num = 3 num %= 2 print("num %= 2:",num)
num **= 2 print("num **= 2:",num)
num = 9 num //= 2 print("num //= 2:",num)
""" 演示字符串的三种定义方式: 单引号定义法 双引号定义法 三引号定义法(使用变量接受它,它就是字符串) """
name = '马' print(type(name))
name = "马" print(type(name))
name = """ 马 """ print(type(name))
name = '"马"' print(name)
name = "'马'" print(name)
name = "\"马\"" print(name) name = '\'马\'' print(name)
print("马" + "少宣")
name = "额" address = "癔症蛋" print("我是:" + name + ",我是" + address)
name = "马" massage = "少宣%s" % name print(massage)
class_num = 57 avg_salary = 16781 massage = "python大数据学科,北京%s期,毕业平均工资:%s" % (class_num, avg_salary) print(massage)
name = "传智播客" setup_year = 2006 stock_price = 19.99 massage = "%s,成立于:%d,我今天的股价是:%f" % (name,setup_year,stock_price) print(massage)
num1 = 11 num2 = 11.345 print("数字11的宽度限制为5,结果是:%5d" % num1) print("数字11的宽度限制为1,结果是:%1d" % num1) print("数字11.345的宽度限制为7,小数精度是2,结果是:%7.2f" % num2) print("数字11的宽度不限制,小数精度是2,结果是:% .2f" % num2)
name = "传智播客" setup_year = 2006 stock_price = 19.99 print(f"我是{name},我成立于:{setup_year},我今天的股价是:{stock_price}")
print("1 + 1 的结果是:%d" % (1 + 1)) print(f"1 * 2 的结果是:{1 * 2}") print("字符串在Python中的类型名是:%s" % type("字符串"))
name = "传智播客" stock_price = 19.99 stock_code = "003032" stock_price_daily_growth_factor = 1.2 growth_days = 7 finally_stock_price = stock_price * stock_price_daily_growth_factor ** growth_days print(f"公司:{name},股票代码:{stock_code},当前股价:{stock_price}") print("每日增长系数%.1f,经过%d天的增长后,股价达到了:%.2f" % (stock_price_daily_growth_factor,growth_days,finally_stock_price))
""" # 演示Python中的input语句,"请告诉我你是谁"可以写到input里面 print("请告诉我你是谁") name = input() print("我知道了,你是:%s" % name) # 输入数字类型 num = input("请告诉我你的年龄:") num = int(num) print("类型:",type(num)) """
bool_1 = True bool_2 = False print(f"bool_1 = {bool_1},类型是{type(bool_1)}") print(f"bool_2 = {bool_2},类型是{type(bool_2)}")
num1 = 10 num2 = 10 print(f"10 = 10的结果是:{num1 == num2}")
num1 = 10 num2 = 15 print(f"10 != 15的结果是:{num1 != num2}")
name1 = "itacst" name2 = "itheima" print(f"itcast == itheima 结果是:{name1 == name2}")
num1 = 10 num2 = 5 print(f"10 > 5结果是:{num1 > num2}") print(f"10 < 5结果是:{num1 < num2}")
num1 = 10 num2 = 11 print(f"10 >= 11结果是:{num1 >= num2}") print(f"10 <= 11结果是:{num1 <= num2}")
age = 20 if age >= 18: print("我已经成年了") print("已经步入大学生活") print("时间过得真快")
""" # 练习成年人判断 age = input("请输入你的年龄:") age = int(age) if age >= 18: print("您已成年需要买票10元") print("祝您游玩愉快") """
""" # if else 语句 age = int(input("请输入你的年龄:")) if age >= 18: print("您已成年,需要买票10元") else: print("您未成年,祝您游玩愉快") """
""" # 练习我要买票吗 height = int(input("请输入你的身高:")) if height > 120: print("您的身高超过120cm,需要买票10元") else: print("你的身高低于120cm,可以免费游玩") print("猪您啊") """
""" # 演示if elif else height = int(input("请输入你的身高:")) vip_level = int(input("请输入你的vip等级:"))
if height < 120: print("可以免费") elif vip_level > 3: print("可以免费") else: print("都不满足,需要买票10元") print("猪你愉快") """ """ #练习猜猜心里数字 num = 5 if int(input("请猜一个数字")) == num: print("第一次猜对") elif int(input("猜错了,再来一次:")) == num: print("猜对了") elif int(input("猜错了,再来一次:")) == num: print("最后一次机会猜对了") else: print("sorry猜错了") """ """ # 嵌套 if int(input("你的身高:")) > 120: print("身高限制,不可免费") print("如果你的VIP大于3可以免费") if int(input("你的VIP等级:")) > 3: print("可以免费") else: print("买票") else: print("欢迎游玩") """ """ # 定义一个随机数字,猜 import random num = random.randint(1,10) guess_num = int(input("输入你要猜的数字:")) if guess_num == num: print("正确") else: if guess_num > num: print("大") else: print("小") guess_num = int(input("再次输入你要猜的数字:")) if guess_num == num: print("第二次正确") else: if guess_num > num: print("大") else: print("小") guess_num = int(input("第三次输入你要猜的数字:")) if guess_num == num: print("第三次正确") else: print("机会用完") """
i = 0 while i < 100: print("我喜欢你") i += 1
i = 1 sum = 0 while i <= 100: sum += i i += 1 print(f"1-100累加的和:{sum}")
""" # 一个1-100随机数,通过while循环,猜,并求猜了几次 import random num = random.randint(1,100) count = 0
flag = True while flag: guess_num = int(input("请输入猜测的数字:")) count += 1 if guess_num == num: print("猜中了") flag = False else: if guess_num > num: print("大") else: print("小")
print(f"一共猜测了{count}次") """
i = 1 while i <= 100: print(f"今天是第{i}天,准备表白") j = 1 while j <= 10: print(f"送给她{j}朵玫瑰") j += 1 print("我喜欢你") i += 1
print(f"坚持到第{i - 1}天,表白成功")
i = 1 while i <= 9: j = 1 while j <= i: print(f"{j} * {i} = {j * i}\t",end='') j += 1 i += 1 print()
name = "mashaoxuan" for x in name: print(x)
name = "mashaoxuan" count = 0 for x in name: if x == "a": count += 1 print(f"共有{count}个a")
for x in range (5): print("送玫瑰")
for x in range (5,10): print(x)
for x in range(5,10,2): print(x)
i = 0 for i in range (5): print(i)
print(i)
for i in range (1,101): print(f"今天是表白的第{i}天") for j in range (1,11): print(f"送给她的第{j}朵玫瑰") print("我喜欢你")
print("第一百天,表白成功")
for i in range(1,10): for j in range(1,i + 1): print(f"{j} * {i} = {i * j}\t",end = '') print()
for i in range(1,6): print("灰太狼") for j in range(1,3): print("红太狼") continue print("哄她爱了") print("爱了")
for i in range(1,6): print("喜洋洋") break print("美羊羊") print("沸羊羊")
money = 10000 for i in range(1,21): import random score = random.randint(1,10) if score < 5: print(f"员工{i}绩效分{score}不足,不发工资") continue if money >= 1000: money -= 1000 print(f"员工{i},发工资1000,账户余额:{money}") else: print(f"余额不足,当前{money}") break
str1 = "ma" str2 = "shao" str3 = "xuan" def my_len(data): count = 0 for i in data: count += 1 print(f"字符串{data}的长度是{count}") my_len(str1) my_len(str2) my_len(str3)
def say_hi(): print("hello world") say_hi()
def check(): print("hello world\n你好")
check()
def add(x,y): result = x + y print(f"{x} + {y} = {result}") add(5,6)
def add(a,b): result = a + b return result r = add(5,6) print(r)
def say_hi(): print("hello world") result = say_hi() print(f"无返回值函数,返回的内容是:{result}") print(f"无返回值函数,返回的内容类型是:{type(result)}")
def say_hi2(): print("hello world") return None result = say_hi2() print(f"无返回值函数,返回的内容是:{result}") print(f"无返回值函数,返回的内容类型是:{type(result)}")
def check_age(age): if age >= 18: return "SUCCESS" else: return None result = check_age(16) if not result: print("未成年,不可进入")
def add(x,y): """ 三个引号,然后回车 :param x: :param y: :return: """ result = x + y print(f"结果是 = {result}") return result add(5,6)
def func_b(): print("2") def func_a(): print("1") func_b() print("3") func_a()
def func_a(): num = 100 print(num) func_a()
num = 200 def test_a(): print(f"test_a:{num}") test_a() print(num)
num = 200 def test_a(): print(f"test_a:{num}") def test_b(): num = 500 print(f"test_b:{num}") test_a() test_b() print(num)
num = 200 def test_a(): print(f"test_a:{num}") def test_b(): global num num = 500 print(f"test_b:{num}") test_a() test_b() print(num)
""" # 案例 money = 5000000 name = None name = input("请输入你的姓名:") def query(show_header): if show_header: print("查询余额") print(f"{name},余额{money}") def saving(num): global money money += num print("存款") print(f"{name},存款{num}") query(False) def get_money(num): global money money -= num print("取款") print(f"{name},取款{num}") query(False) def main(): print("主菜单") print(f"{name}") print("查询余额\t[1]") print("存款\t\t[2]") print("取款\t\t[3]") print("退出\t\t[4]") return input("选择:") while True: keyboard_input = main() if keyboard_input == "1": query(True) continue elif keyboard_input == "2": num = int(input("存入:")) saving(sum) continue elif keyboard_input == "3": num = int(input("取走:")) get_money(sum) continue else: print("退出") break """
name_list = ["m",6,True] print(name_list) print(type(name_list)) print(name_list[1]) print(name_list[-1])
my_list = [[1, 2, 3],[4, 5, 6]] print(my_list) print(type(my_list)) print(my_list[1][0])
mylist = ["m","s","x"] index = mylist.index("s") print(f"s下标索引值:{index}")
mylist[0] = "马" print(f"修改后:{mylist}")
mylist.insert(1,"少") print(f"插入后:{mylist}")
mylist.append("宣") print(f"追加后:{mylist}")
mylist2 = [1,2,3] mylist.extend(mylist2) print(f"追加后:{mylist}")
mylist = [4,5,6] del mylist[2] print(f"删除后:{mylist}")
mylist = [4,5,6] element = mylist.pop(2) print(f"通过pop方法取出后:{mylist},取出:{element}")
mylist = [4,4,5,6] mylist.remove(4) print(f"通过remove方法移除元素:{mylist}")
mylist = [4,5,6] mylist.clear() print(f"清空后:{mylist}")
mylist = [4,4,5,6] count = mylist.count(4) print(f"4的数量是:{count}")
mylist = [4,4,5,6] count = len(mylist) print(f"全部元素数量:{count}")
def list_while_func(): mylist = [1,2,3] index = 0 while index < len(mylist): element = mylist[index] print(f"列表的元素:{element}") index += 1 list_while_func()
def list_for_func(): mylist = [1,2,3] for element in mylist: print(f"列表的元素有:{element}") list_for_func()
t1 = (1,"m",True) t2 = () t3 = tuple() print(f"t1的类型是:{type(t1)},内容是:{t1}") print(f"t2的类型是:{type(t2)},内容是:{t2}") print(f"t3的类型是:{type(t3)},内容是:{t3}")
t4 = ("m",) print(f"t4的类型是:{type(t4)},内容是:{t4}")
t5 = ((1,2,3),(4,5,6)) print(f"t5的类型是:{type(t5)},内容是:{t5}")
num = t5[1][2] print(f"取出元素:{num}")
t6 = ("m","s","x") index = t6.index("s") print(f"s的下标:{index}")
t7 = ("m","m","s","x") num = t7.count("m") print(f"m个数:{num}")
t8 = ("m","m","s","x") num = len(t8) print(f"元素个数:{num}")
index = 0 while index < len(t8): print(t8[index]) index += 1
for element in t8: print(f"元组的元素:{element}")
t9 = ("m","m","s","x",["ma","shao"]) print(t9) t9[4][0] = "xuan" print(f"t9的内容:{t9}")
mystr = "ma shao xuan"
value = mystr[2] value2 = mystr[-1] print(f"从字符串{mystr}去下标为2的元素{value},下标为-1的元素{value2}")
value = mystr.index("shao") print(f"在字符串{mystr}中查找shao,起始下标:{value}")
newstr = mystr.replace("shao","少") print(newstr)
mystr = "ma shao xuan" mystrlist = mystr.split(" ") print(f"将字符串{mystr}进行split切分后{mystrlist},类型{type(mystrlist)}")
mystr = " ma shao xuan " mystrlist = mystr.strip() print(mystrlist)
mystr = "12ma shao xuan21" newstr = mystr.strip("12") print(f"字符串{mystr}被strip('12')后,结果:{newstr}")
mystr = "ma shao" count = mystr.count("m") print(count)
num = len(mystr) print(num)
mystr = "itma shaoit" num = mystr.count("it") print(f"{mystr},中有it{num}个") newstr = mystr.replace(" ","|") print(newstr) mystrlist = newstr.split("|") print(mystrlist)
mylist = [1,2,3,4,5,6,7,8,9] result1 = mylist[1:4] print(result1)
mytuple = (1,2,3,4,5,6,7,8,9) result2 = mytuple[:] print(result2)
mystr = ("01234567") result3 = mystr[::2] print(result3)
mystr = ("01234567") result4 = mystr[::-1] print(result4)
mylist = [1,2,3,4,5,6,7,8,9] result5 = mylist[3:1:-1] print(result5)
mytuple = (1,2,3,4,5,6,7,8,9) result6 = mytuple[::-2] print(result6)
myset = {"ma","shao","xuan","ma","shao","xuan","ma","shao","xuan"} myset2 = set() print(f"myset的内容:{myset},类型是:{type(myset)}") print(f"myset2的内容:{myset2},类型是:{type(myset2)}")
myset.add("python") myset.add("ma") print(f"myset的内容:{myset},类型是:{type(myset)}")
myset .remove("python") print(f"myset的内容:{myset},类型是:{type(myset)}")
myset = {"ma","shao","xuan"} element = myset.pop() print(f"取出{element},取出后:{myset}")
myset.clear() print(f"清空后:{myset}")
set1 = {1,2,3} set2 = {1,5,6} set3 = set1.difference(set2) print(f"取差集后:{set3}") print(f"取差集后原有set1:{set1}") print(f"取差集后原有set2:{set2}")
set1 = {1,2,3} set2 = {1,5,6} set1.difference_update(set2) print(f"消除差集后原有set1:{set1}") print(f"消除差集后原有set2:{set2}")
set1 = {1,2,3} set2 = {1,5,6} set3 = set1.union(set2) print(f"2集合合并:{set3}") print(f"合并后集合1:{set1}") print(f"合并后集合2:{set2}")
set1 = {1,2,3,4,5,6}
num = len(set1) print(f"集合中元素数量:{num}")
set1 = {1,2,3,4,5,6} for element in set1: print(element)
my_dict1 = {"ma":99,"shao":88,"xuan":77} my_dict2 = {} my_dict3 = dict() print(f"字典一:{my_dict1},类型:{type(my_dict1)}") print(f"字典二:{my_dict2},类型:{type(my_dict2)}") print(f"字典三:{my_dict3},类型:{type(my_dict3)}")
my_dict1 = {"ma":99,"shao":88,"xuan":77} score = my_dict1["ma"] print(f"ma的分数:{score}")
my_dict1 = {"ma":{"语文":77, "数学":66, "英语":33}, "shao":{"语文":88, "数学":86, "英语":55}, "xuan":{"语文":99, "数学":86, "英语":55} } print(f"学生考试:{my_dict1}")
score = my_dict1["shao"]["语文"] print(f"shao的语文:{score}")
my_dict = {"ma":99,"shao":88,"xuan":77} my_dict["马"] = 66 print(f"新增后:{my_dict}")
my_dict["ma"] = 33 print(f"更新后:{my_dict}")
score = my_dict.pop("ma") print(f"移除后:{my_dict}")
my_dict.clear() print(f"清空后:{my_dict}")
my_dict = {"ma":99,"shao":88,"xuan":77} keys = my_dict.keys() print(keys)
for key in keys: print(f"字典的key:{key}") print(f"字典的value是:{my_dict[key]}")
for key in my_dict: print(f"字典的key:{key}") print(f"字典的value是:{my_dict[key]}")
num = len(my_dict) print(num)
print(f"abd大于abc,结果:{'abd' > 'abc'}")
def test_return(): return 1,"hello",True x,y,z = test_return() print(x) print(y) print(z)
def user_info(name,age,gender): print(f"姓名:{name},年龄:{age},性别:{gender}")
user_info('小米',20,'男')
user_info(name='小王',age=11,gender='女') user_info(age=10,gender='女',name='笑笑') user_info('天天',gender='女',age=9)
def user_info(name,age,gender='男'): print(f"姓名:{name},年龄:{age},行捏:{gender}") user_info('小天',13,'男')
def user_info(*args): print(f"类型:{type(args)},内容:{args}") user_info(1,2,3,'小米','难哄')
def user_info(**kwargs): print(f"类型:{type(kwargs)},内容:{kwargs}") user_info(name='ma',age=20,gender='nanhai')
def test_fund(compute): result = compute(1,2) print(f"compute参数的类型:{type(compute)}") print(result) def compute(x,y): return x + y test_fund(compute)
def test_fund(compute): result = compute(1,2) print(f"结果:{result}") test_fund(lambda x,y:x +y)
f = open("D:/Python/PycharmProjects/pythonProject/练习/测试.txt","r",encoding="UTF-8") print(type(f))
print(f"读取两个字节的结果:{f.read(2)}") print(f"读取全部的结果:{f.read()}")
lines = f.readlines() print(f"lines对象的类型:{type(lines)}") print(f"lines对象的内容:{lines}")
line1 = f.readline() line2 = f.readline() line3 = f.readline() print(f"第一行:{line1}") print(f"第二行:{line2}") print(f"第三行:{line3}")
for line in f: print(line)
with open("D:/Python/PycharmProjects/pythonProject/练习/测试.txt"): for line in f: print(line)
f.close()
try: f = open("D:/abc.txt","r",encoding="UTF-8") except: print("出现异常,因为文件不存在,我将open的模式,改为w模式去打开") f = open("D:/abc.txt","w",encoding="UTF-8")
try: print(name) except NameError as e: print("出现了变量未定义的异常") print(e)
try: 1 / 0 print(name) except (NameError,ZeroDivisionError) as e: print("出现了变量未定义 或者 除以0的异常错误")
try: print(name) 1 / 0 except Exception as e: print("出现异常") else: print("没有异常") finally: print("我是finally,有没有异常都要执行") f.close()
|