03-Python基础

01-math.pow(a,b)语句

表示a  的b 次方

# 求水仙花数
if math.pow((i%10),3)+math.pow((i//10%10),3)+math.pow((i//100),3) == i:

02-enumerate()内置函数:

对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值

year = [82,88,98,86,92,0,99]
for index,value in enumerate(year):
    print(index,value)

03-find( )函数

通过find( )函数进行查找字符串的时候,如果字符串中存在所需要查找的字符,则返回字符所在的位置,如果未查找到,则返回   '-1'

    for item in lst:
        if item.find(num) != -1:
            cart.append(item)
            break

04-逆向遍历

for i in range(len(cart)-1,-1,-1):
    print(cart[i])           # cart 为列表,一定要给步长‘-1’

05-enumerate()函数

输出元组及其序号,序号默认为从0开始排序,可以通过‘+’,‘-’进行调整

dic_coffee = ('蓝山','卡布奇洛','拿铁','女王咖啡','皇家咖啡','美丽与哀愁')
print('本店经营的咖啡有:')
for index,item in enumerate(dic_coffee):
    print(index+1,'.',item,end='\t\t')

06-重复内容尽量用函数表达

07-主程序写入方式:main   会自动填充()

if __name__ == '__main__':

08-except Exception as e:(抛出错误类型)

    try:
        a = int(input('请输入第一条边:'))
        b = int(input('请输入第二条边:'))
        c = int(input('请输入第三条边:'))
        is_triangle(a,b,c)
    except Exception as e:
        print(e)

09-抛出异常,切打断函数:(raise Exception)

    if a <= 0 or b <= 0 or c <= 0:
        raise Exception('三条边长不能为0和负数')

10-Python strip()方法

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

s_lst = s.strip('#')    # 去掉首尾的‘#’

11-从类函数外部调取其内部函数

class Instrument():            # 定义类
    def make_sound(self):
        pass
class Erhu(Instrument):        # 子类
    def make_sound(self):
        print('二胡在演奏')
def play(a):                   # 子类
    a.make_sound()
class Bird():                  # 可变
    def make_sound(self):
        print('小鸟在唱歌')
if __name__ == '__main__':
    play(Erhu())               # 调取函数
    play(Bird())

12-导入表格

需要先导入:import prettytable

import prettytable as pt

# 显示坐席
def show_ticket(row_num):
    tb = pt.PrettyTable()
    tb.field_names = ['行号','座位1','座位2','座位3','座位4','座位5']
    for i in range(row_num):
        lst = [f'第{i+1}行','有票','有票','有票','有票','有票']
        tb.add_row(lst)
    print(tb)

13-strftime

strftime():  (格式化日期时间的函数)

strptime():   (由字符串转为日期型的函数)

 s = f'用户名{user_name},登录时间:{time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime(time.time()))}'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值