首页 > Python基础教程 >
-
python基础教程之学习Python第一天,命令很多跟L
学习Python第二天,看了一天,有点头疼,准备先休息一会,再继续。有一点C语言和Java基础,学起来不是很费劲。学习热情尚好。
学习了dir,math模块,import加载模块,有跟Linux相似的地方。
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> help(math.pow)
Help on built-in function pow in module math:
pow(x, y, /)
Return x**y (x to the power of y).
模块的加载方式:
>>> from __future__ import division
>>> 5/2
2.5
>>> import math
>>> help(math)
Help on built-in module math:
NAME
math
Python 3 中字符串的连接,3舍弃了``,反向单引号,因为辨识度差。print后面需要加括号().
>>> a=("free")
>>> b=1988
>>> print a+'b'
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(a+'b')?
>>> print a
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(a)?
>>> print (a+b)
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
print (a+b)
TypeError: can only concatenate str (not "int") to str
>>> print (a+'b')
freeb
>>> print (a+`b`)
SyntaxError: invalid syntax
>>> print (a+str'b')
SyntaxError: invalid syntax
>>> print (a+str(b))
free1988
>>>
转义符
"\"
赋值时,“r"表示为原始字符串。字符串里面的内容没有含义
>>> d="c:\news"
>>> print (d)
c:
ews
>>> d=r"c:\news"
>>> print(d)
c:\news
>>> e="c:\\news"
>>> print e
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(e)?
>>> print (e)
c:\news
>>>
input函数,input()
>>> input("input your name:")
input your name:python
'python'
>>>
input结合print的小程序
print("Hello,World!")
name=input("What's your name?")
age=input("How old are you?")
print("Your name is " + name)
print("And you are "+age+" years old.")
ten=int(age)+10
print("After ten years,you will be "+ str(ten) +" years old. ")
运行结果。
D:\WPy64-3720\ZZ>python 0515-2.py
Hello,World!
What's your name?Zoe
How old are you?31
Your name is Zoe
And you are 31 years old.
After ten years,you will be 41 years old.
索引和切片
>>> lang=("study")
>>>
>>> lang[0]
's'
>>> lang.index("d")
3
>>> a=lang[2:4]
>>> a
'ud'
>>> b=[2:]
SyntaxError: invalid syntax
>>> b=lang[2:]
>>> b
'udy'
>>>
序列的切片,一定要左边的数字小于右边的数字,lang[-1:-3]就没有遵守这个规则,返回的是一个空。
(前包括,后不包括)
如果第二个数字大于字符串的长度,得到的返回结果就自动到最大长度位置终止。