首页 > 其他 > 详细

计算机之基础篇(三)

时间:2019-09-14 18:55:37      阅读:68      评论:0      收藏:0      [点我收藏+]

流程控制之for循环

for循环:

提供了一种手段,不依赖索引取值

格式:

for 变量名(会拿到容器类元素的每一个值,没有了就结束循环) in 容器类元素:

? print(变量名)

  1. for + break

     for i in range(50,101,3):  # 顾头不顾尾,3表示步长
         if i == 53:
             break  # 中断循环
      print(i)
  2. for + continue

     for i in range(50,101,3):  # 顾头不顾尾,2表示步长
            if i == 53:
             continue  # 跳出本次循环,不执行下面的代码
  3. for +else(仅作了解)

     for i in range(50,101,3):
         if i == 1000:
             break
         print(i)
     else:u
         print('如果没有被break终止我就打印')
    

数字类型内置方法

整型int:

  1. 作用:年龄/id

  2. 定义方式:x=10

  3. 使用方法: +,-,*,/,%,//,**,<,>,=,!=,==,>=,<=

  4. 有序or无序(有索引的就有序,无索引的就无序):没这一说

  5. 可变or不可变(值变id不变叫可变,值变id变叫不可变):整型不可变

    x = 10
    print(id(x))
    x += 1
    print(id(x))
    
    lt = [1, 2, 3]
    
    print(id(lt))
    lt.append(4)
    print(lt)
    print(id(lt))
    

浮点型float:

  1. 作用:薪资

  2. 定义方式:x = 10.1

  3. 使用方法: +,-,*,/,%,//,**,<,>,=,!=,==,>=,<=

  4. 有序or无序: 压根没有有序无序一说

  5. 可变or不可变:不可变

x = 10.1
print(id(x))
x +=1
print(id(x))

列表内置方法:

  1. 作用:存储多个值

  2. 定义方式:[]内用逗号隔开多个元素

  3. 使用方法

优先掌握

  1. 索引取值/索引修改:

    it = [1,2,3,4]
    
    print(it[1])
     # 2
    it[1] = 3
    print(it)
     # [1, 3, 3, 4]
  2. 切片:

    it = [1,2,3,4]
    
    print(it[:])
     # [1,2;,3,4]
    print(lt[1:4:2]) # 2表示步数
     # [3,4]
  3. for循环:

    it = [1,2,3,4]
    
    for i in it:
        print(i ** 2)
    
     # 1 4 9 16
  4. 成员运算:

    it = [1,2,3,4]
    
    print(1 in it)
     # True
    print(5 in it)
     # False
  5. len:

    it = [1,2,3,4]
    
    print(len(it))
     # 4
  6. append:

    it = [1,2,3,4]
    
    it.append(5)
    print(lt)
     # [1, 2, 3, 4, 5]
  7. del删除:

    it = [1,2,3,4]
    
    print(lt)
    del lt[0]
    print(lt)
    
     # [2, 3, 4]

需要掌握

  1. insert

    lt = [1,2,3,4,5]
    
    lt.insert(0.0) # 往前插入
    print(lt)
     # [0, 1, 2, 3, 4, 5]
  2. pop

    lt = [11,22,33,44,55]
    
    lt.pop(0) # 按索引值删除
    print(lt)
     # [22, 33, 44, 55]
  3. remove

    lt = [11,22,33,44,55]
    
    lt.remove(22) # 按照值删除
    print(li)
     # [11,33,44,55]
  4. count

    lt = [11,11,11,44,55]
    
    print(lt.count(11)) #统计个数
     # 3
  5. index

    lt = [11,11,11,44,55]
    
    print(index(11)) # 寻找值的索引
     # 0
  6. clear

    lt = [11,11,11,44,55]
    
    lt.clear
    print(lt) # 清空列表
     # []
  7. copy

    lt = [1, 2, 3, 4]
    
    lt1 = lt.copy() # 拷贝列表
    print(lt1)
     # lt1 = [1, 2, 3, 4]
  8. extend

    lt1 = [1, 2, 34]
    lt2 = [1, 1, 2, ]
    
    lt1.extend(lt2) # 扩展列表
    print(lt1)
     # [1, 2, 34, 1, 1, 2]
  9. reverse

    lt = [1, 2, 3, 4]
    
    lt.reverse() # 反转列表
    print(lt) 
     # [4, 3, 2, 1]
  10. sort

    lt = [2, 3, 1, 0, 4]
    
    lt.sort() # 排序
    print(lt)
     # [0, 1, 2, 3, 4]
    lt.sort(reverse=True) # 反转列表
    print(lt)
     # [4, 3, 2, 1, 0]

字符串内置方法

优先掌握

  1. 索引取值
  2. 切片取值
  3. 长度len
  4. 成员运算 in,not in
  5. 移除空白strip
  6. 切分split
  7. 循环 for...in ...:

1.索引取值

s ='hello world'

print(s[3]) # l
print(s[-1]) # d

2.索引切片

s ='hello world'

print(s[::-1] ) # dlrow olleh
print(s[4:5])   # o

3.len

s ='hello world'

print(len(s)) # 11个长度

4.成员运算符in not in

s ='hello world'

print('nice' in s)  # False
print('nice' not in s) # Ture

5.移除空白strip

s ='hello world'

print(s.strip('h')) # 两边开始移除
#ello world )

6.切分split

s ='hello world'

print(s.split('w'))  # 随意一个字符进行切割,在全部转换为列表形式
# ['hello ', 'orld'] 

7.循环

s ='hello world'

for i in s:
    print(i,end='') #一次打印出变量名中的字符

需要掌握

  1. lstrip&rstrip

  2. lower&uppre

  3. startswith&endswith

  4. rsplit

  5. join

  6. replace

  7. sdigit

1.lstrip()和rstrip() 切片

s ='Hello : world***'
d = 'hello teacher'

print(s.rstrip('*')) # 右边开始去
 # Hello : world
print(s.lstrip('He')) # 左边开始去
 # llo : world***

2.lower()和upper()

s ='Hello : world***'
d = 'hello teacher'

print(s.lower()) # 将大写字母小写
 # hello : world***
print(s.upper()) #将小写字母大写
 # HELLO : WORLD***

3.startswith()和endswith()

s ='Hello : world***'
d = 'hello teacher'

print(s.startswith('Hello')) # 以....开始
 # True
print(s.endswith('***')) # 以....结束
 # True

4.rsplit() 切割

s ='Hello : world***'
d = 'hello teacher'

print(s.rsplit(':')) # 切割不需要的字符在转换为列表
 # ['Hello ', ' world***']

5.join() 拼接

s ='Hello : world***'
d = 'hello teacher'

it = s.split(':') # 用变量名 接收s的值,并且切割出':',转换为列表
print(it) 
 # ['Hello ', ' world***']
print(''.join(it)) # 在将重新定义的变量名进行拼接
 # Hello  world***

6.replace() 取代,代替

s ='Hello : world***'
d = 'hello teacher'

print( s.replace('Hello','morning')) #取出需要代替的值,和输入代替的值
 # morning : world***

7.isdigit() #判断字符是否为数字

s ='Hello : world***'
d = 'hello teacher'

print(d.isdigit()) # False

age = input('age')
if age.isdigit(): 
    print('good')
else:
    print('bad')

其他操作

  1. find|rfind|index|rindex|count
  2. centenr|ljust|rjust|zfill
  3. expandtabs
  4. captalize|swapcase|title
  5. is系列

计算机之基础篇(三)

原文:https://www.cnblogs.com/hj59988326/p/11519812.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!