Python笔记一-基本概念

从官网下载了window x86版本的python-3.4.1.msi,直接运行安装,很容易。

在开始菜单启动python,有一个python(command line)菜单,点击运行即可进入python的工作界面,类似cmd,也可以选择IDLE(Python GUI)启动

1、

>>> "hello,python"
'hello,python'

>>> x="hello,"
>>> y="world"
>>> x+y
'hello,world'

写print “hello,world” 竟然没有编译通过,提示以下错误:

>>> print "hello,world"
SyntaxError: invalid syntax
>>> 

原来从python3.x版本,print是函数,需要添加(),因此改为以下即可编译执行

>>> print ("hello,world")
hello,world
>>> 

再比如

>>> print 2*2
SyntaxError: invalid syntax
>>> print (2*2)
4
>>> 

2、支持运算

>>> 2+2
4
>>> 1/2
0.5
>>> 1.0/2
0.5
>>> 2**3
8
>>> -3**2
-9
>>> (-3)**2
9

>>> pow(2,3)
8

>>> abs(-10)
10

>>> round(1.0/2.0)
0

3、支持大数

>>> 1987163987163981639186 * 198763981726391826 +23
394976626432005567613000143784791693659

4、支持16进制(0x)但不支持8进制的写法(010)

>>> 0xaf
175
>>> 010
SyntaxError: invalid token

5、支持变量

>>> x = 2
>>> x*3
6、获取用户输入

>>> input("the meaning of lie:")
the meaning of lie:42
'42'

>>> x= input("x: ")
x: 34
>>> y=input("y: ")
y: 42

7、模块

>>> import math
>>> math.floor(32.9)
32
>>> int(math.floor(32.9))
32

引入函数的方法

>>> sqrt(9)
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    sqrt(9)
NameError: name 'sqrt' is not defined
>>> from math import sqrt
>>> sqrt(9)
3.0

如果是想计算复数,则可能遇到

>>> sqrt(-1)
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    sqrt(-1)
ValueError: math domain error
>>> import cmath
>>> sqrt(-1)
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    sqrt(-1)
ValueError: math domain error
>>> cmath.sqrt(-1)
1j
>>> (1+3j)*(9+4j)
(-3+31j)

8、长字符串,可以用三个' 将 很长的字符串包含起来,比如 ''' hello,world‘, this is a long string ’‘’, 中间可以出现' 或者‘’

9、python3.0以上已经不再支持raw_input函数,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值