print(isinstance(3,int))
x=3
print(id(x))
y=3
x=x+6
print(id(y))
print(id(x))
True
1826254368
1826254368
1826254560
a=99999999999999
print(a*a)
print(math.sqrt(3**2+4**2))
a=3+4j
b=complex(5,6)
c=a+b
print(c)
print(c.real)
print(c.imag)
print(c.conjugate())
9999999999999800000000000001
5.0
(8+10j)
8.0
10.0
(8-10j)
a='abc'+'123'
b=3.6674
print(a)
print(a*2)
print('%7.3f'%b)
print('%d:%c'%(65,65))
print(a[-1])
print(a[0:3])
abc123
abc123abc123
3.667
65:A
3
abc
- 在python中字符串属于不可变序列,可以用单引号、双引号、三引号界定,并且三者可以相互嵌套
a[0:3]
表示从下表为0到2 (3-1)print("{}{}{}".format("圆周率是","3.1415926","..."))
print("{1}:{2}:{0}".format("123","abc","wd"))
print("{0:-^20.7}".format("123"))
圆周率是3.1415926…
abc:wd:123
--------123--------
- 字符串format()方法的基本使用格式是:
<模板字符串>.format(<逗号分隔的参数>)
- format()方法中模板字符串的槽除了包括参数序号,还可以包括格式控制信息
print("2/3=",2/3)
print("3//2=",3//2)
print("2**3=",2**3)
print("3.0//3.0=",3.0//2.0)
print(2>=3)
print(3>=2)
print(2!=2)
print(10 and 25)
print(0 or 20)
print(not 10)
2/3= 0.6666666666666666
3//2= 1
2**3= 8
3.0//3.0= 1.0
False
True
False
25
20
False
print('a' in 'b','a')
print('a' in ('b','a'))
x=3,5
print(x)
print(3==3,5)
x=3+5,7
print(x)
False a
True
(3, 5)
True 5
(8, 7)
>>>dir(__builtins__)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
- 执行这个命令可以查看所有内置函数和内置对象(两个下划线)
a=-2
b=[1,2,3,4,0]
c='a'
print( abs(a) ) #返回数字的绝对值
print( all(b) ) #对于可迭代对象所有元素 全部非零返回true 若存在零返回false
print( any(b) ) #对于可迭代对象存在元素非零,则返回true
print( bin(a) ) #把数字转换为二进制串
print( callable(a) ) #测试对象是否可调用,类和函数是可调用的
print( chr(65) ) #返回ASCII编码的字符
#print( dir(a) ) #返回指定对象的成员列表
print( ord(c) ) #返回一个字符的编码
print( str(b) ) #把对象转化为字符串
print( help(math.sin)) #查看指定方法的使用帮助
print( list(b) )# 把对象转换为 列表
print( set(b) ) #集合
print( tuple(b) )# 元组
#print( dict(b) ) #字典并返回
2
False
True
-0b10
False
A
97
[1, 2, 3, 4, 0]
Help on built-in function sin in module math:
sin(…)
sin(x)
Return the sine of x (measured in radians).
None
[1, 2, 3, 4, 0]
{0, 1, 2, 3, 4}
(1, 2, 3, 4, 0)
x=[1,2,3,4,5]
y=3
print(y)
del y
del x[1]
print(x)
print(y)
3
[1, 3, 4, 5]
Traceback (most recent call last):
File “E:/robocup/python/练习.py”, line 11, in
print(y)
NameError: name ‘y’ is not defined
x=input("please input:")
print(type(x))
print(x,end=" ")
print(int(x)+1)
please input:6
6 7
fp=open('E:\\robocup\\python\\python.txt','a+')
print('hello,world!',file=fp)
fp.close()
- python在指定文件中输出,用文件的方法,先打开文件(注意路径用\ \ 第一个是转义字符),然后输出,最后关闭文件
import math
from math import sin as f
print(math.sin(3))
print(f(3))
0.1411200080598672
0.1411200080598672
- 比较极端的情况是一次导入模块中全部的对象
from math import *
不推荐使用这中方法,一旦多个模块中有同名的对象,会导致混乱
import this
```python
>输入import this 即可得到一篇美文,多体会这段话,努力让自己的代码更优雅
>The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!