• Python 入门日记(二)—— 变量与简单数据类型


    2020.07.05 Python 入门的 Day2

    成就:部分字符串、整数、浮点数操作

    字符串:

    • 字符串就是一系列字符。在 Python 中,用括号括起来的都是字符串,其中的引号可以是单引号,也可以是双引号。
    • 方法是 Python 可对数据执行的操作。每个方法后面都跟着一对括号,用以提供必要的信息。
    • 通过方法对字符串的改动是暂时的,方法结束后,字符串就会恢复。永久保存改动的方式就是通过赋值记录下来。
    name = "Everything has changed!"
    print(name)
    
    # .title() 是一个使字符串中的每个单词首字母大写、其余字母小写的方法
    print(name.title())
    
    # 使方法的结果得以保存的方法就是赋值
    name = name.title()
    print(name)
    • title() 的作用是使字符串中所有单词的首字母都大写、除了首字母以外的其他字母都小写的方法。
    • upper() 的作用是使字符串中所有单词的字母都大写。
    • lower() 的作用是使字符串中所有单词的字母都小写。
    • replace() 的作用是将字符串中的特定的单词全部替换为另一个单词。
    name = "Everything hAs cHAnged aGain!"
    print(name.title())
    print(name.upper())
    print(name.lower())
    name.replace('aGain', 'again and again')
    • Python 用加号(+)来合并字符串,这种合并字符串的方法称为拼接
    • 在编程中,空白泛指任何非打印字符,如空格、制表符合换行符。字符组合 可用于添加制表符, 用于添加换行符。
    • 输出字符组合 或 需要用 \t 或 \n。
    • rstrip() 是删除字符串末尾空白的方法,lstrip() 是删除字符串开头空白的方法,strip() 是删除字符串两端空白的方法。
    name = '     Everything has changed again and again!     '
    thought = "I think."
    print(name.rstrip())
    print(name.lstrip())
    print(name.strip())
    # 删除字符串末尾、开头、两端的空白
    
    name = name.strip() + "," + " " + thought
    print(name)
    # 字符串拼接
    
    print("	" + name)
    print("
    	" + name)
    
    print("\t")
    print("\n")
    print("\t\n")
    print("	\t")
    • 在单引号(')括起来的字符串中使用(')会导致错误。

    数字:

    • 整数支持加(+)、减(-)、乘(*)、除(/)运算。
    • Python 将带小数点的数字都成为浮点数
    • Python 使用两个乘号表示乘方运算。
    • 函数 str() 将非字符串值表示为字符串。
    print(1 + 7)
    print(19 - 11)
    print(2 * 4)
    print(16 / 2)
    print(2 ** 3)
    print(2 ** 0.5)
    
    age = 20
    message = "Happy " + str(age) + "th Birthday!"
    print(message)
    
    # Twenty is also a good thing!

    Python 之禅:

    import this
    • The Zen of Python, by Tim Peters
    • Beautiful is better than ugly.
    • Explicit is better than implicit.
    • Simple is better than complex.
    • Complex is better than complicated.
    • Flat is better than nested.
    • Sparse is better than dense.
    • Readability counts.
    • Special cases aren't special enough to break the rules.
    • Although practicality beats purity.
    • Errors should never pass silently.
    • Unless explicitly silenced.
    • In the face of ambiguity, refuse the temptation to guess.
    • There should be one-- and preferably only one --obvious way to do it.
    • Although that way may not be obvious at first unless you're Dutch.
    • Now is better than never.
    • Although never is often better than *right* now.
    • If the implementation is hard to explain, it's a bad idea.
    • If the implementation is easy to explain, it may be a good idea.
    • Namespaces are one honking great idea -- let's do more of those!
  • 相关阅读:
    OpenStack网卡桥接问题
    Linux DD添加swap分区
    OpenStack KVM嵌套虚拟化的配置
    OpenStack KVM嵌套虚拟化的配置
    received packet with own address as source address
    mysql ERROR 1040 (08004): Too many connections
    openstack以其他tenant用户创建实例
    OpenStack Controller HA (3)
    OpenStack Controller HA (2)
    OpenStack controller HA
  • 原文地址:https://www.cnblogs.com/A-Tree/p/13252445.html
Copyright © 2020-2023  润新知