2. Python 用于计算,,1.Numbersa


1.Numbers

a.The integer numbers (e.g.2,4,20) have typeint, the ones with a fractional part (e.g.5.0,1.6) have typefloat.

b.Division (/) always returns a float. To dofloor divisionand get an integer result (discarding any fractional result) you can use the//operator; to calculate the remainder you can use%

c.use the**operator to calculatepowers

d. In interactive mode, the last printed expression is assigned to the variable_.for example:

>>> tax = 12.5 / 100>>> price = 100.50>>> price * tax12.5625>>> price + _113.0625>>> round(_, 2)113.06
e.we can use Python for more complicated tasks than adding two and two together.
>>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1>>> while b < 10:...     print(b)...     a, b = b, a+b...112358
2.Strings
a.Strings can be enclosed in single quotes (‘...‘) or double quotes ("...") with the same result.\can be used to escape quotes:
>>> ‘spam eggs‘  # single quotes‘spam eggs‘>>> ‘doesn\‘t‘  # use \‘ to escape the single quote..."doesn‘t">>> "doesn‘t"  # ...or use double quotes instead"doesn‘t">>> ‘"Yes," he said.‘‘"Yes," he said.‘>>> "\"Yes,\" he said."‘"Yes," he said.‘>>> ‘"Isn\‘t," she said.‘‘"Isn\‘t," she said.‘
b.The built-in functionlen()returns the length of a string:
c. Strings can be concatenated (glued together) with the+operator, and repeated with*:
>>> # 3 times ‘un‘, followed by ‘ium‘>>> 3 * ‘un‘ + ‘ium‘‘unununium‘
d. Two or morestring literals(i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
>>> ‘Py‘ ‘thon‘‘Python‘
e. Strings can beindexed(subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:
>>> word = ‘Python‘>>> word[0]  # character in position 0‘P‘>>> word[5]  # character in position 5‘n‘
f. Indices may also be negative numbers, to start counting from the right,negative indices start from -1:
>>> word[-1]  # last character‘n‘>>> word[-2]  # second-last character‘o‘>>> word[-6]‘P‘
g. In addition to indexing,slicingis also supported. While indexing is used to obtain individual characters,slicingallows you to obtain substring:
>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)‘Py‘>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)‘tho‘
>>> word[:2]   # character from the beginning to position 2 (excluded)‘Py‘>>> word[4:]   # characters from position 4 (included) to the end‘on‘
3.Lists
a. list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.
>>> squares = [1, 4, 9, 16, 25]>>> squares[1, 4, 9, 16, 25]
b. Like strings (and all other built-insequencetype), lists can be indexed and sliced:
>>> squares[0]  # indexing returns the item1>>> squares[-1]25>>> squares[-3:]  # slicing returns a new list[9, 16, 25]
c. Unlike strings, which areimmutable, lists are amutabletype, i.e. it is possible to change their content:
>>> cubes = [1, 8, 27, 65, 125]  # something‘s wrong here>>> 4 ** 3  # the cube of 4 is 64, not 65!64>>> cubes[3] = 64  # replace the wrong value>>> cubes[1, 8, 27, 64, 125]
d. You can also add new items at the end of the list, by using theappend()method(we will see more about methods later):
>>> cubes.append(216)  # add the cube of 6>>> cubes.append(7 ** 3)  # and the cube of 7>>> cubes[1, 8, 27, 64, 125, 216, 343]
e. It is possible to nest lists (create lists containing other lists), for example:
>>> a = [‘a‘, ‘b‘, ‘c‘]>>> n = [1, 2, 3]>>> x = [a, n]>>> x[[‘a‘, ‘b‘, ‘c‘], [1, 2, 3]]>>> x[0][‘a‘, ‘b‘, ‘c‘]>>> x[0][1]‘b‘

参考:

The Python Tutorial

2. Python 用于计算

评论关闭