老男孩教育专注IT教育10余年,只培养IT技术精英

全国免费咨询电话(渠道合作):400-609-2893

Python最常用的基础语句有哪些?

老男孩IT教育

行业新闻

2023年4月21日 16:43

在学习或者工作中,通过Python进行编码的时候,经常会用到一些常用的句式,也就是所谓的基础语句。它们出现的频繁非常高,也是约定俗成的写法。那么Python最常用的基础语句有哪些?本文为大家简单介绍几个,看看你了解多少。

  在学习或者工作中,通过Python进行编码的时候,经常会用到一些常用的句式,也就是所谓的基础语句。它们出现的频繁非常高,也是约定俗成的写法。那么Python最常用的基础语句有哪些?本文为大家简单介绍几个,看看你了解多少。

Python基础语句

  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条件语句是通过一条或多条语句的执行结果来决定执行的代码块。其中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 note passed")

  4、for...in\while循环语句

  循环语句就是遍历一个序列,循环去执行某个操作,Python中的循环语句有for和while。

  for循环

  # Initialize the list

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

  print("Seven Weekdays are:n")

  # Iterate the list using for loop

  for day in range(len(weekdays)):

  print(weekdays[day])

  while循环

  # Initialize counter

  counter = 1

  # Inerate the loop 5 times

  while counter < 6:

  #print the counter value

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

  # Increment the counter

  counter = counter + 1

  老男孩教育是Python培训领域的专家,2012年就开展了Python培训,是行业较早的Python培训机构,积累了大量的Python培训教学经验,并能全局把控企业用人指标,科学的制定Python教学课程体系,满足5-8年职业生涯需求,让学员轻松拿下高薪职位!

  推荐阅读:

  Python实战小案例,值得收藏!

  Python中如何进行代码换行?

  Python中如何计算时间差?

本文经授权发布,不代表老男孩教育立场。如若转载请联系原作者。