python 3.7
IDE: Spyder
整型和浮点型的基本用法与其他语言的使用方法是一样的。基本用法:x = 3print(type(x)) # Prints '
上述程序的而运行结果如下:
Booleans: python 常常使用英文字母来实现逻辑运算,而不是使用操作符&&,||等。使用详解:t = Truef = Falseprint(type(t)) # Prints '
步骤三程序的运行结果如下所示:
Python支持字符串类型的操作。使用如下:hello = 'hello' # String literals can use single quotesworld = 'world' # or double quotes; it does not matter.print(hello) # Prints 'hello'print(len(hello)) # String length; prints '5'hw = hello + ' ' + world # String concatenationprint(hw) # prints 'hello world'hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formattingprint(hw12) # prints 'hello world 12'
运行结果:hello5hello worldhello world 12
string拥有这许多有用的方法。如下: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'
运行结果如下:HelloHELLO hello hello he(ell)(ell)oworld