python基础:
编译出现错误:
编译出现错误:
SyntaxError: Non-ASCII character '\xe6' in file F:/python/test.py on line 1
在文件前添加:
# -*- coding: utf-8 -*
>>> 10000000000000000000000000
10000000000000000000000000L
#长整形
>>> input("the mean of life")
the mean of life42
42
#括号中为提示输入,件简略地将就是将c++中cin和cout放在一起使用
>>> 2**3
8
#使用两个*作为乘方
>>> import math
>>> math.pow(2,3)
8.0
#也可以调用math模块中的内建函数pow(a,b)
>>> from math import sqrt
>>> sqrt(-1)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
sqrt(-1)
ValueError: math domain error
>>> import cmath
>>> cmath.sqrt(-1)
1j
#在不同包中的sqrt功能不一样。cmath包含对复数的运算
>>> "hello world"
'hello world'
#单引号和双引号其实是一样的
#在某些情况下会排上用场,例如:
>>> "let's go"
"let's go"
#或者调用反斜线
>>> 'let\'s go'
"let's go"
>>> x = 'hello'
>>> y = 'world'
>>> x+y
'helloworld'
#拼接字符串
#这里可以看到python打印字符串的时候还是有引号的,这是因为python打印值得时候,会保持改值在代码的中的状态。这里可以和print进行对比。
#python中存在两种值被装换成字符串的机制:
1.str:将值转换成合理形式的字符串
>>> print str("hello world")
hello world
2.repr:创建一个字符串,以合法的python表达式的形式表示值
>>> print repr("hello world")
'hello world'
#字符串和整数相加,当然并不能相加,这还是需要将整数转化为字符串的形式
>>> age = 18
>>> word = "my age is :"
>>> print word+age
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
print word+age
TypeError: cannot concatenate 'str' and 'int' objects
#当然并不能相加,这还是需要将整数转化为字符串的形式
>>> print word+repr(age)
my age is :18
>>> input("what is you name ")
what is you name fp
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
input("what is you name ")
File "<string>", line 1, in <module>
NameError: name 'fp' is not defined
#input会假设用户的输入的是合法的python表达式
>>> raw_input("what is your name")
what is your namefp
'fp'
#raw_input 会把所有的额输入当作原始的数据,然后将其放入字符串中
#长字符串
>>> print '''this
is
very
long'''
this
is
very
long
#原始字符串
>>> print r"let's go"
let's go
#原始字符串的最后一个字符不能为反斜杠,否则python就不清楚是否结束字符串。