python基础知识

C:\Users\Administrator>python
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

>>> 1+3
4
>>> 1+3.
4.0

>>> 9-7.0
2.0
>>> 2-4
-2
>>> 2-4.0
-2.0

>>> 6*5
30
>>> 6*5.0
30.0

>>> 4/6
0
>>> 4/6.
0.66666666666666663

整除,即保留整数部分

>>> 6//2   
3
>>> 6//4
1

求余

>>> 8%3
2
>>> 8%4
0

平方

>>> 3**2
9
>>> 3**3
27
>>> -3**3
-27
>>> (-3)**3
-27

内建函数
平方

>>> pow(2,3)
8

绝对值

>>> abs(-12)
12

将浮点数四舍五入成最接近的整数值

>>> round(1.0/2.0)  
1.0

模块

>>>import math
>>>math.floor(32.9)
32.0
>>>int(math.floor(32.9))
32
>>>from math import sqrt
>>>sqrt(9)
3.0
>>>import cmath
>>>cmath.sqrt(-1)
1j
>>>cmath.sqrt(1)
(1+0j)
>>>(1+3j)*(9+4j)
(-3+31j)

字符串表示
值被转换成字符串的两种机制,一种是通过str函数,将值转换成合理形式的字符串,另一种是通过repr函数,它会创建一个字符串,以合法的python表达式的形式来表示值。
str与int,long一样是一种数据类型,而repr仅仅是一种函数

>>>print repr("hello world")
'hello world'
>>>print repr(1000L)
1000L
>>>print str("hello world")
hello world
>>>print str("1000L")
1000
>>>temp=35
>>>print "the temperature is" + str(temp)
the temperature is 35

input和raw_input的区别

>>> input("enter a number:")
enter a number:3
3
>>> raw_input("enter a number:")
enter a number:3
'3'
>>> input("enter a number:")
enter a number:xshow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'xshow' is not defined
>>> input("enter a number:")
enter a number:"xshow"
'xshow'

长字符串

>>> print '''this is a very long string
... it continus here
... and it's not over yet
... "hello,world!"
... still here.'''
this is a very long string
it continus here
and it's not over yet
"hello,world!"
still here. 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值