python 学习笔记(超详细)(一)

python 学习笔记(一)

1.python内存管理方式

print(isinstance(3,int))
x=3
print(id(x))
y=3
x=x+6
print(id(y))
print(id(x))

True
1826254368
1826254368
1826254560

  • 在python中修改变量值的操作,并不是修改了变量的数值,而是修改了变量指向的内存地址。
  • 首先读取x的值,然后加6,并将结果存放于内存中,最后将x指向该内存空间。
  • python采用的是基于值的内存管理方式,如果为不同的变量赋值为相同值,则这个值在内存中只有一份,多个变量指向同一内存。
  • python具有自动管理内存的功能,对于没有任何变量指向的值,python将其自动删除。

2.数字

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)

  • python中数字类型的变量可以表示任意大的数值
  • 十六进制一0x开头,八进制以0o开头,二进制以0b开头
  • python中可以表示复数用j来表示虚部complex(a,b)函数可以形成复数
  • real查看实部 imag查看虚部 conjugate()返回共轭复数

3.字符串

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中字符串属于不可变序列,可以用单引号、双引号、三引号界定,并且三者可以相互嵌套
  • 空字符串可表示为’ ‘或’’ ‘‘或’’’ ‘’’
  • 字符串支持+ * 运算进行合并生成新字符串
  • python支持字符串按从右-1编号,也可以切片
    a[0:3]表示从下表为0到2 (3-1)
  • a[ start:end]
    start和end都是整数型数值,这个子序列从索引start开始直到索引end结束,但不包括end位置。
    在这里插入图片描述
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()方法中模板字符串的槽除了包括参数序号,还可以包括格式控制信息

在这里插入图片描述

4.运算符与表达式

在这里插入图片描述

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

  • x or y 只有x为假的才计算y
  • x and y 只有x为真才计算y
  • 大小比较可以连用 x>=y x<=y
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)

  • python中逗号 , 并不是运算符,而只是一个普通的分隔符

5.常用内置函数

 >>>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)

6.对象的删除(好习惯)

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

  • del命令可以显示删除并解除值之间的指向关系,如果变量指向的值还有别的变量指向,则不删除该值。
  • del可以删除列表中指定元素,也可以删除整个列表
  • del无法删除元组或字符串中的指定元素,只能删除整个字符串(两者属于不可变序列)

7.基本的输入输出

x=input("please input:")
print(type(x))
print(x,end=" ")
print(int(x)+1)

please input:6
<class ‘str’>
6 7

  • 在python中,使用内置函数input()来接收用户的键盘输入,括号内可以添加提示字符串
  • python3中输入的统一默认为字符串,如果需要改变变量需要用强制转换
  • python3中输出要加括号并且自动换行,如果不需要换行,则加上end=“ ”
fp=open('E:\\robocup\\python\\python.txt','a+')
print('hello,world!',file=fp)
fp.close()
  • python在指定文件中输出,用文件的方法,先打开文件(注意路径用\ \ 第一个是转义字符),然后输出,最后关闭文件

8.模块的导入与使用

import math
from math import sin as f

print(math.sin(3))
print(f(3))

0.1411200080598672
0.1411200080598672

  • python中有大量第三方库可用“pip install 。。。”进行有需要的安装
  • 在使用库函数时,需要导入,有两种方法:1. import 模块名【as 别名】
    使用这种方式导入后,需要在使用的对象前加上前缀 “模块名 . 对项名”的方式进行访问,也可以用“别名 . 对象名”的方式使用其中的对象
    2.from 模块名 import 对象名【as 别名】
    使用这种方式仅导入使用的对象,并且可以为这个对象起一个别名,这种方法可以减少查询次数,减少程序员的代码量,不需要使用模块名作为前缀
  • 比较极端的情况是一次导入模块中全部的对象
    from math import *
    不推荐使用这中方法,一旦多个模块中有同名的对象,会导致混乱

9.python之禅–The Zen of Python

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!


































猜你喜欢

转载自blog.csdn.net/qq_40181592/article/details/86770782