所谓覅破解 发表于 2018-8-15 11:51:06

python学习笔记(一)

获取用户输入:  
>>> data=input("Please Eneter you Think:")
  
          raw_input()
  
Please Eneter you Think:30
  
>>> print data
  
30
  

  
input与raw_input的区别:
  
input会假设用户输入的是合法的python表达式,要求用户带着引号(“”)输入
  
raw_input会把所有输入当做原始数据,然后将其放入字符串中
  
>>> input("name:" )
  
name:Jeff
  
Traceback (most recent call last):
  
File "<stdin>", line 1, in <module>
  
File "<string>", line 1, in <module>
  
NameError: name 'Jeff' is not defined
  
>>> input("name:" )
  
name:"Jeff"
  
'Jeff'
  
>>> raw_input("name:" )
  
name:Jeff
  
'Jeff'
  

  

  
幂运算符(**)函数pow():
  
>>> pow(2,3)
  
8
  

  
计算平方根math.sqrt():
  
>>> math.sqrt(9)
  
3.0
  

  
获取数的绝对值abs():
  
>>> abs(-10)
  
10
  

  
把浮点数四舍五入为最近的整数值round():
  
>>> round(1.0/2.0)
  
1.0
  

  
将给定的数值向下取整为某个特定的整数math.floor():
  
>>> math.floor(32.9)
  
32.0
  

  
将浮点数转换为整数int():
  
>>> int(32.9)
  
32
  

  
将整数转换为浮点数float():
  
>>> float(32)
  
32.0
  

  
long (长整数[也可以以八进制和十六进制表示])
  
>>> long(32)
  
32L
  

  
虚数(以及复数,即实数和虚数之和)是由另外一个叫做cmath模块来实现的:
  
>>> cmath.sqrt(-1)
  
1j
  

  

  
拼接字符‘+’:
  
>>> Name='Lily,'
  
>>> Age='23'
  
>>> print Name + Age
  
Lily,23
  

  

  
字符串表示str,repr:
  
str函数会把至转换成合理形式的字符串,以便用户理解;
  
repr会创建一个字符串,以合法的python表达式的形式来表示值
  
>>> print repr("hello world")
  
'hello world'
  
>>> print repr(1000L)
  
1000L
  
>>> print str("hello world")
  
hello world
  
>>> print str(10000L)
  
10000
  

  
原始字符串r:
  
>>> print r'/home/jeff/'
  
/home/jeff
  

  
Unicode字符:
  
python中的普通字符串在内部是以8位的ASCII的形式存储,而Unicode字符串则以16位Unicode字符,这样就能够表示更多的字符集了
  
>>> print u'hello world'
  
hello world
页: [1]
查看完整版本: python学习笔记(一)