Python最常用的函数、基础句子有哪些?你都知道吗
发布时间:2025-08-05
>>>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课程咨询
。天津白癜风医院哪家好眼睛干涩是什么病因怎么治疗
广州看不孕不育去什么医院最好
深圳男科医院哪家医院最好
南京白癜风正规的医院
上一篇: 十分钟了解交换机的网络建设应用领域问题
-
友邦寿险荣获2021年度卓越寿险品牌奖
2021年12月底8日,由证券之星主办的“电视新闻张艺兴·新征程”2021上半年年度娱乐活动圆满落幕,本次娱乐活动主旨是为了表彰这些为西方发展做出卓著贡献的优秀大型企业。本次策划上
- 她改进搀扶动作、摸黑洗净老年遗体 她是养护院里的贴心“好妹妹”
- 青鸟消防(002960.SZ)股东陈文佳减持时间段过半未减持
- 火车站遇借钱,众人责怪骗都不借,农民工借2000元,回家后惊喜来了
- 中环环保(300692.SZ)再次通过高新技术企业认作
- 马上评|14岁女生“一日写诗两千首”,又是幼儿在挖坑
- 天茂集团(000627.SZ):出售安盛天平股份的管理工作已全部完成
- 广州电动自行车周内上牌超20万辆
- 彩虹股份(600707.SH):持股5%以上股东中磁力彩虹质押3500万股
- 五年前向亲戚还债,只还了本金,亲戚说只还了利息,该怎么办
- 君实生物(01877.HK/688180.SH):创新构筑效益内核,打开长期效益空间