Python basics tutorial
了解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)
The above is the detailed content of Python basics tutorial. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.
