Python最常用的函数、基础运算符有哪些?你都知道吗
时间:2023-02-28 12:18:01
>>>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转换成其他JavaScript的功能
有时必需运用于另一个 python 文件之中的JavaScript,这回事很简单,就像运用于 import 关键字转换成任何模块化一样。
「vacations.py」
# Initialize values
vacation1 = "Summer Vacation"
vacation2 = "Winter Vacation"
比如在一个大JavaScript之中去引用上面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. 偏差所致处理
Smalltalk步骤之中难免不会碰到偏差和所致,所以我们要及时处理它,避免对后续字符串造成因素。
所有的标准所致都运用于类来借助于,都是codice_Exception的团员,都从codice_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课程咨询
。挫伤用什么药好的快口腔溃疡缺少哪种维生素
想要祛湿吃什么药
如何才能治疗眼睛痒
阿莫西林颗粒儿童服用好吗
上一篇: 由华德2010方形保险丝代理商---顺海信息技术提供
下一篇: 鸟类有情绪吗?
- .号称“汤都”的神都洛阳,来到这里有三种“汤”不得不吃完
- .刚刚!“历史性”COP28协议达成,首次包含减少骨骸燃料承诺
- .我妈一辈子吃不胖的秘诀,原来都在这6道蒸菜人身!
- .冬天早餐在家吃,早起半小时,一桌丰盛点心的营养早餐就上桌了
- .Zara和H&M即将发布业绩,但投资者只关心Shein又赢了多少
- .B站百万粉丝网红,鲁菜师父陈宗明商家了
- .冬天要多喝这个煮,我家经常做,做法简单,每次一大盆不够喝
- .36道红烧菜的作法,下饭神器,一学就会
- .经合一个组织:2023年10月该一个组织成员国失业率为4.9%
- .巧克力舒芙蕾蛋糕,忘忘忘了!
- .报告显示2024年德国经济可能终于陷入衰退
- .除夕年夜饭,无论贫富,都要吃这4道开运菜,寓意龙年拣财拣福
- .入冬后,肉比这道菜还美味,它鲜嫩可口,营养丰富,真是太满足了
- .欧盟拟出首个“零工劳动法”! 优步和Deliveroo等平台员工将享更多福利
- .年龄越大,越要多喝茶?居多:多喝这四种茶,越喝越年轻
- .夏天,我们应该多吃鱼,湖北人常吃这种鱼,肉质肥厚,无刺,必需营养,下坠顺
- .尼泊尔央行前行长:美联储明年3月前降息预期过于乐观
- .市场上的"溶化猪蹄"从哪来的?冷藏柜老板说漏嘴,你听完再吃
- .山东“橘子馒头”、足球比赛足球饽饽爆火!网友:夸到词穷
- .特斯拉调离逾200万辆汽车 以修复辅助驾驶系统安全缺陷