Python基本数据操作(1)字符/数字

来源:互联网 发布:巴比伦空中花园知乎 编辑:程序博客网 时间:2024/06/16 10:51
x = 3print(type(x)) # Prints "<type 'int'>"print(x)      # Prints "3"print(x + 1 )  # Addition; prints "4"print(x - 1)   # Subtraction; prints "2"print(x * 2 )  # Multiplication; prints "6"print(x ** 2) # Exponentiation; prints "9"x += 1print(x)  # Prints "4"x *= 2print(x)  # Prints "8"y = 2.5print(type(y) )print(y, y + 1, y * 2, y ** 2 )t = Truef = Falseprint (type(t)) # Prints "<type 'bool'>"print( t and f)# Logical AND; prints "False"print (t or f ) # Logical OR; prints "True"print (not t)   # Logical NOT; prints "False"print (t != f ) # Logical XOR; prints "True"  s = "hello"print (s.capitalize() ) # Capitalize a string; prints "Hello"print (s.upper()  )    # Convert a string to uppercase; prints "HELLO"print (s.rjust(7)  )    # Right-justify a string, padding with spaces; prints "  hello"print (s.center(7) )    # Center a string, padding with spaces; prints " hello "print( s.replace('l', '(ell)') ) # Replace all instances of one substring with another;                               # prints "he(ell)(ell)o"print ('  world '.strip() ) # Strip leading and trailing whitespace; prints "world"
原创粉丝点击