目录
1、数据类型
2、变量
3、列表
4、集合
5、字典
6、注释
7、基本功能
8、条件语句
9、循环语句
10、函数
11、异常处理
12、字符串操作
13、正则表达式
首页 后端开发 Python教程 13个Python必备的知识建议收藏

13个Python必备的知识建议收藏

May 09, 2023 pm 03:10 PM
python 编程语言

13个 Python 必备的知识,建议收藏!

Python在编程语言流行指数PYPL中已多次排名第一。

由于其代码可读性和更简单的语法,它被认为是有史以来最简单的语言。

NumPy、Pandas、TensorFlow等各种AI和机器学习库的丰富性,是Python核心需求之一。

如果你是数据科学家或 AI/机器学习的初学者,那么Python是开始你的旅程的正确选择。

本次,小F会带着大家探索一些Python编程的基础知识,虽然简单但都很有用。

  • 目录
  • 数据类型
  • 变量
  • 列表
  • 集合
  • 字典
  • 注释
  • 基本功能
  • 条件语句
  • 循环语句
  • 函数
  • 异常处理
  • 字符串操作
  • 正则表达式

1、数据类型

数据类型是可以存储在变量中的数据规范。解释器根据变量的类型为变量分配内存。

下面是Python中的各种数据类型。

13个 Python 必备的知识,建议收藏!

2、变量

变量是存放数据值的容器。

变量可以使用短名称(如x和y)或更具描述性的名称(age、carname、total_volume)。  

Python 变量命名规则:

  • 变量名必须以字母或下划线字符开头
  • 变量名称不能以数字开头
  • 变量名只能包含字母数字字符和下划线(A-z、0-9和_)
  • 变量名称区分大小写(age、Age和AGE是三个不同的变量)
var1 = 'Hello World'
var2 = 16
_unuseful = 'Single use variables'
登录后复制

输出结果如下。

13个 Python 必备的知识,建议收藏!

3、列表

列表(List)是一种有序和可更改的集合,允许重复的成员。

它可能不是同质的,我们可以创建一个包含不同数据类型(如整数、字符串和对象)的列表。‍

>>> companies = ["apple","google","tcs","accenture"]
>>> print(companies)
['apple', 'google', 'tcs', 'accenture']
>>> companies.append("infosys")
>>> print(companies)
['apple', 'google', 'tcs', 'accenture', 'infosys']
>>> print(len(companies))
5
>>> print(companies[2])
tcs
>>> print(companies[-2])
accenture
>>> print(companies[1:])
['google', 'tcs', 'accenture', 'infosys']
>>> print(companies[:1])
['apple']
>>> print(companies[1:3])
['google', 'tcs']
>>> companies.remove("infosys")
>>> print(companies)
["apple","google","tcs","accenture"]
>>> companies.pop()
>>> print(companies)
["apple","google","tcs"]
登录后复制

4、集合

集合(Set)是一个无序和无索引的集合,没有重复的成员。

对于从列表中删除重复条目非常有用。它还支持各种数学运算,例如并集、交集和差分。

>>> set1 = {1,2,3,7,8,9,3,8,1}
>>> print(set1)
{1, 2, 3, 7, 8, 9}
>>> set1.add(5)
>>> set1.remove(9)
>>> print(set1)
{1, 2, 3, 5, 7, 8}
>>> set2 = {1,2,6,4,2}
>>> print(set2)
{1, 2, 4, 6}
>>> print(set1.union(set2))# set1 | set2
{1, 2, 3, 4, 5, 6, 7, 8}
>>> print(set1.intersection(set2)) # set1 & set2
{1, 2}
>>> print(set1.difference(set2)) # set1 - set2
{8, 3, 5, 7}
>>> print(set2.difference(set1)) # set2 - set1
{4, 6}
登录后复制

5、字典

字典是作为键值对的可变无序项集合。

与其他数据类型不同,它以【键:值】对格式保存数据,而不是存储单个数据。此功能使其成为映射JSON响应的最佳数据结构。

