Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1683062
  • 博文数量: 2273
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 22859
  • 用 户 组: 普通用户
  • 注册时间: 2020-11-26 14:30
个人简介

更多python、Linux、网络安全学习内容,可移步:www.oldboyedu.com或关注\"老男孩Linux\"公众号

文章分类

全部博文(2273)

文章存档

2024年(123)

2023年(643)

2022年(693)

2021年(734)

2020年(80)

我的朋友

分类: Python/Ruby

2023-12-01 11:02:12

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

  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

阅读(1309) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~