python笔记二 基础

10/24

对于Python,一切事物都是对象,对象基于类创建,对象所有的功能都是去类里面找的



变量名 = 对象 (值)                              重复的功能 创建一个类,然后对象去引用  




整数

age = 18

print(type(age))

<class ‘int‘>

如果是其他的类型,会在下面显示类地址。

age.__abs__()

all_item = 95

pager = 10

result = all_item.__divmod__(10)

print(result)

(9, 5)

5/6  5//6

age.__add__(7)   age.__rdivmod__(7)           abs 绝对值

浮点型float

as_integer_ratio



10/25

字符串

name = ‘eric‘

print(dir(name))  ## 这个str所有的成员

[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]

contains 包含   print(name.__contains__(‘er‘))   True

capitalize  首字母大写   casefold  小写首字母

center    居中

print(name.center(40,‘*‘))                 count(self, sub, start=None, end=None)

******************eric******************

count()   字数

name = ‘何全‘

print(name.encode(‘gbk‘))

b‘\xba\xce\xc8\xab‘

expantabs    #一个type 等于8个空格

name = ‘h\tlex‘

print(name.expandtabs())

find  和 index   find找不到返回-1, index找不到报错

name = "he {0} is {1}"

print(name.format(‘a‘,‘b‘))

he a is b

join  拼接

h = [‘h‘,‘e‘,‘q‘,‘u‘,‘a‘,‘n‘]

print(‘‘.join(h))

hequan

partition  分割

replace(‘a‘,‘b‘,1)  替换1个

split 指定字符,分割字符    rsplit  从右开始

splitlines()   换行符   =split(‘\n‘)

swapcase  大变小,小写变大写



10/26

list: append  clear  copy count  extend 合并  index  insert  pop(下标) remove(值) reverse反转  sort

元祖tuple

字典dict   : keys  values  items

for k,v in dic.items()

print(k,v)



10/31

set   无需不重复的元素集合

交集: update_set = old.intersection(new)

差集: delete_set  = old.symmetric_difference(update_set)

add_set = new.symmetric_difference(update_set)

s1 = set([11,22,33])

s2 = set([22,44])

ret0 = s1.intersection(s2)

ret1 = s1.difference(s2)

ret2 = s1.symmetric_difference(s2)

print(ret0)

print(ret1)

print(ret2)

{22}

{33, 11}

{33, 11, 44}

collections系列

import collections         计数器,多少次重复

obj = collections.Counter(‘sdafsdafsdafsdafsdaf‘)

print(obj)

Counter({‘f‘: 5, ‘d‘: 5, ‘a‘: 5, ‘s‘: 5})

orderedDict有序字典

dic = collections.OrderedDict()

dic[‘k1‘] = [‘v1‘]

dic[‘k2‘] = [‘v2‘]

print(dic)

OrderedDict([(‘k1‘, [‘v1‘]), (‘k2‘, [‘v2‘])])

defaultDict默认字典

dic = collections.defaultdict(list)

dic[‘k1‘].append(‘alex‘)

namedtuple 可命名元祖

mytuple = collections.namedtuple(‘mytuple‘,[‘x‘,‘y‘,‘z‘])

obj = mytuple(11,22,33)

print(obj.x)

11

队列  双向队列      单项队列

双进双出      单进单出

deque     queue

d = collections.deque()

import  queue

d = queue.Queue()

d.put(‘123‘)               d.get()

浅拷贝  深拷贝 赋值

copy.copy

copy.deepcopy

时间: 2024-08-26 14:20:54

python笔记二 基础的相关文章

玩蛇(Python)笔记之基础Part3

玩蛇(Python)笔记之基础Part1 一.集合 1.set 无序,不重复序列 {}创建,直接写元素 2.set功能 __init__()构造方法,,使用强制转换就会调用此方法 1 set1 = {'year', 'jiujiujiu'} 2 print(type(set1)) 3 # 创建集合 4 s = set() # 创建空集合 5 li = [11, 22, 11, 22] 6 s = set(li) set 3.集合的基本操作 1 # 操作集合 2 s1 = set() 3 s1.a

