Day02

自学笔记


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

name = ‘Young’       -   对象

Li1 = [11,22,33]       -   对象

列表创建:

  Li = [11,22,33]

也可以这样创建:

  Li = list(11,22,3)

字符串:

  1. S = “fha”
  2. S = str(‘dd’)

以此类推......

2.  int 内部功能介绍

  __init__ () 构造方法

  比如:

  Age = int(19)   #执行__init__()方法

  Age = 18

  Age.__add__(7)

  18+7

  Age.__divmod__(7)

  18/7 = (商,余数)

  Age.__rdivmod__(7)

  7/18 = (商,余数)

3.  str内部功能介绍:

  __contains__()  --    1

 my_string = 'young'
result = my_string.__contains__('you')
#result = 'you' in my_string
print(result)

  结果:

  True

  capitalize      --     2

my_string = 'young'
result = my_string.capitalize()
print(result)

结果:

Young

  center           --     3

 name = 'Young'
result = name.center(40,'*')
print(result)

结果:

*****************Young******************

  count           --     4

 name = 'Youngyounggdgfak'
#result = name.count('ou')
result = name.count('ou',0,5)
print(result)

结果:

1

  encode         --     5

 name = '雨纷纷'
result = name.encode('gbk')
print(result)

结果:

b'\xd3\xea\xb7\xd7\xb7\xd7'

  endswith        --    6

name = 'Young'
#result = name.endswith('g')
result = name.endswith('un',0,4) # [0,4)
print(result)

结果:

True

  expandtabs      --    7

name = 'You\tng'
result = name.expandtabs()
print(result)

结果:

You     ng

  find                --     8

name = 'Young'
#result = name.find('g')#返回所在索引的位置,没有找到返回-1
result = name.find('un',0,4) # [0,4)
print(result)

结果:

2

  index            --     9

 name = 'Young'
#result = name.index('g')#返回所在索引的位置,没有找到报错
result = name.index('unc',0,4) # [0,4)
print(result) 

结果:

Traceback (most recent call last):

File "E:/Pycharm/01_projects/day02/01_examples.py", line 52, in <module>

result = name.index('unc',0,4) # [0,4)

ValueError: substring not found

  format          --    10

 name ="Young  {0}"
result = name.format("good")
print(result)

结果:

Young  good

 name ="Young  {0} as {1}"
result = name.format("good",'hello')
print(result)

结果:

Young  good as hello

 name ="Young  {name} as {id}"
result = name.format(name="good",id='hello')
print(result)

结果:

Young  good as hello

  类似与 %s ,强过‘+’

  join            --    11

 li = ['g','o','o','d']
#result = "".join(li)
result = "_".join(li)
print(result)

结果:

#good

g_o_o_d

  partition        --    12

 name ="Youngisgood"
result = name.partition('is')#以is将name分割
print(result)

结果:

('Young', 'is', 'good')

  replace         --    13

 name ="Youngisgood"
#result = name.replace('o','m')#用 ‘m’ 替换 'o'
result = name.replace('o','m',2)#用 ‘m’ 替换 'o',转换前两个
print(result)

结果:

Ymungisgmod

  splitlines        --    14

 name ='''
hello
kitty
good
man
'''
#result = name.split('\n')#指定字符分割
result = name.splitlines()#按行切割,根据‘\n’
print(result)

结果:

['', 'hello', 'kitty', 'good', 'man']

 4. 列表

列表的元素可以是列表

字典的元素也可以是字典

  extend  

 li = [1,2,3]
print(li)
#li.extend([4,5])
li.extend((4,5,))# 5后加上个逗号,大家默认的
print(li)

  结果:

  [1, 2, 3]

  [1, 2, 3, 4, 5]

   pop

 li = [1,2,3]
print(li)
ret = li.pop() # ret 等于被删除的那个数
print(li)
print(ret)

  结果:

  [1, 2, 3]

  [1, 2]

  3

 li = [1,2,3]
print(li)
ret = li.pop(1) #pop()里是下标
print(li)
print(ret)

  结果:

  [1, 2, 3]

  [1, 3]

  2

  remove 和 reverse

 li = [11,11,2,3,99]
print(li)
li.remove(11)# 第一个11 被拿走
print(li)
li.reverse()
print(li)

 5.   元组

  T1= (1,2,{‘k1’:’v1’})

  元组的元素不可以变,但元组的元素的元素可修改

 t1 = (1,2,{'k1':'v1'})
#del t1[0]
#t1[2] = 123 #t1[2] 字典元素
t1[2]['k1'] = 2
print(t1)

 6.   文件操作

读写方式              1

    r+   读写

    w+  写读

read()                        2

(注:read是按字符来执行)

 #coding=utf-8
__author__ = 'Young'
f =open('test.log','r')
#f.write('无hadksfh') # 先写,后读
ret = f.read(2)
f.close()
print(ret)

结果:

无华

tell()                             3

(注:tell按照字节来执行的,read是按字符来执行)

 f =open('test.log','r')
print(f.tell())
ret = f.read(2) #加参数指定读取字符
print(f.tell())
f.close()

结果:

0

4

seek 和 truncate          4

(注:tell用来查看当前指针位置  seek用来指定当前指针位置 ,truncate截取数据,只保留指针之前的数据)

 f =open('test.log','r+')
