您当前的位置:首页 >> 工业物联
工业物联

Python最常用的函数、基础句子有哪些?你都知道吗

发布时间:2025-08-05

xpression并作为 Python 运算符顺利完成正则表达式(从技术上说是一个前提条件一览表),引入globals和locals注释作为在实践中都和渐进命名空间。

>>>x = 7

>>> eval( '3 * x')

21

>>> eval( 'pow(2,2)')

4

>>> eval( '2 + 2')

4

>>> n= 81

>>> eval( "n + 4")

85

02 典型词组

在日常示例现实生活中都,只不过有很多典型的词组,用到频率比较较高,也是大家叫法的写法。

1. format正则表达式内嵌

format把正则表达式当成一个模板,通过广泛传播的给定顺利完成内嵌,比较简约且强大。

# 内嵌正则表达式

print( '{} {}'.format( 'hello', 'world'))

# 浮点数

float1 = 563.78453

print( "{:5.2f}".format(float1))

2. 连结正则表达式

用于+连结两个正则表达式。

string1 = "Linux"

string2 = "Hint"

joined_string = string1 + string2

print(joined_string)

3. if...else前提条件运算符

Python 前提条件运算符是通过一条或多条运算符的监督结果(True 或者 False)来要求监督的示例块。

其中都if...else运算符用来监督无需说明的情况下。

# Assign a numeric value

number = 70

# Check the is more than 70 or not

if(number>= 70):

print( "You have passed")

else:

print( "You have not passed")

4. for...in、while尿素运算符

尿素运算符就是遍历一个基因组,尿素去监督某个操作,Python 中都的尿素运算符有 for 和 while。

for尿素

# Initialize the list

weekdays = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

print( "Seven Weekdays are:")

# Iterate the list using for loop

forday inrange(len(weekdays)):

print(weekdays[day])

while尿素

# Initialize counter

counter = 1

# Iterate the loop 5 times

whilecounter < 6:

# Print the counter value

print( "The current counter value: %d"% counter)

# Increment the counter

counter = counter + 1

5. import导入其他CGI的功能

有时无需用于另一个 python 副本中都的CGI,这只不过很简单,就像用于 import 表单导入任何模组一样。

「vacations.py」

# Initialize values

vacation1 = "Summer Vacation"

vacation2 = "Winter Vacation"

比如在示意图CGI中都去重述上面vacations.py中都的示例。

# Import another python

importvacations asv

# Initialize the month list

months = [ "January", "February", "March", "April", "May", "June",

"July", "August", "September", "October", "November", "December"]

# Initial flag variable to print summer vacation one time

flag = 0

# Iterate the list using for loop

formonth inmonths:

ifmonth == "June"ormonth == "July":

ifflag == 0:

print( "Now",v.vacation1)

flag = 1

elifmonth == "December":

print( "Now",v.vacation2)

else:

print( "The current month is",month)

6. 一览表推导式

Python 一览表推导式大概一个或者多个迭代可用并能简约地创建数据可数的一种法则,它将尿素和前提条件说明结合,从而避开词汇冗长的示例,提较高示例运行灵活性。能熟练用于推导式也可以间接说明你不太可能超越了 Python 才行的程度。

# Create a list of characters using list comprehension

char_list = [ char forchar in"linuxhint"]

print(char_list)

# Define a tuple of websites

websites = ( "google.com", "yahoo.com", "ask.com", "bing.com")

# Create a list from tuple using list comprehension

site_list = [ site forsite inwebsites ]

print(site_list)

7. 只读副本

与计算的交互式Python最常用于的场景之一,比如去读取D盘中都CSV副本,然后重新写入数据再保留。这就无需python监督只读副本的操作,这也是才行要把握的框架职业技能。

#Assign the filename

filename = "languages.txt"

# Open file for writing

fileHandler = open(filename, "w")

# Add some text

fileHandler.write( "Bash")

fileHandler.write( "Python")

fileHandler.write( "PHP")

# Close the file

fileHandler.close

# Open file for reading

fileHandler = open(filename, "r")

# Read a file line by line

forline infileHandler:

print(line)

# Close the file

fileHandler.close

8. 腌渍和资料库

形如一览表、正则表达式、元组等基因组,都有腌渍和资料库的需求,因为我们无需从中都都从数据,所以这也是比较框架的职业技能。

var1 = 'Hello World!'

var2 = "zhihu"

print( "var1[0]: ", var1[ 0])

print( "var2[1:5]: ", var2[ 1: 5])

9. 用于运算符和类

运算符和类是一种封装好的示例块,可以让示例更加简约、简约、较高效、强壮,是python的框架词汇之一。

概念和调用运算符

# Define addition function

defaddition(number1, number2):

result = number1 + number2

print( "Addition result:",result)

# Define area function with return statement

defarea(radius):

result = 3.14* radius * radius

returnresult

# Call addition function

addition( 400, 300)

# Call area function

print( "Area of the circle is",area( 4))

概念和比如说化类

# Define the class

classEmployee:

name = "Mostak Mahmud"

# Define the method

defdetails(self):

print( "Post: Marketing Officer")

print( "Department: Sales")

print( "Salary: $1000")

# Create the employee object

emp = Employee

# Print the class variable

print( "Name:",emp.name)

# Call the class method

emp.details

10. 严重错误极度妥善处理

面向对象现实生活中都常常遇到严重错误和极度,所以我们要及时妥善处理它,避开对后续示例造成影响。

所有的标准极度都用于类来实现,都是渐进数据类型Exception的成员,都从渐进数据类型Exception继承,而且都在exceptions模组中都概念。

Python自动将所有极度名称放在内建命名空间中都,所以程序在不必导入exceptions模组即可用于极度。一旦激起而且并未捕获SystemExit极度,程序在监督就会终止。

极度的妥善处理现实生活、如何激起或抛极度及如何框架自己的极度类都是无需深入理解的。

# Try block

try:

# Take a number

number = int(input( "Enter a number: "))

ifnumber % 2== 0:

print( "Number is even")

else:

print( "Number is odd")

# Exception block

except(ValueError):

# Print error message

print( "Enter a numeric value")

小结

当然Python还有很多有用的运算符和法则,无需大家自己去总结,这里抛砖引玉,希望能为了让到无需的时在。

点这里👇关注我,记得标星哦~

CDA课程咨询

天津白癜风医院哪家好
眼睛干涩是什么病因怎么治疗
广州看不孕不育去什么医院最好
深圳男科医院哪家医院最好
南京白癜风正规的医院

上一篇: 十分钟了解交换机的网络建设应用领域问题

下一篇: PARKER派克液压系统快换接头简介-福乐尔

友情链接