玩蛇(Python)笔记之基础Part2

玩蛇(Python)笔记之基础Part2 一.列表 1.列表 别的语言叫数组 python牛逼非要取个不一样的名字 1 age = 23 2 name = ["biubiubiu", "jiujiujiu", 22, age] 3 # namecopy = name 4 # namecopy.pop() 5 print(name) 6 # print(namecopy) List 2.列表取值 正常index 从零开始,,取倒数加负号 倒数第一就是[-1] 3.列表

Python笔记一 基础概念与基础认识

简介 python的创始人为吉多·范罗苏姆. 在10月份TIOBE排行榜上,python为第五,占有率为3.775%. python是一门动态解释型的强类型定义语言. 执行.py的时候是由python解释器,逐行编译→解释,在运行. PyCodeObject 是编译结果,运行完成后,保存到pyc中,以便下次直接运行pyc文件 动态类型 是指运行期间才去做数据类型检查的语言. 强类型定义语言 是指一旦一个变量被指定了某个数据类型,不经过强制转换,永远都是这个类型. 优点: 优雅.明确.简单. 开发

python笔记二(数据类型和变量、字符串和编码)

一.数据类型 python可以直接处理的数据类型有:整数.浮点数.字符串.布尔值.空值. 整数 浮点数 字符串:双引号内嵌套单引号,可以输出 i'm ok. 也可以用\来实现,\n 换行 \t table 多行字符串用  ''' ''' 布尔值: 可以对布尔值进行与或非运算/ 与and 或or 非not 空值 二.变量 a = 1/ a = "nadech"    与java不同,python在声明一个变量的时候不用指定变量的类型,直接赋值就可以了 在 执行a =1 的时候,pytho

inking Python 笔记(1) : 基础

1. type()可以查看变量的类型. >>> type(12.1) <class 'float'> >>> type('hello') <class 'str'> >>> type(2) <class 'int'> >>> type('2') <class 'str'> 2. Python的变量名支持数字.字母及下划线,但不能以数字开头. 3. 表达式及声明. >>>

【python笔记 二 】python语句

1.for语句: 脚本实例: #!/usr/bin/python for i in range(1,15): print i else: print 'The loop is done !' 2.while/if语句: 脚本实例: #!/usr/bin/env python #FileName:while1.py running = True a = 20 b = int(raw_input('Enter A number b: ')) while running: if a == b: pri

21天学通Python笔记(二)

五. >>> def hello(): print('123123') >>> hello() 123123 >>> def hello1(v): print(v) return v >>> a = hello1(123) 123 >>> a 123 >>> def hello2(v1, v2): print(v1) return v2 >>> b = hello2(1, 3) 1

python笔记二:常用数据类型操作

1.切片:常用于取list或tuple的部分元素的操作 1)l=[1,2,3,4,5,6] l[:3]表示取前3个值,l[1:5]表示1到5个值, L[-3:]从列表最后往前数即最后3个数.... 2)t=(1,2,3,4,5,6) t(:3),t(:),t(1:5)... 2.迭代 1)for ch in 'ABC' 2)for i, value in enumerate(['A', 'B', 'C']) 3)for x, y in [(1, 1), (2, 4), (3, 9)] 3.列表生

OpenGL超级宝典笔记二 - 基础变换

1.向量: 点乘:float m3dDotProduce3(u,v):返回两个单位向量的余弦值 叉乘:float m3dCrossProduct3(result,u,v):返回垂直于两个向量定义的平面的向量 2.矩阵: OpenGL使用的是列优先排序的矩阵 单位矩阵(对角线为1,其他为0):任何向量乘以一个单位矩阵都不会发生任何改变 3.变换:最终获得的变换矩阵会应用到每个顶点 视图矩阵x模型矩阵x投影矩阵->投影摄像机的位置变换,物体对象的位置变换,投影裁剪变换 若顶点向量为Vert,则变换公