>>> # example 1
>>> user = { 'username': 'Fan', 'age': 20, 'mail_id': 'codemaker2022@qq.com', 'phone': '18650886088' }
>>> print(user)
{'mail_id': 'codemaker2022@qq.com', 'age': 20, 'username': 'Fan', 'phone': '18650886088'}
>>> print(user['age'])
20
>>> for key in user.keys():
>>> print(key)
mail_id
age
username
phone
>>> for value in user.values():
>>>print(value)
codemaker2022@qq.com
20
Fan
18650886088
>>> for item in user.items():
>>>print(item)
('mail_id', 'codemaker2022@qq.com')
('age', 20)
('username', 'Fan')
('phone', '18650886088')
>>> # example 2
>>> user = {
>>> 'username': "Fan",
>>> 'social_media': [
>>> {
>>> 'name': "Linkedin",
>>> 'url': "https://www.linkedin.com/in/codemaker2022"
>>> },
>>> {
>>> 'name': "Github",
>>> 'url': "https://github.com/codemaker2022"
>>> },
>>> {
>>> 'name': "QQ",
>>> 'url': "https://codemaker2022.qq.com"
>>> }
>>> ],
>>> 'contact': [
>>> {
>>> 'mail': [
>>> "mail.Fan@sina.com",
>>> "codemaker2022@qq.com"
>>> ],
>>> 'phone': "18650886088"
>>> }
>>> ]
>>> }
>>> print(user)
{'username': 'Fan', 'social_media': [{'url': 'https://www.linkedin.com/in/codemaker2022', 'name': 'Linkedin'}, {'url': 'https://github.com/codemaker2022', 'name': 'Github'}, {'url': 'https://codemaker2022.qq.com', 'name': 'QQ'}], 'contact': [{'phone': '18650886088', 'mail': ['mail.Fan@sina.com', 'codemaker2022@qq.com']}]}
>>> print(user['social_media'][0]['url'])
https://www.linkedin.com/in/codemaker2022
>>> print(user['contact'])
[{'phone': '18650886088', 'mail': ['mail.Fan@sina.com', 'codemaker2022@qq.com']}]
登录后复制

6、注释

