• Python数据结构:数字与字符串


    Python数据结构

    数字、字符串、

    数字

    Python Number 数据类型用于存储数值。

    Python Number 数据类型用于存储数值,包括整型、长整型、浮点型、复数。

    (1)Python math 模块:Python 中数学运算常用的函数基本都在 math 模块

    In [1]

    1. import math
    2. print(math.ceil(4.1)) #返回数字的上入整数
    3. print(math.floor(4.9)) #返回数字的下舍整数
    4. print(math.fabs(-10)) #返回数字的绝对值
    5. print(math.sqrt(9)) #返回数字的平方根
    6. print(math.exp(1)) #返回e的x次幂
    5
    4
    10.0
    3.0
    2.718281828459045
    

    (2)Python随机数

    首先import random,使用random()方法即可随机生成一个[0,1)范围内的实数

    In [4]

    1. import random
    2. ran = random.random()
    3. print(ran)
    0.8838912632723844
    

    randint()生成一个随机整数

    In [5]

    1. ran = random.randint(1,20)
    2. print(ran)
    1
    

    字符串

    字符串连接:+

    In [7]

    1. a = "Hello "
    2. b = "World "
    3. print(a + b)
    Hello World 
    

    重复输出字符串:*

    In [8]

    print(a * 3)
    Hello Hello Hello 
    

    通过索引获取字符串中字符[]

    In [9]

    print(a[0])
    H
    

    字符串截取[:] 牢记:左闭右开

    In [8]

    print(a[1:4])
    ell
    

    判断字符串中是否包含给定的字符: in, not in

    In [ ]

    1. print('e' in a)
    2. print('e' not in a)
    True
    False
    

    join():以字符作为分隔符,将字符串中所有的元素合并为一个新的字符串

    In [9]

    1. new_str = '-'.join('Hello')
    2. print(new_str)
    H-e-l-l-o
    

    字符串单引号、双引号、三引号

    In [ ]

    1. print('Hello World!')
    2. print("Hello World!")

    转义字符 \

    In [ ]

    1. print("The \t is a tab")
    2. print('I\'m going to the movies')
    The 	 is a tab
    I'm going to the movies
    

    三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保持一小块字符串的格式是所谓的WYSIWYG(所见即所得)格式的。

    In [1]

    1. print('''I'm going to the movies''')
    2. html = '''
    3. <HTML><HEAD><TITLE>
    4. Friends CGI Demo</TITLE></HEAD>
    5. <BODY><H3>ERROR</H3>
    6. <B>%s</B><P>
    7. <FORM><INPUT TYPE=button VALUE=Back
    8. ONCLICK="window.history.back()"></FORM>
    9. </BODY></HTML>
    10. '''
    11. print(html)
    I'm going to the movies
    
    
    Friends CGI Demo
    

    ERROR

    %s

    来源:https://www.weidianyuedu.com

  • 相关阅读:
    【andv】a-select 多条数据重复(搜索无效)的问题:
    RabbitMQ 基本介绍
    vue——响应式数据、双向数据绑定、filter过滤器、面试题
    【校招VIP】产品深度理解之热点事件分析
    ECharts的配置(一)
    nginx的安装及使用
    linux网络编程——UDP编程
    SpringMVC 02
    PostgreSQL下载和安装教程
    flutter仿支付宝余额宝年化收益折线图
  • 原文地址:https://blog.csdn.net/hdxx2022/article/details/127803870