Python最常用的基础语句分享!

本文介绍了Python编程中的一些常用基础语句,包括format字符串格式化、字符串连接、if...else条件语句以及for...in和while循环。这些语句在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值