Summary of basic knowledge about python3 learning
1. Data type
1. Number
2. Boolean value
True or False
Variable naming rules:
Variable name can only be any combination of letters, numbers or underscores
- The first character of the variable name cannot be a number
- The variable name cannot be a keyword (for example: and, or,
三, String splicing
1. Use the plus sign (+)
name = "Tom"age = 25print(name + "s age is " + str(age)) #输出:Toms age is 25
2. String formatting
name = = 25( %
4. Lists and Tuples
1. List
Create a list
str_list = ['Tom','Lucy','Mary'] 或者 str_list = list(['Tom','Lucy','Mary'])
Index (access a value in the list)
str_list[0]
Append (add elements to the end)
str_list.append('lilei')print(str_list)#输出:['Tom', 'Lucy', 'Mary', 'lilei']
Insert (add an element at the specified position)
str_list.insert(1,'lilei')print(str_list)#输出:['Tom', 'lilei', 'Lucy', 'Mary']
Delete (delete the specified element)
str_list.remove('Lucy')print(str_list)#输出:['Tom', 'Mary']
Slice
str_list = [3,4,5,6,7,8,9] new_1 = str_list[1:3] #从索引1开始取,取到索引3 new_2 = str_list[0:6:2] #从索引0开始取,每两位一取,到第6位为止 new_3 = str_list[-2:] # 取后面2个数 new_4 = str_list[:3] # 取前面3个数 new_5 = str_list[::3] #所有数,每3个取一个 print(new_1,new_2,new_3,new_4,new_5) #输出:[4, 5] [3, 5, 7] [8, 9] [3, 4, 5] [3, 6, 9]
2. Tuple
Creating tuples
age = (18,25,33) 或者 age = tuple((18,25,33))
5. Dictionary
Use key-value storage method
Create dictionary
phone = { '张三':'13075632152', '李四':'15732015632', '王五':'13420321523', }
Get the value of the key in the dictionary
print(phone['张三']) #如果key不存在,会报错,key用中括号装print(phone.get('老黄')) #如果key不存在,返回None,key用小括号装#输出:13075632152 # None
Assignment
phone[] = phone[] =
删除
phone.pop('张三') #第一种方法del phone['李四'] #第二种方法phone.popitem() #随机删除某一个
遍历
for key in phone: print(key,phone[key]) #输出: # 王五 13420321523 # 张三 13075632152 # 李四 15732015632
多级嵌套
phone = { '人事部':{'老张':'13700112233','老李':'13432023152'}, '财务部':{'小丽':'13230555666','小映':'13723688888'}, '技术部':{'老罗':'13866666333'} } print(phone['人事部']['老李']) #输出:13432023152
六、if语句
1、if...else
age = 16 if age <18: print('你还未成年呢') else: print('你已经成年了')
2、if...elif....else
score = 85 if score > 0 and score< 60: print('你的成绩不及格') elif score >= 60 and score <80: print('你的成绩及格了') elif score>=80 and score<90: print('你的成绩良好') else: print('你的成绩优秀')
七、while循环
i=0 num=0 while i<=100: num+=i i+=1 print('1-100累加等于%d'%num)
八、for...in循环
num = [] for i in range(10): num.append(i) print(num) #输出:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
九、用户交互(input)
name = input('请输入你的名字:') height = input('请输入你的身高:') print('%s的身高%s厘米' %(name,height))
十、文件基本操作
打开文件:f = open('文件路径','模式') 或者 with open('文件路径','模式') as f:
模式:
r:以只读方式打开文件
w:打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a:打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
w+:打开一个文件用于读写。(文件一打开就清空了,还能读到东西吗?)
a+:打开一个文件用于读写。
读文件:
read() readlines() readline() 的用法
f = open('d:/test.txt','r') #以只读方式打开文件 print(f.read()) #read()一次读取文件的全部内容 for line in f.readlines(): #readlines()读取整个文件,并按行存进列表 print(line.strip('\n')) #去掉行尾的'\n' while 1: line = f.readline() #readline()每次只读取一行 print(line.strip('\n')) if not line: break f.close() #关闭文件
写文件:
f =open('d:/test.txt','a') f.write('hello,boy!\n') f.close()
The above is the detailed content of Summary of basic knowledge about python3 learning. 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











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.
