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课程咨询
。挫伤用什么药好的快口腔溃疡缺少哪种维生素
想要祛湿吃什么药
如何才能治疗眼睛痒
阿莫西林颗粒儿童服用好吗
上一篇: “巨人”夫妻:张常宁193,吴冠希211,欧洲杯最高伴娘团世界纪录呀
下一篇: 鸟类有情绪吗?
- .中小学生家教被禁止,中小学生表示不服:我不是在职教师,能怎么罚我
- .南开大学个人化宿舍火了,自选室友不是事,夫妻寝室才让人羡慕
- .未来很吃香的教师编制,并非是中小学老师,这类教师或顺利逆袭
- .5+2课后服务于落地后,家长抱怨没晚餐,如今午餐和晚餐都来了
- .补课班一时间关停,这类培训班却获教育部支持,或成为家长下个目标
- .食堂阿姨“手抖”成习,剩菜宁愿倒掉也不多给教职员,原因很现实
- .公务人员待遇将发生变化,绩效奖金或将下调,部分地区已开始实施
- .双减下教师准许补课?但有4个前提条件,家长:不好意思开口
- .28岁孙娜恩,颜值萌芽,腹肌显露,自信的“无瑕美人”
- .Red Velvet涩琪,伦敦演奏会后照,秀出可爱小肚皮
- .1999年失踪幼子被埋自家,十九年后发现,父母哭喊:宁愿不挖出来
- .BLACKPINK Lisa回归前,不愧是“天生名人”,自恋又漂亮
- .让你曾说越来越舒服的7条小建议
- .古天乐《明日战记》Extreme预告和剧照 大战将至
- .让一个人变得愈发好的9种习惯
- .一周热闻|俄罗斯将退出国际空间站、雪碧拟放弃历史性绿瓶、安哥拉发现300年最大粉钻……
- .警惕“去中国化”陷阱——点评西方学者的中国西南边疆史研究
- .致敬山东教育品牌|威海海洋职业学院明德学院“三化”文化以人为本实践
- .他火了!曾被质疑“谈渴望?配吗!”
- .封“神”纪(84)|90后网络作家“那一只蚊子”:写小说像角色换用,技能越多小说越好看