首页 > 编程语言 > 详细

Python之基础(一)

时间:2014-03-29 20:10:34      阅读:523      评论:0      收藏:0      [点我收藏+]

数学计算

要利用相关的数学计算函数,首先需要把数学模块包含进来:

>>>import math

进行计算:

>>> math.pi
3.141592653589793
>>> math.sqrt(90)
9.486832980505138
>>> 

随机数

包含随机函数:

bubuko.com,布布扣
>>> random
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    random
NameError: name random is not defined
>>> import random
>>> random.random()
0.5821979178456164
>>> random.choice([1, 3, 2, 4, 5])
2
bubuko.com,布布扣

  Python also includes more exotic numeric objects—such as complex, fixed-precision, and rational numbers, as well as sets and Booleans—and the third-party open source extension domain has even more (e.g., matrixes and vectors, and extended precision numbers). We’ll defer discussion of these types until later in this chapter and book.

  So far, we’ve been using Python much like a simple calculator; to do better justice to its built-in types, let’s move on to explore strings.

 

String

 

bubuko.com,布布扣
>>> S = Spam
>>> len(S)
4
>>> S[0]
S
>>> S[-1]
m
>>> 
bubuko.com,布布扣

  注意:

  In Python, we can also index backward, from the end—positive indexes count from the left, and negative indexes count back from the right:

bubuko.com,布布扣
>>> S[-1]
m
>>> S[-2]
a
>>> S[-3]
p
>>> S[-4]
S
>>> 
bubuko.com,布布扣
>>> S *= 8
>>> S
SpamSpamSpamSpamSpamSpamSpamSpam
>>> 

Python之基础(一),布布扣,bubuko.com

Python之基础(一)

原文:http://www.cnblogs.com/wiessharling/p/3632453.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!