单行注释,以井字符(#)开头,后面带有消息并在行尾结束。

# 定义用户年龄
age = 27
dob = '16/12/1994' # 定义用户生日
登录后复制

多行注释,用特殊引号(""")括起来,你可以将消息放在多行中。

"""
Python小常识
This is a multi line comment
"""
登录后复制

7、基本功能

print()函数在控制台中打印提供的消息。此外你还可以提供文件或缓冲区输入作为在屏幕上打印的参数。

print(object(s), sep=separator, end=end, file=file, flush=flush)
print("Hello World") # prints Hello World
print("Hello", "World")# prints Hello World?
x = ("AA", "BB", "CC")
print(x) # prints ('AA', 'BB', 'CC')
print("Hello", "World", sep="---") # prints Hello---World
登录后复制

input()函数用于收集来自控制台的用户输入 。

这里需要注意,input()会把你输入的任何内容转换为字符串。

因此,如果你将年龄作为整数值提供,但input()方法将其作为字符串返回,此时就需要手动将其转换为整数。

>>> name = input("Enter your name: ")
Enter your name: Codemaker
>>> print("Hello", name)
Hello Codemaker
登录后复制

len()可以查看对象的长度。如果你输入一个字符串,则可以获取指定字符串中的字符数。

>>> str1 = "Hello World"
>>> print("The length of the stringis ", len(str1))
The length of the stringis 11
登录后复制

str()用于将其他数据类型转换为字符串值。

>>> str(123)
123
>>> str(3.14)
3.14
登录后复制

int()用于将字符串转换为整数。

>>> int("123")
123
>>> int(3.14)
3
登录后复制

8、条件语句

条件语句是用于根据特定条件更改程序流程的代码块。这些语句只有在满足特定条件时才会执行。

在Python中,我们使用if,if-else,循环(for,while)作为条件语句根据某些条件来改变程序的流程。

if-else语句。

>>> num = 5
>>> if (num > 0):
>>>print("Positive integer")
>>> else:
>>>print("Negative integer")
登录后复制

elif语句。

>>> name = 'admin'
>>> if name == 'User1':
>>> print('Only read access')
>>> elif name == 'admin':
>>> print('Having read and write access')
>>> else:
>>> print('Invalid user')
Having read and write access
登录后复制

9、循环语句

循环是一个条件语句,用于重复某些语句(在其主体中),直到满足某个条件。

在Python中,我们通常使用for和while循环。

for循环。

>>> # loop through a list
>>> companies = ["apple", "google", "tcs"]
>>> for x in companies:
>>> print(x)
apple
google
tcs
>>> # loop through string
>>> for x in "TCS":
>>>print(x)
T
C
S
登录后复制

range()函数返回一个数字序列,它可以用作for循环控制。

它基本上需要三个参数,其中第二个和第三个是可选的。参数是开始值、停止值和步进数。步进数是每次迭代循环变量的增量值。

>>> # loop with range() function
>>> for x in range(5):
>>>print(x)
0
1
2
3
4
>>> for x in range(2, 5):
>>>print(x)
2
3
4
>>> for x in range(2, 10, 3):
>>>print(x)
2
5
8
登录后复制

我们还可以使用else关键字在循环结束时执行一些语句。

在循环结束时提供else语句以及循环结束时需要执行的语句。

>>> for x in range(5):
>>>print(x)
>>> else:
>>>print("finished")
0
1
2
3
4
finished
登录后复制

while循环。

>>> count = 0
>>> while (count < 5):
>>>print(count)
>>>count = count + 1
0
1
2
3
4
登录后复制

我们可以在while循环的末尾使用else,类似于for循环,当条件为假时执行一些语句。

>>> count = 0
>>> while (count < 5):
>>>print(count)
>>>count = count + 1
>>> else:
>>>print("Count is greater than 4")
0
1
2
3
4
Count is greater than 4
登录后复制

10、函数

函数是用于执行任务的可重用代码块。在代码中实现模块化并使代码可重用,这是非常有用的。

>>> # This prints a passed string into this function
>>> def display(str):
>>>print(str)
>>>return
>>> display("Hello World")
Hello World
登录后复制

11、异常处理

即使语句在语法上是正确的,它也可能在执行时发生错误。这些类型的错误称为异常。我们可以使用异常处理机制来避免此类问题。

在Python中,我们使用try,except和finally关键字在代码中实现异常处理。

>>> def divider(num1, num2):
>>> try:
>>> return num1 / num2
>>> except ZeroDivisionError as e:
>>> print('Error: Invalid argument: {}'.format(e))
>>> finally:
>>> print("finished")
>>>
>>> print(divider(2,1))
>>> print(divider(2,0))
finished
2.0
Error: Invalid argument: division by zero
finished
None
登录后复制

12、字符串操作

字符串是用单引号或双引号(',")括起来的字符集合。

我们可以使用内置方法对字符串执行各种操作,如连接、切片、修剪、反转、大小写更改和格式化,如split()、lower()、upper()、endswith()、join()和ljust()、rjust()、format()。

>>> msg = 'Hello World'
>>> print(msg)
Hello World
>>> print(msg[1])
e
>>> print(msg[-1])
d
>>> print(msg[:1])
H
>>> print(msg[1:])
ello World
>>> print(msg[:-1])
Hello Worl
>>> print(msg[::-1])
dlroW olleH
>>> print(msg[1:5])
ello
>>> print(msg.upper())
HELLO WORLD
>>> print(msg.lower())
hello world
>>> print(msg.startswith('Hello'))
True
>>> print(msg.endswith('World'))
True
>>> print(', '.join(['Hello', 'World', '2022']))
Hello, World, 2022
>>> print(' '.join(['Hello', 'World', '2022']))
Hello World 2022
>>> print("Hello World 2022".split())
['Hello', 'World', '2022']
>>> print("Hello World 2022".rjust(25, '-'))
---------Hello World 2022
>>> print("Hello World 2022".ljust(25, '*'))
Hello World 2022*********
>>> print("Hello World 2022".center(25, '#'))
#####Hello World 2022####
>>> name = "Codemaker"
>>> print("Hello %s" % name)
Hello Codemaker
>>> print("Hello {}".format(name))
Hello Codemaker
>>> print("Hello {0}{1}".format(name, "2022"))
Hello Codemaker2022
登录后复制

13、正则表达式

  • 导入regex模块,import re。
  • re.compile()使用该函数创建一个Regex对象。
  • 将搜索字符串传递给search()方法。
  • 调用group()方法返回匹配的文本。
>>> import re
>>> phone_num_regex = re.compile(r'ddd-ddd-dddd')
>>> mob = phone_num_regex.search('My number is 996-190-7453.')
>>> print('Phone number found: {}'.format(mob.group()))
Phone number found: 996-190-7453
>>> phone_num_regex = re.compile(r'^d+$')
>>> is_valid = phone_num_regex.search('+919961907453.') is None
>>> print(is_valid)
True
>>> at_regex = re.compile(r'.at')
>>> strs = at_regex.findall('The cat in the hat sat on the mat.')
>>> print(strs)
['cat', 'hat', 'sat', 'mat']
登录后复制

好了,本期的分享就到此结束了,有兴趣的小伙伴可以自行去实践学习。

以上是13个Python必备的知识建议收藏的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何在LAMP架构下高效整合Node.js或Python服务? 如何在LAMP架构下高效整合Node.js或Python服务? Apr 01, 2025 pm 02:48 PM

在LAMP架构下整合Node.js或Python服务许多网站开发者都面临这样的问题:已有的LAMP(Linux Apache MySQL PHP)架构网站需要...

使用Scapy爬虫时,管道持久化存储文件无法写入的原因是什么? 使用Scapy爬虫时,管道持久化存储文件无法写入的原因是什么? Apr 01, 2025 pm 04:03 PM

使用Scapy爬虫时,管道持久化存储文件无法写入的原因探讨在学习使用Scapy爬虫进行数据抓取时,经常会遇到一�...

Python进程池处理并发TCP请求导致客户端卡死的原因是什么? Python进程池处理并发TCP请求导致客户端卡死的原因是什么? Apr 01, 2025 pm 04:09 PM

Python进程池处理并发TCP请求导致客户端卡死的解析在使用Python进行网络编程时,高效处理并发TCP请求至关重要。...

如何查看Python functools.partial对象内部封装的原始函数? 如何查看Python functools.partial对象内部封装的原始函数? Apr 01, 2025 pm 04:15 PM

深入探讨Pythonfunctools.partial对象的查看方法在使用Python的functools.partial...

如何解决Linux终端中查看Python版本时遇到的权限问题? 如何解决Linux终端中查看Python版本时遇到的权限问题? Apr 01, 2025 pm 05:09 PM

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

Python跨平台桌面应用开发:哪个GUI库最适合你? Python跨平台桌面应用开发:哪个GUI库最适合你? Apr 01, 2025 pm 05:24 PM

Python跨平台桌面应用开发库的选择许多Python开发者都希望开发出能够在Windows和Linux系统上都能运行的桌面应用程...

Python沙漏图形绘制:如何避免变量未定义错误? Python沙漏图形绘制:如何避免变量未定义错误? Apr 01, 2025 pm 06:27 PM

Python入门:沙漏图形绘制及输入校验本文将解决一个Python新手在沙漏图形绘制程序中遇到的变量定义问题。代码...

如何用Python高效统计并排序大型商品数据集? 如何用Python高效统计并排序大型商品数据集? Apr 01, 2025 pm 08:03 PM

数据转换与统计:高效处理大型数据集本文将详细介绍如何将一个包含商品信息的数据列表,转换为另一个包含...

See all articles