记录python若干功能特点,帮助快速了解python语法
#!/usr/bin/env python # 验证若干python功能 import pandas as pd import numpy as np import time import datetime from string import Template if __name__ == "__main__": # 格式化字符串,带参数名称 tmpl = Template("Hello, $who! $what enough for ya?") str2 = tmpl.substitute(who="Mars", what="Dusty") print(str2) # 格式化一个字符串...直接使用外部变量 day = 20230308 str3 = f""" today is {day} . """ print("str3:", str3) # 格式化一个字符串,通过传入对应位置参数赋值 str4 = " paraA:{} , paraB:{} , paraC:{}, paraD:{}" str5 = str4.format("A", "B", "C", day) print("str4:", str4) print("str5:", str5) # 用get方法访问字典,可以避免程序出现错误或异常 dic1: dict = {'name': 'henry', 'age': "123"} print("dic1.get name:", dic1.get('name', "name_bk")) print("dic1.get name2:", dic1.get('name2', "name_bk")) # 用作布尔表达式(如用作if语句中的条件)时,下面的值都将被解释器视为假: # False None 0 "" () [] {} if None: print("true") else: print(None) # 程序里面的断言 age = -1 assert (0 < age < 100, "The age must be realistic") print("age:", age)
标签:入门,python,age,dic1,print,import,快速,name From: https://www.cnblogs.com/music-liang/p/17196721.html