2. Python 用于计算

本文介绍了Python的基础知识,包括数值类型如整数(int)和浮点数(float),字符串操作如索引和切片,以及列表的基本使用方法。通过示例展示了如何进行简单的数学运算,字符串的拼接和重复,列表的索引及内容修改。

1.Numbers

a.The integer numbers (e.g. 24, 20) have type int, the ones with a fractional part (e.g. 5.0, 1.6) have type float.

b.Division (/) always returns a float. To do floor division and 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 calculate powers  

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

>>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.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 ... 1 1 2 3 5 8
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 function len() 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 more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
>>> 'Py' 'thon'
'Python'
e. Strings can be indexed (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, slicing is also supported. While indexing is used to obtain individual characters, slicing allows 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-in sequence type), lists can be indexed and sliced:
>>> squares[0]  # indexing returns the item 1 >>> squares[-1] 25 >>> squares[-3:] # slicing returns a new list [9, 16, 25]
c. Unlike strings, which are immutable, lists are a mutable type, 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 the append() 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'

参考:

转载于:https://www.cnblogs.com/qlqiu/p/7742487.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值