欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

Python 基础知识教程

程序员文章站 2022-05-07 13:48:13
...
了解python基本语法 尝试写简单的python程序
 1 count = 0 2 age_to_guess = 23 3  4 while count<3: 5     age_guessed = int(input("age:")) 6     if age_guessed == age_to_guess: 7         print("congratulations") 8         break 9     elif age_guessed > age_to_guess:10         print("Too big")11     else:12         print("Too small")13     count = count + 114     if count == 3:15         confirm_input = input("Do you want to play more?")16         if confirm_input != "n":17             count = 0

1 age_to_guess = 232 age_guessed = int(input("age:"))3 4 if age_guessed == age_to_guess :5     print("Congratulations")6 elif age_guessed > age_to_guess:7     print("Too big")8 else:9     print("Too small")

 1 import sys 2  3 ''' 4 print(sys.path) #打印环境变量 5 print(sys.argv) #相对路径 可在后面加参数 6 ''' 7  8 #操作系统模块 9 import os10 11 cmd_result = os.system("dir") #执行命令 不保存结果12 print(cmd_result) #输出0 为什么? os.system是直接在频幕上输出 没返回13 14 cmd_result_internal_storage = os.popen("dir") #打开的是一个内存地址15 print(cmd_result_internal_storage)16 cmd_result_content = os.popen("dir").read() #读操作17 print(cmd_result_content)18 19 os.mkdir("new_dir") #创建目录20 21 msg = "我爱北京*"22 23 msg_bytes= msg.encode("utf-8")24 25 print(msg_bytes)26 27 msg_str = msg_bytes.decode("utf-8")28 29 print(msg_str)

