zoukankan      html  css  js  c++  java
  • Python Learning(2) 变量

    Python编程从入门到实践:

    """
    chapter2 变量和简单数据类型
    """
    # 2.1 hello world
    print("hello world")
    
    # 2.2 变量
    message = "hello python world!"
    print(message)
    
    message = "hello python crash Course world!"
    print(message)
    
    # 2.2.1 python中变量应该尽量使用小写 避免不合理的命名
    
    # 2.3 字符串
    message1 = 'i am good "man"'
    message2 = "i am 'good' man "
    message3 = "i am good man"
    message4 = 'i am good man '
    
    # 2.3.1 字符串的大小写
    name1 = 'ada lovelace'
    print(name1.title())  # .title() 以首字母大写的方式显示每个单词
    name2 = "Ada Lovelace"
    print(name2.upper())
    name3 = "Ada Lovelace"
    print(name3.lower())
    
    # 2.3.2 合并字符串
    first_name = 'ada'
    last_name = 'lovelace'
    full_name = first_name + last_name
    full_name2 = first_name + " " + last_name
    print(full_name)
    print(full_name2)
    print('hello,' + full_name2.title() + "!")
    
    # 2.3.3 使用制表符或者换行符来添加空白
    print('python')
    print('	python')  # 	 制表符
    
    print('python')
    print('languages:
    python
    c
    java')  # 换行符
    
    # 2.3.4 删除空白
    favorite_language = 'python '
    print(favorite_language + "!")  # python !
    print(favorite_language.rstrip() + "!")  # python!  使用rstrip去除字符串末尾的空白
    
    favorite_language1 = ' python '
    print("!" + favorite_language1)  # ! python
    print("!" + favorite_language1.lstrip())  # !python 使用lstrip去除字符串末尾的空白
    
    favorite_language2 = ' python '
    print("!" + favorite_language2 + '!')
    print("!" + favorite_language2.strip() + '!')  # !python! 使用strip去除字符串前后的空格
    
    # 2.4 数字
    # 2.4.1 整数
    print(100 + 200)
    print(100 - 200)
    print(0.1 - 0.2)
    print(200 * 100)
    print(0.1 * 2)
    print(3 / 2)
    print(2 ** 4)  # 2的4次方
    
    #  2.4.2 浮点数
    print(0.1 * 0.2)  # 0.020000000000000004
    print(0.3 / 0.2)  # 1.4999999999999998
    print(0.1 + 0.2)  # 0.30000000000000004
    print(3 * 0.1)  # 0.30000000000000004
    
    #  2.4.3 使用str()函数避免类型错误
    age = 23
    # message = 'happy ' + age + "birthday!"
    # print(message)  # TypeError: can only concatenate str (not "int") to str
    message = 'happy ' + str(age) + "birthday!"
    print(message)
    
    # 2.5 注释
    # 你好
    print('你好')
    
    '''
    你好吗
    '''
    print('你好吗')
    
    import this # module level import should be located at the top of this file
    print(this)
    '''
    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!
    '''
    
    
    
    
  • 相关阅读:
    java中volatile关键字的含义
    2019年个人总结
    跟随Javac代码来解答字节码的疑惑
    Python装饰器实现带参数和不带参数
    try with resource当中你没有注意到点。。
    IDEA中,已经被加到版本库的文件如何在提交的时候忽略它们
    连接mysql客户端报错: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'
    从字节码层次看i++和++i
    对lambda表达式的字节码实现个人理解
    解决mvn clean install的报错The packaging for this project did not assign a file to the build artifact
  • 原文地址:https://www.cnblogs.com/DiZhang/p/12544800.html
Copyright © 2011-2022 走看看