f.seek(4)
print(f.tell())
#print(f.read())
f.truncate() #截取数据,只保留指针之前的数据
print(f.tell())
f.close()

结果:

4

4

Python 从零学起(纯基础) 笔记 (二)的更多相关文章

  1. JavaScript基础笔记二

    一.函数返回值1.什么是函数返回值    函数的执行结果2. 可以没有return // 没有return或者return后面为空则会返回undefined3.一个函数应该只返回一种类型的值 二.可变 ...

  2. Python 从零学起(纯基础) 笔记 之 迭代器、生成器和修饰器

    Python的迭代器. 生成器和修饰器 1. 迭代器是访问集合元素的一种方式,从第一个到最后,只许前进不许后退. 优点:不要求事先准备好整个迭代过程中的所有元素,仅仅在迭代到某个元素时才计算该元素,而 ...

  3. Python 从零学起(纯基础) 笔记 之 深浅拷贝

    深浅拷贝 1. import  copy#浅拷贝copy.copy()#深拷贝copy.deepcopy()#赋值 = 2.   对于数字和字符串而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个 ...

  4. Python 从零学起(纯基础) 笔记(一)

    作者身份:初学Python,菜鸟 ================================================= 1. 主提示符和次提示符  >>> 主提示符   ...

  5. Python 从零学起(纯基础) 笔记 之 collection系列

    Collection系列  1.  计数器(Counter) Counter是对字典类型的补充,用于追踪值的出现次数   ps  具备字典所有功能 + 自己的功能 Counter import col ...

  6. Python基础笔记(二)

    1. List和Tuple List和Tuple是Python的内置的数据类型,区别在于可变和不可变,List用[]表示,Tuple用()表示,它们之间可以相互转换: # List to Tuple ...

  7. Python开发【第一篇】基础题目二

    1 列表题 l1 = [11, 22, 33] l2 = [22, 33, 44] # a. 获取l1 中有,l2中没有的元素 for i in l1: if i not in l2: # b. 获取 ...

  8. Shell脚本编程基础笔记二

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/8177697.html 一:输入 1:运行时参数 可以在启动脚本时,在其后输入参数. ./脚本 参数1 参数2. ...

  9. Vue学习计划基础笔记(二) - 模板语法,计算属性,侦听器

    模板语法.计算属性和侦听器 目标: 1.熟练使用vue的模板语法 2.理解计算属性与侦听器的用法以及应用场景 1. 模板语法 <div id="app"> <!- ...

随机推荐

  1. cocos 锚点、包围盒

    cocos中,setPosition就是设置一个sprite的锚点在父级元素的坐标 默认锚点是sprite矩形的中点 可以用getBoundingBox返回一个sprite所占矩形范围.范围用Rect ...

  2. offsetParent详解

    offsetParent与parentNode一样,都是获取父节点,但是offsetParent却有很大的不同之处: offsetParent找有定位的父节点,没有定位默认是body,ie7以下定位在 ...

  3. Python的高级特性4:函数式编程

    函数式编程的核心就是把函数当成对象来进行编程. 有两个常用到的方法:map/reduce,filter,其中map和filter是内建方法,而reduce不是,所以需要import相关模块. map接 ...

  4. 源码安装mysql

    1. 安装依赖组件 # yum install gcc gcc-c++ ncurses-devel perl -y   2. 安装cmake # wget http://www.cmake.org/f ...

  5. ES6 WeakSet数据结构 与Set十分相似

    它与Set十分相似,对象的值也不能是重复的,与Set不同点: .WeakSet成员只能够是对象. .作为WeakSet成员的对象都是弱引用,即垃圾回收机制不考虑WeakSet对该对象的引用,也就是说, ...

  6. linux不同角色server分区方案

    服务器角色 分区建议 优点    RAID方案 单机服务器 如8G内存,300G硬盘        /boot 100-200M swap 16G,内存大小8G*2 / 80G /var 20G(也可 ...

  7. MyEclipse对Struts2配置文件较检异常 Invalid result location value/parameter

    有时在编写struts.xml时会报错,但是找不出有什么她方有问题.也能正常运行 MyEclipse有地方去struts的xml进行了验证,经查找把这里 的build去掉就可以了

  8. 单片机C语言探究--为什么变量最好要赋初值

    有许多书上说,变量最好要赋初值.但是为什么要初值呢?不赋初值可能会出现什么样的意外呢?以下就我在以51单片机为MCU,Keil为编译器看到的实现现象作分析.众所周知,变量是存储在RAM中,掉电后即丢失 ...

  9. WPF循环加载图片导致内存溢出的解决办法

    程序场景:一系列的图片,从第一张到最后一张依次加载图片,形成“动画”. 生成BitmapImage的方法有多种: 1. var source=new BitmapImage(new Uri(" ...

  10. 深入理解计算机系统(2.2)---布尔代数以及C语言上的位运算

    布尔代数上的位运算 布尔代数是一个数学知识体系,它在0和1的二进制值上演化而来的. 我们不需要去彻底的了解这个知识体系,但是里面定义了几种二进制的运算,却是我们在平时的编程过程当中也会遇到的.这四种运 ...