Blame | Last modification | View Log | Download
4.1 Indentationn = 9r = 1while n > 0:r = r * nn = n - 14.3 Variables>>> x = "Hello">>> print(x)Hello>>> x = 5>>> print(x)5>>> x = 5>>> print(x)5>>> del x>>> print(x)Traceback (most recent call last):File "<stdin>", line 1, in <module>NameError: name 'x' is not defined>>>4.4 Expressionsx = 3y = 5z = (x + y) / 24.5 Stringsx = "Hello, World"x = "\tThis string starts with a \"tab\"."x = "This string contains a single backslash(\\)."x = "Hello, World"x = 'Hello, World'x = "Don't need a backslash"x = 'Can\'t get by without a backslash'x = "Backslash your \" character!"x = 'You can leave the " alone'x = """Starting and ending a string with triple " characterspermits embedded newlines, and the use of " and ' withoutbackslashes"""4.6 Numbers>>> 5 + 2 - 3 * 21>>> 5 / 2 # floating-point result with normal division2.5>> 5 / 2.0 # also a floating-point result2.5>>> 5 // 2 # integer result with truncation when divided using '//'2>>> 30000000000 # This would be too large to be an int in many languages30000000000>>> 30000000000 * 390000000000>>> 30000000000 * 3.090000000000.0>>> 2.0e-8 # Scientific notation gives back a float.2e-08>>> 3000000 * 30000009000000000000>>> int(200.2)200>>> int(2e2)200>>> float(200)200.04.6.4 Complex Numbers>>> (3+2j)(3+2j)>>> 3 + 2j - (4 + 4j)(-1-2j)>>> (1 + 2j) * (3 + 4j)(-5+10j)>>> 1j*1j(-1+0j)>>> z = 3+5j>>> z.real3.0>>> z.imag5.04.6.5 Advanced complex-number functions>>> import cmath>>> cmath.sqrt(-1)1j4.8 Getting input>>> name = input("Name? ")Name? Jane>>> print(name)Jane>>> age = int(input("Age? "))Age? 28>>> print(age)28>>>