栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python学习第八天

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python学习第八天

floor函数

floor 下取整函数。

import math

math.floor( x )

字符串分割

字符串分割,分割后是列表,默认以空格

def first_word(text: str) -> str:
    """
    returns the first word in a given text.
    """
    aa=text.split(' ')[0]
   
    return aa


if __name__ == "__main__":
    print("Example:")
    print(first_word("Hello world"))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert first_word("Hello world") == "Hello"
    assert first_word("a word") == "a"
    assert first_word("hi") == "hi"
    print("Coding complete? Click 'Check' to earn cool rewards!")

分割。a.split(' ')[0]读取字符串以空格分割的第一个元素。

strip()

去除字符串两边字符,strip('0')去除字符串两边的零。

def end_zeros(num: int) -> int:
    b=str(num)
    j=0
    return len(b)-len(b.strip('0'))
   
if __name__ == "__main__":
    print("Example:")
    print(end_zeros(0))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert end_zeros(0) == 1
    assert end_zeros(1) == 0
    assert end_zeros(10) == 1
    assert end_zeros(101) == 0
    assert end_zeros(245) == 0
    assert end_zeros(100100) == 2
    print("Coding complete? Click 'Check' to earn cool rewards!")

列表转化为字符串形式。用join函数。

def solve_it(a):
    '''
    pythontip oj不同于传统oj,代码里面直接使用变量,无需要提前声明,免去复杂的输入解析
    life is short, so i user python~
    you can use variables a
    '''
    b=[]
    for i in a[0::2]:
        b.append(i)
    
    return(''.join(b))  #列表转化为字符串方式
a='12456'
print(solve_it(a))  # 答案需要输出

 列表逆序输出

def solve_it(a):
    '''
    pythontip oj不同于传统oj,代码里面直接使用变量,无需要提前声明,免去复杂的输入解析
    life is short, so i user python~
    '''
    b=[]
    for i in a[::-1]:
        b.append(i)

    return b
a='1234'
print(solve_it(a))  # 答案需要输出

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号