【python】入门学习(一)
2014-09-01 16:03
113 查看
主要记录一下与C语言不同的地方和特别需要注意的地方:
// 整除
** 乘方
整数没有长度限制,浮点数有长度限制
复数:
导入模块:
import
①import math #导入math中所有函数 使用时要用 math.sqrt()的形式
②from math import * #导入math中的所有函数, 可直接使用sqrt()
③from math import sqrt, tan #只导入sqrt和tan两个函数 推荐使用
字符串: ‘ ’, “ ”, “““ ”””
len(): 求字符串长度 返回的是整形 不像C有个‘\0’ 返回的是字符串本身的长度
+:字符串拼接
*:多次拼接
帮助:
dir():括号中是导入模块后的模块名,列出模块的所有函数
dir(__builtins__):查看Python内置函数清单
help():括号中是函数名,查看函数的文档字符串
print():打印函数文档字符串
如:print(math.tanh.__doc__)
print(bin.__doc__)
类型转换:
float(): 把整数和字符串转换为浮点数
str(): 把整数和浮点数转换为字符串
int():把浮点数和字符串转换为整数 舍弃小数部分 字符串必须长得像整数 “123.5”是不可以的
round(): 浮点数转整数 四舍六入五成双 不支持字符串
多重赋值:
交换变量的值
// 整除
** 乘方
整数没有长度限制,浮点数有长度限制
复数:
>>> 1j*1j (-1+0j)
导入模块:
import
①import math #导入math中所有函数 使用时要用 math.sqrt()的形式
②from math import * #导入math中的所有函数, 可直接使用sqrt()
③from math import sqrt, tan #只导入sqrt和tan两个函数 推荐使用
>>> import math >>> math.sqrt(5) 2.23606797749979 >>> math.sqrt(2)*math.tan(22) 0.012518132023611912 >>> from math import * >>> log(25+5) 3.4011973816621555 >>> sqrt(4)*sqrt(100) 20.0 >>> from math import sqrt, tan >>> tan(20)*sqrt(4) 4.474321888449484
字符串: ‘ ’, “ ”, “““ ”””
len(): 求字符串长度 返回的是整形 不像C有个‘\0’ 返回的是字符串本身的长度
+:字符串拼接
*:多次拼接
>>> len("""No No No""") 8 >>> "No"*10 'NoNoNoNoNoNoNoNoNoNo' >>> "No"+' Yeah!' 'No Yeah!' >>>
帮助:
dir():括号中是导入模块后的模块名,列出模块的所有函数
dir(__builtins__):查看Python内置函数清单
help():括号中是函数名,查看函数的文档字符串
print():打印函数文档字符串
如:print(math.tanh.__doc__)
print(bin.__doc__)
类型转换:
float(): 把整数和字符串转换为浮点数
str(): 把整数和浮点数转换为字符串
int():把浮点数和字符串转换为整数 舍弃小数部分 字符串必须长得像整数 “123.5”是不可以的
round(): 浮点数转整数 四舍六入五成双 不支持字符串
>>> str(3.1415) '3.1415' >>> float('3') 3.0 >>> int("123") 123 >>> int("123.5") Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> int("123.5") ValueError: invalid literal for int() with base 10: '123.5' >>> int(123.5) 123 >>> round("123"ArithmeticError) SyntaxError: invalid syntax >>>
多重赋值:
>>> x,y,z=1,"r",1.414 >>> x 1 >>> y 'r' >>> z 1.414
交换变量的值
>>> y,x=x,y >>> x 'r' >>> y 1
相关文章推荐
- IronPython入门学习(二种不同的语言比较)
- Python 学习(二)【快速入门】
- Debian Linux下的Python学习——入门
- python入门学习之一
- Python学习笔记 01 快速入门
- python学习入门
- python学习系列一:python快速入门
- [Python入门及进阶笔记00]-写在前面(目录/书籍/学习路线/其他)
- [Python入门及进阶笔记00]-写在前面(目录/书籍/学习路线/其他)
- 滴水穿石--python 学习笔记--入门 (2)
- 001--Python入门学习
- python与c++交互学习入门之5
- Python3.x快速入门学习
- Debian Linux下的Python学习——入门
- Python学习笔记 01 快速入门
- python与c++交互学习入门之5
- Django学习入门步骤 教程步骤 python
- python学习总结一(快速入门)
- python学习入门
- python入门指引--Python书籍、学习资料等推荐