首页 > 编程语言 >Python入门系列(九)pip、try except、用户输入、字符串格式

Python入门系列(九)pip、try except、用户输入、字符串格式

时间:2022-09-04 14:00:40浏览次数:69  
标签:format Python price except try print txt

pip

包含模块所需的所有文件。

检查是否安装了PIP

$ pip --version

安装

$ pip install package_name

使用包

import package_name

删除包

$ pip uninstall camelcase

列出包

pip list

Try Except

try:
  print(x)
except:
  print("An exception occurred")

您可以根据需要定义任意数量的异常块,例如,如果您想为特殊类型的错误执行特殊代码块

try:
  print(x)
except NameError:
  print("Variable x is not defined")
except:
  print("Something else went wrong")

如果没有引发错误,可以使用else关键字定义要执行的代码块

try:
  print("Hello")
except:
  print("Something went wrong")
else:
  print("Nothing went wrong")

如果指定了finally块,则无论try块是否引发错误,都将执行finally。

ry:
  print(x)
except:
  print("Something went wrong")
finally:
  print("The 'try except' is finished")

这对于关闭对象和清理资源非常有用

try:
  f = open("demofile.txt")
  try:
    f.write("Lorum Ipsum")
  except:
    print("Something went wrong when writing to the file")
  finally:
    f.close()
except:
  print("Something went wrong when opening the file")

作为Python开发人员,如果出现条件,您可以选择抛出异常。

x = -1

if x < 0:
  raise Exception("Sorry, no numbers below zero")

您可以定义要引发的错误类型,以及要打印给用户的文本。

x = "hello"

if not type(x) is int:
  raise TypeError("Only integers are allowed")

用户输入

username = input("Enter username:")
print("Username is: " + username)

字符串格式

price = 49
txt = "The price is {} dollars"
print(txt.format(price)) # The price is 49 dollars

可以在花括号内添加参数,以指定如何转换值

price = 49
txt = "The price is {:.2f} dollars"
print(txt.format(price)) # The price is 49.00 dollars

如果要使用更多值,只需在format()方法中添加更多值

quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} dollars."
print(myorder.format(quantity, itemno, price))

您可以使用索引号(大括号{0}内的数字)确保将值放置在正确的占位符中

quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars."
print(myorder.format(quantity, itemno, price))

此外,如果要多次引用同一值,请使用索引号

age = 36
name = "John"
txt = "His name is {1}. {1} is {0} years old."
print(txt.format(age, name))

命名索引

myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname = "Ford", model = "Mustang"))

您的关注,是我的无限动力!

公众号 @生活处处有BUG

标签:format,Python,price,except,try,print,txt
From: https://www.cnblogs.com/bugs-in-life/p/16654985.html

相关文章

  • 10个python初学者常犯的错误
    下面10个python初学者常犯的错误,并不是真正的代码错误,代码照样可以运行,但是不够pythonic,因为python有自己的语言哲学,在代码的处理上有传统语言无法比拟的简洁性和便捷性。......
  • 总结90条写Python程序的建议
    首先建议1、理解Pythonic概念—-详见Python中的《Python之禅》建议2、编写Pythonic代码(1)避免不规范代码,比如只用大小写区分变量、使用容易混淆的变量名、害怕过......
  • 掌握 Python 中下划线的 5 个潜规则
    本文将介绍Python中单下划线和双下划线("dunder")的各种含义和命名约定,名称修饰(namemangling)的工作原理,以及它如何影响你自己的Python类。单下划线和双下划线在Python......
  • 40 个好用的 Python 技巧
    Python简单易学,现今非常流行。Python被用于各种场景,如数据科学、机器学习、web开发、脚本编制、自动化等等。目录01列表推导式02枚举函数03通过函数返回多个值04......
  • 10 个实用的 Python 编程技巧
    字典翻转首先我们来看字典的翻转,假设我们有下面类似的一个字典对象与数据car_dict={"brand":"Tesla","model":"ModelY","year":2017}倘若我们......
  • Python 中的深拷贝和浅拷贝
    一、结论首先说结论:深拷贝出来的对象就是完完全全的新对象,不管是对象本身(id),还是对象中包含的子对象,都和原始对象不一样;浅拷贝出来的对象就是外新内旧的对象,对象本身(id......
  • Python 中的冒泡排序和解释 - 教程。
    Python中的冒泡排序和解释-教程。冒泡排序是您比较相邻值并相应地交换它的地方。这种排序算法有一些应用,其中一些是对数据库中的数据进行排序,例如:按价格对亚马逊产品进......
  • Python|使用Python实现png格式文件转tif格式文件
    一、TIF/TIFF介绍标签图像文件格式(TagImageFileFormat,简写为TIFF)是一种灵活的位图格式,主要用来存储包括照片和艺术图在内的图像,是一种常见的遥感影像存储格式。二、......
  • Python中的石头剪刀布游戏
    Python中的石头剪刀布游戏继续阅读WordPress继续阅读知乎版权声明:本文为博主原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接和本声明本文链接:https:/......
  • Python|使用Python实现按掩膜提取
    使用GDAL中的Warp函数实现了ArcGIS中的按掩膜提取功能,直接上代码:fromosgeoimportgdalinput_raster=r""#输入栅格路径#orasanalternativeiftheinputisa......