发布于2019-08-08 12:38 阅读(589) 评论(0) 点赞(4) 收藏(0)
学习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]就没有遵守这个规则,返回的是一个空。
(前包括,后不包括)
如果第二个数字大于字符串的长度,得到的返回结果就自动到最大长度位置终止。
作者:343ueru
链接:https://www.pythonheidong.com/blog/article/14308/f4374e3ab96f2f612e27/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!