python list 用法(增删改查):

   names = [, , , ,    (names[2 (names[1:3])  (names[2 (names[:-1 (names[-3:-1])    names.append()    names.insert(2,    names.insert(4,     names[2] =     names.remove(   names[3  names.pop()     (names.index( (names[names.index( (names.count( names.reverse()   names.sort()   names2 = [1, 2, 3, 4         (names)
执行结果:
 1 D:\python\python\python.exe D:/xampp/htdocs/python/day2/list_cut.py 2 num1 3 num3 4 ['num2', 'num3'] 5 ['num3', 'num4', 'num3'] 6 ['num1', 'num2', 'num3', 'num4'] 7 ['num3', 'num4'] 8 ['num1', 'num2', 'num3', 'num4', 'num3', 'num10086'] 9 ['num1', 'num2', 'num_', 'num3', 'num4', 'num3', 'num10086']10 ['num1', 'num2', 'num_', 'num3', '_num', 'num4', 'num3', 'num10086']11 ['num1', 'num2', 'change2', 'num3', '_num', 'num4', 'num3', 'num10086']12 ['num1', 'num2', 'num3', '_num', 'num4', 'num3', 'num10086']13 ['num1', 'num2', 'num3', 'num4', 'num3', 'num10086']14 ['num1', 'num2', 'num3', 'num4', 'num3']15 216 num317 218 ['num3', 'num4', 'num3', 'num2', 'num1']19 ['num1', 'num2', 'num3', 'num3', 'num4']20 ['num1', 'num2', 'num3', 'num3', 'num4', 1, 2, 3, 4]21 []22 23 进程已结束,退出代码0

python list 用法(复制 循环):

 1 #author F 2 import copy 3  4  5 #copy 列表浅复制 6  7 names = ["name1", "name2", "name3", ["alibaba", "blili"], "name4", "name5"] 8 names2 = names.copy() #浅copy:只copy第一层地址 原因:复制的第二层列表的指针地址 9 print(names)10 print(names2)11 12 names[1] = "name_change"13 # print(names)14 # print(names2)15 16 names[3][1] = "BLILI"17 print(names)18 print(names2)19 20 '''21 ['name1', 'name2', 'name3', ['alibaba', 'blili'], 'name4', 'name5']22 ['name1', 'name2', 'name3', ['alibaba', 'blili'], 'name4', 'name5']23 ['name1', 'name_change', 'name3', ['alibaba', 'BLILI'], 'name4', 'name5']24 ['name1', 'name2', 'name3', ['alibaba', 'BLILI'], 'name4', 'name5']25 '''26 27 # = 列表复制28 names = ["name1", "name2", "name3", ["alibaba", "blili"], "name4", "name5"]29 names2 = names30 print(names)31 print(names2)32 33 names[1] = "name_change"34 # print(names)35 # print(names2)36 37 names[3][1] = "BLILI"38 print(names)39 print(names2)40 '''41 ['name1', 'name2', 'name3', ['alibaba', 'blili'], 'name4', 'name5']42 ['name1', 'name2', 'name3', ['alibaba', 'blili'], 'name4', 'name5']43 ['name1', 'name_change', 'name3', ['alibaba', 'BLILI'], 'name4', 'name5']44 ['name1', 'name_change', 'name3', ['alibaba', 'BLILI'], 'name4', 'name5']45 '''46 47 #  模块复制48 names = ["name1", "name2", "name3", ["alibaba", "blili"], "name4", "name5"]49 names2 = copy.deepcopy(names) #深copy : 会占用两份独立的内存空间 慎用50 print(names)51 print(names2)52 53 names[1] = "name_change"54 # print(names)55 # print(names2)56 57 names[3][1] = "BLILI"58 print(names)59 print(names2)60 '''61 ['name1', 'name2', 'name3', ['alibaba', 'blili'], 'name4', 'name5']62 ['name1', 'name2', 'name3', ['alibaba', 'blili'], 'name4', 'name5']63 ['name1', 'name_change', 'name3', ['alibaba', 'BLILI'], 'name4', 'name5']64 ['name1', 'name2', 'name3', ['alibaba', 'blili'], 'name4', 'name5']65 '''66 67 #循环68 for i in names:69     print(i)70 71 print(names[0:-1:1])  #['name1', 'name_change', 'name3', ['alibaba', 'BLILI'], 'name4']72 print(names[0::1])  #['name1', 'name_change', 'name3', ['alibaba', 'BLILI'], 'name4', 'name5']

元组 浅copy补充

 1 person = ["name", ["saving", 123]] 2 #浅copy的三种实现方式 3 ''' 4 p1 = copy.copy(person) 5 p2 = person[:] 6 p3 = list(person) 7 ''' 8 p1 = person[:] 9 p2 = person[:]10 11 p1[0] = 'ale1'12 p2[0] = 'feng'13 14 p1[1][1] = 5015 16 print(p1) #联合账号的用法17 18 #元组19 names = ('231', ' 21314')20 # 元组只有index和count方法

 1 what_he_does = ' plays ' 2 his_instrument = 'guitar' 3 his_name = 'Robert Jhonson' 4 artist_intro = his_name + what_he_does + his_instrument 5  6 print(artist_intro) 7 print(type(what_he_does)) 8  9 10 num = 111 string = '1'12 print(num+int(string))13 14 word = 'word'15 words = word*316 print(words)17 18 word = 'word'19 num = 120 astr = word * (len(word) - num)21 print(astr)22 23 word = 'friends'24 find_the_evil_in_your_friends = \25 word[0] + word[2:4] + word[-3:-1]26 print(find_the_evil_in_your_friends)27 28 tel = '135-8866-9555'29 hide_tel = tel.replace(tel[:9], '*'*9)30 print(hide_tel)31 32 search = '234'33 num_a = '135-8855-2345'34 num_b = '123-2346-2346'35 print(search + ' is at ' + str(num_a.find(search)) + ' to ' + str(num_a.find(search)+len(search)) + ' of num_a')36 print(search + ' is at ' + str(num_b.find(search)) + ' to ' + str(num_b.find(search)+len(search)) + ' of num_b')37 38 39 print('{} a word she can get what she {} for.'.format('With', 'came'))40 print('{prepositon} a word she can get what she {verb} for.'.format(prepositon='With', verb='came'))41 print('{0} a word she can get what she {1} for.'.format('With', 'came'))42 43 # city = input("write down the name of city:")44 # url = "http://api.baidu.com/wather?city={}".format(city)45 # print(url)46 47 48 def f_c(c):49     f = c*9/5 + 3250     return str(f) + '°F'51 52 fahrenheit = f_c(35)53 print(fahrenheit)

以上就是Python 基础知识教程的详细内容,更多请关注其它相关文章!

相关标签: Python 基础