python知识点(一)

变量

1、创建 第一次赋值时生成(与C语言不同),规则如下:
1)包含字母、数字、下划线;
2)只能以字母或下划线开头;
3)不能有空格;
4)避免与python关键字或函数名重复;
5)简短且有描述性,建议使用下划线分隔;
6)区别l、1、o、0;
2、类型
使用type(),函数可以查看变量类型,id()可以查看变量引用的地址;
变量无类型约束; 类型取决于关联对象,与变量无关;
3、垃圾回收
python自动释放未被引用对象,内部原理是依据对象引用计数器的数值。

import sys
sys.getrefcount()

4、共享引用
多个变量引用同一个对象
举例

a = 20
b = 20
a == b
a is b

上面代码段返回都是Ture。

a = 4321
b = 4321
a is b

上面代码返回False,因为python会预存一些对象,比如一些较小的int型对象(小于256).

对象类型

1、数字
int、float、decimal、fraction。。。。。。

1.1+2.2
Out[23]: 3.3000000000000003

存储精度问题
2、字符串 str
3、列表 list
4、字典表 dict
5、元组 tuple
6、文件 file
7、集合 set
8、布尔 Boolean:Ture、False
9、空 None
10、程序单元

数据类型

1、数值
1)int整型
无限范围,仅受限与内存和计算机配置
十六进制:0x12,hex();
八进制:0o7,oct();
二进制:0b001,bin();
2)float浮点型

f = 3.3333333
'f = {0}'.format(f)
Out[25]: 'f = 3.3333333'
'f = {0:.2f}'.format(f)
Out[26]: 'f = 3.33'

格式化输出浮点数

10 / 4
Out[27]: 2.5
10 // 4 # //表示取整
Out[28]: 2
10 // 4.0 # 结果取精度高的类型
Out[29]: 2.0
10 // -4.0 # 取整向左靠
Out[30]: -3.0
import math
math.floor(3.14)  #向左取整
Out[32]: 3
math.trunc(-3.9)  #向右取整
Out[33]: -3
round(-3.14)  #四舍五入取整
Out[34]: -3
round(-3.9)
Out[35]: -4

3)decimal
精度高

import decimal
decimal.Decimal('1.1')+decimal.Decimal('2.2')
Out[37]: Decimal('3.3')

4)random

import random
l = [1,2,3,4,5,6,7]
random.choice(l)
Out[6]: 1
random.sample(l,3)
Out[7]: [4, 2, 3]
random.shuffle(l) #打乱顺序
l
Out[9]: [6, 7, 5, 3, 4, 2, 1]
random.randint(1,10)
Out[10]: 9

2、字符串
1)忽略转义符r

url = r'c:\wer\asd\wq.txt'
Out[39]: 'c:\\wer\\asd\\wq.txt'

2)截取操作

s = 'qwerewasd'
s[0:4]
Out[41]: 'qwer'
s[-1]  #取最后一个字符
Out[42]: 'd'
s[:]  #取全部字符
Out[43]: 'qwerewasd'
s[::2]
Out[44]: 'qeead'
s[::-1]  #逆序
Out[45]: 'dsawerewq'

注意,字符串截取时,“:”前为下标,后为截取个数,再后面为步长。
3)字符串不能原位改变
4)字符串的一些内置方法

s = 'www.asdfg.com'
s.replace('com', 'cn')
Out[49]: 'www.asdfg.cn'

s = 'www.qwe.com,www.asd.com,www.zxc.com'
s.split(',')
Out[51]: ['www.qwe.com', 'www.asd.com', 'www.zxc.com']
s.startswith('www')
Out[52]: True
s.endswith('cn')
Out[53]: False
s.find(',')
Out[54]: 11

'{name}:{xxx}'.format(name='name', xxx='小明')
Out[56]: 'name:小明'

3、列表
任意对象的有序集合;
通过下标索引元素;
可变长度;
属于可变序列;
支持原位改变,可使用拷贝副本【[:]、.copy()】;
常用操作:.append()、 .extend()、.sort()、.reverse()、.index()、.count()

l = list('hahha')
l
Out[58]: ['h', 'a', 'h', 'h', 'a']
'k' in l
Out[59]: False
for c in l:
    print(c,end=' ')
 
h a h h a 

l = [1,2,3,4]
for c in l:
    s.append(c**2)
        
s
Out[68]: [1, 4, 9, 16]

l1 = [i**2 for i in l]
l1
Out[70]: [1, 4, 9, 16]
l.append(9)
l
Out[73]: [1, 2, 3, 4, 9]
l.extend([6,7,8])
l
Out[75]: [1, 2, 3, 4, 9, 6, 7, 8]
l.sort()
l
Out[77]: [1, 2, 3, 4, 6, 7, 8, 9]

['qwe']*3
Out[71]: ['qwe', 'qwe', 'qwe']

4、字典表dict
{键:值},支持嵌套
常用操作: .get() .update() .pop() .keys() .values() .items()

d = {'name':'stars', 'age':27, 'job':'pm'}
d.get('id',0000)
Out[79]: 0
d = dict(name='eason',age=22,job='student')
d
Out[81]: {'name': 'eason', 'age': 22, 'job': 'student'}

dat = {'tel':1380202}
d.update(dat)
d
Out[84]: {'name': 'eason', 'age': 22, 'job': 'student', 'tel': 1380202}
d.pop('job')
Out[86]: 'student'
d
Out[87]: {'name': 'eason', 'age': 22, 'tel': 1380202}
d.items()
Out[88]: dict_items([('name', 'eason'), ('age', 22), ('tel', 1380202)])
for a,s in d.items():
    print('{}:{}'.format(a,s))
    
name:eason
age:22
tel:1380202

5、元组tuple
任意对象有序集合
通过下标访问
属于“不可变”类型
常用操作:.index() .count()

from collections import namedtuple
Employee = namedtuple('Employee',['name','age','tel'])
tom = Employee('tom', age=25,tel=122
tom
Out[97]: Employee(name='tom', age=25, tel=122)

6、文件

file=open('文件名', mode)    #mode=w(写)、r(读)、b(二进制方式) 默认为读
file.write('xxxxx\n')
file.close()
file = open('文件名', encoding='utf8')

.read()、.readline()、readlines(),读操作会将文件内容读走

with open(‘123.txt’) as f 不需要close文件

语句与表达式

1、赋值语句

a=10
b=20
a,b=b,a

这样可以方便地交换变量

a,s,*d='12345'
d
Out[99]: ['3', '4', '5']
a
Out[100]: '1'
s
Out[101]: '2'

序列解包赋值
2、表达式
打印操作print
print(xx,xx,sep=,end=,file=open())
sep 分隔符;end 终止符;file 指定文件
3、if语句
4、循环语句

while True:
	...
else:
	...		

5、迭代
支持.next()的方法,可以使用next()函数
迭代器占内存小
迭代工具:for
推导
map
测试是否有迭代器:
iter(f) is f
使用iter()生成迭代器
6、函数
1)定义

def xxx():

注意函数缩进!!
2)变量作用域
built-in,内置变量
global x,使用全局变量x
enclousure,封装 nonlocal x
local x,局部变量x
3)参数传递
不可变类型,传递副本给函数 ,函数内操作,不影响原始值;
可变类型,传递引用地址给函数,函数内操作,可能影响原始值;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值