培训第一天小笔记

时间:2022-07-23
本文章向大家介绍培训第一天小笔记,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Python版本: 2.6.6

知识点

  • Python List类型
  • Python 字符串小加密
  • 练习题

0x01 Python List类型

基本

列表是Python中最基本的数据结构,列表中每一个内容都会有一个索引的数字,第一位是从0开始计算,0,1,2这样以此类推 写法:

list=['one','two','three']

结果:

>>> list
['one', 'two', 'three']
>>> type(list)
<class 'list'>
>>> list[1]
'two'

0x02 更新内容

列表的内容是可以 增加的 写法:

list=['one','two','three']
list.append('four') #将four内容增加到列表list中

结果:

>>> list
['one', 'two', 'three', 'four']

有增加就有删除,有两种删除数据的方法

>>> list
['one', 'two', 'three']
>>> del list[2] #第一种,删除索引为2的数据
>>> list
['one', 'two']
---分割线---
>>> list
['one', 'two']
>>> list.remove("two") #第二种,指定内容删除
>>> list
['one']
>>>

0x03 扩展(Python列表截取)

>>> list=['one','two','three'] #列表内容
>>> list[2] #列表中第三个内容
'three'
>>> list[-2] #列表中倒数第二个内容
'two'
>>> list[1:] #从第二个开始截取列表
['two', 'three']
>>>

1x01 Python 字符串小加密

def enc(key,message): #定义一个类
    res="" #创建个空函数
    for i in range(len(message)): #这里做一个for循环,循环次数为message这个字符串的长度
        if message[i].isupper(): #判断该字符是不是大写
            num = ord(message[i])-ord('A') #返回对应的 ASCII 数值
             res += chr(ord('A')+(num+key)%26) #加密,返回当前整数对应的ascii字符
        elif message[i].islower():
            num = ord(message[i])-ord('a') #返回对应的 ASCII 数值
            res += chr(ord('a')+(num+key)%26) #加密,返回当前整数对应的ascii字符
    print res #将结果输出出来
e1=input("Plz input Key: ")
e2=input("Plz input Message: ")
enc(e1,e2)

.isupper()是判断该字符是否大写, .islower()则反之,判断是不是小写 ord()chr(),前者是将对应的ascii字符转换成数字,后者是数字转换成字符

2x01练习题

1.a=[1,2,3,4,5,6,7,8,9,10] 将a中的奇数和偶数分开放到不同的列表中

list=[] #创建空列表
list2=[] #创建空列表
for i in range(1,11): #循环1-10
    if i%2==0: #判断能不能取模 - 返回除法的余数
        list.append(i) #若可以,表示是偶数,append()到list中
    else:
        list2.append(i) 
print list #输出
print list2