Python入门学习笔记_1

基础一

赋值

a = b = c = d = 10

e记法

a = 0.000000025
b = 2.5e-8
print(a, "\n")
print(b)

变量类型转换

a = "520"
b = int(a)
b
a = 5.99
b = int(a)
b
a = "520"
b = float(a)
b
b = str(5e+19)
b
a = "520"
type(a)
a = "打野"
isinstance(a, str)

运算符

+, -, *, /, %, **, //
#//地板除,取商
print(10 // 4)
print(3.0 // 2)
#幂运算
print(3 ** 3)
# 同时得到商和余数
print(divmod(9, 5))
2
1.0
27
(1, 4)

逻辑操作符

True and False
True or False
not True
3 < 4 < 5

条件分支

a = 0
if True:
    a = 1;
else:
    a = 2;
a

while循环

a = 0
while a < 8:
    a += 1

输入

temp = input("Enter a number:")
print(temp)

导入包

import keyword
print( keyword.kwlist )

断言(assert)

assert 3 < 4

for循环

str = "Daye"
for ch in str:
    print(ch, end = " ")
member = ["Tom", "Nancy", "Tom", "Bill"]
for each in member:
    print(each, len(each))
range(5)
list(range(5)) 
for each in range(2, 9):
    print(each)
for each in range(1, 10, 2):
    print(each)

基础二

字符串

  1. 分片([start : end : step])
str = 'Daye is a boy'
print(str[ : 13 : 2])
print(str[-3 :  : 1]) #倒数第三位到最后一位
Dy saby
boy
  1. split()分割
str2 = 'I am twenty-two years old'
print(str2.split())

str3 = 'apple,pear,banana,bean'
print(str3.split(','))
['I', 'am', 'twenty-two', 'years', 'old']
['apple', 'pear', 'banana', 'bean']
  1. join()合并
crypto_list = ['yellow', 'blue', 'black', 'white']
crypto_string = ','.join(crypto_list)
print(crypto_string)
yellow,blue,black,white
  1. startswith()、endswith()
poem = 'All that doth flow we cannot liquid name that'
print("startswith = ", poem.startswith('All'))
print("endswith = ", poem.endswith('name'))
startswith =  True
endswith =  False
  1. find()、rfind()
print(poem.find('that'))
print(poem.rfind('that'))#最后一次出现的位置
4
41
  1. count()
poem.count('that')
2
  1. isalnum()是否全为数字或者字母
print("pome ", poem.isalnum())
poem2 = 'abcd'
print("pome2 ", poem2.isalnum())
pome  False
pome2  True
  1. strip()将字符串尾部的规定字符删除
setup = 'a duck goes into a bar...'
setup.strip('.')
'a duck goes into a bar'
  1. 大小写操作
  • capitalize()[将字符串首字母大写]
setup.capitalize()
'A duck goes into a bar...'
  • title()[将所有单词的开头字母大写]
setup.title()
'A Duck Goes Into A Bar...'
  • upper()[将所有字母变成大写]
setup.upper()
'A DUCK GOES INTO A BAR...'
  • lower()[将所有字母变成小写]
setup.lower()
'a duck goes into a bar...'
  • swapcase()[将所有字母大小写转换]
setup.swapcase()
'A DUCK GOES INTO A BAR...'
  1. 格式排版函数
  • center()、ljust()、rjust()
print(setup.center(30))
print(setup.ljust(30))
print(setup.rjust(30))
  a duck goes into a bar...   
a duck goes into a bar...     
     a duck goes into a bar...
  1. replace()
setup.replace('duck', 'marmoset', 100)#100表示最多修改次数
'a marmoset goes into a bar...'

列表、元组、字典与集合

列表

  1. 普通列表
member = ["Tom", "Nancy", "Tony", "Bill"]
print(member)
number = [1, 2, 3, 4, 5]
print(number)
['Tom', 'Nancy', 'Tony', 'Bill']
[1, 2, 3, 4, 5]
  1. 混合列表
mex = [1, "Daye", 3.14, [1, 2, 3]]
print(mex)
  1. 空列表
empty = []
empty
向列表添加元素
  1. append()方法
member.append("Linda")
member
  1. extend()方法
member.extend(['Dear', 'Pmr'])
member
  1. insert()方法
member.insert(1, 'Zqy')
member
从列表中删除元素
  1. remove()方法
member.remove('Tom')
member
  1. del方法
del member[0] #如果不加下标,会整个列表删除
member
['Nancy', 'Bill', 'Linda', 'Dear', 'Pmr']
  1. pop()方法[有返回值]
member.pop()
'Pmr'
name = member.pop()
name
'Dear'
member.pop(1)
member
['Nancy', 'Linda']
列表分片(Slice)
print("member = ", member)
print("1:3 = ", member[1:3])
print(":3 = ", member[:3])
print(": = ", member[:])
print(": : 2= ", member[: : 2])
member =  ['Tom', 'Nancy', 'Tony', 'Bill']
1:3 =  ['Nancy', 'Tony']
:3 =  ['Tom', 'Nancy', 'Tony']
: =  ['Tom', 'Nancy', 'Tony', 'Bill']
: : 2=  ['Tom', 'Tony']
列表常用操作符
  1. 比较操作符
list1 = [123, 456]
list2 = [234, 123]
print("list1 > list2 = ", list1 > list2) #第一个决定大小,如果相等比较下一个,以此类推
list3 = [123, 456]
print("list1 < list2) and (list1 == list3) = ", (list1 < list2) and (list1 == list3))
list1 > list2 =  False
list1 < list2) and (list1 == list3) =  True
  1. 连接、重复操作符
#list4 = list1 + list2 建议不要使用,使用extend
list3 * 3
list3 *= 3
print(list3)
[123, 456, 123, 456, 123, 456]
  1. 成员关系操作符
123 in list3
"123" not in list3
True
list5 = [123, ["Daye", "Pmr"], 456]
print('Daye' in list5)
print('Daye' in list5[1])
print(list5[1][1])
False
True
Pmr
  1. 内置函数
print(dir(list))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
  • count()
print(list3.count(123))
81
  • index(member, startSite, endSite)
print(list3)
print(list3.index(123, 3, 10)) #3-10内,123出现的位置
[123, 456, 123, 456, 123, 456]
4
  • reverse()
list3.reverse()
print(list3)
[456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123]
  • sort()
list6 = [4, 5, 6, 1, 10, 2]
list6.sort()
print(list6)
list6.sort(reverse = True)
print(list6)
list6_1 = sorted(list6)
print(list6_1)
[1, 2, 4, 5, 6, 10]
[10, 6, 5, 4, 2, 1]
[1, 2, 4, 5, 6, 10]
  • 分片拷贝**(注意)**
list7 = list6
print("list7 = ", list7)
list8 = list6[:]
print("list8 = ", list8)
list9 = list6.copy()
print("list9 = ", list9)
#list6.sort()
list6.sort()
print("反向排序后:")
print("list7 = ", list7)
print("list8 = ", list8)
print("list9 = ", list9)
list7 =  [10, 6, 5, 4, 2, 1]
list8 =  [10, 6, 5, 4, 2, 1]
list9 =  [10, 6, 5, 4, 2, 1]
反向排序后:
list7 =  [1, 2, 4, 5, 6, 10]
list8 =  [10, 6, 5, 4, 2, 1]
list9 =  [10, 6, 5, 4, 2, 1]
  • join()
marxes = ['yellow', 'black', 'white']
print(','.join(marxes))
yellow,black,white

元组

创建元组
  1. 普通创建
empty_tutle = ()
print(empty_tutle)
one_marx = 'Groucho',#如果里面只有一个元素,需要在末尾加上','逗号
print(one_marx)
marx_tuple = ('yellow', 'black', 'white')
print(marx_tuple)
x, y, z = marx_tuple  #可直接将元组赋值给多个变量,变量数量 = 元组元素个数
print(x, " ", y, " ", z)
()
('Groucho',)
('yellow', 'black', 'white')
yellow   black   white
  1. 利用tuple()函数创建元组
marx_list = ['yellow', 'black', 'white'] #创建一个列表
tuple(marx_list)
('yellow', 'black', 'white')

字典

创建字典
  1. 普通创建
empty_dict = {}
print(empty_dict)
bierce = {
    "day" : "A period of twenty-four hours",
    "positive" : "Mistaken at the top of one's voice",
    "misfortune" : "The kind of fortune that never misses"
}
print(bierce)
{}
{'day': 'A period of twenty-four hours', 'positive': "Mistaken at the top of one's voice", 'misfortune': 'The kind of fortune that never misses'}
  1. 利用dict()函数转换为字典
lol = [['a', 'b'], ['c', 'd'], ['e', 'f']]  #列表+子列表
print("列表+子列表: ", dict(lol))
lot = [('a', 'b'), ('c', 'd'), ('e', 'f')]  #列表+元组
print("列表+元组: ", dict(lot))
tol = (['a', 'b'], ['c', 'd'], ['e', 'f'])  #元组+列表
print("元组+列表: ", dict(tol))
los = ['ab', 'cd', 'ef']  #列表+双字符
print("列表+双字符: ", dict(los))
tos = ('ab', 'cd', 'ef')  #元组+双字符
print("元组+双字符: ", dict(tos))
列表+子列表:  {'a': 'b', 'c': 'd', 'e': 'f'}
列表+元组:  {'a': 'b', 'c': 'd', 'e': 'f'}
元组+列表:  {'a': 'b', 'c': 'd', 'e': 'f'}
列表+双字符:  {'a': 'b', 'c': 'd', 'e': 'f'}
元组+双字符:  {'a': 'b', 'c': 'd', 'e': 'f'}
使用[key]添加或修改元素
some_pythons = {
    'A':'Tony',
    'B':'Nancy',
    'C':'Tom',
    'D':'Linda',
    'E':'Terry'
}
print(some_pythons)
some_pythons['E'] = 'Anny'
some_pythons['F'] = 'Lucy'
print(some_pythons)
{'A': 'Tony', 'B': 'Nancy', 'C': 'Tom', 'D': 'Linda', 'E': 'Terry'}
{'A': 'Tony', 'B': 'Nancy', 'C': 'Tom', 'D': 'Linda', 'E': 'Anny', 'F': 'Lucy'}
update()合并字典
other = {'G':'Vance', 'H':'Case'}
some_pythons.update(other)
print(some_pythons)
{'A': 'Tony', 'B': 'Nancy', 'C': 'Tom', 'D': 'Linda', 'E': 'Anny', 'F': 'Lucy', 'G': 'Vance', 'H': 'Case'}
del删除元素
del some_pythons['H']
some_pythons
{'A': 'Tony',
 'B': 'Nancy',
 'C': 'Tom',
 'D': 'Linda',
 'E': 'Anny',
 'F': 'Lucy',
 'G': 'Vance'}
clear()删除所有元素
other.clear()
other
{}
获取元素
print(some_pythons['A'])
print(some_pythons.get('B'))
Tony
Nancy
dict_keys(['A', 'B', 'C', 'D', 'E', 'F', 'G'])
获取键和值
print(some_pythons.keys())  #取得键
print(some_pythons.values())  #取得值
print(some_pythons.items())  #取得键和值
dict_keys(['A', 'B', 'C', 'D', 'E', 'F', 'G'])
dict_values(['Tony', 'Nancy', 'Tom', 'Linda', 'Anny', 'Lucy', 'Vance'])
dict_items([('A', 'Tony'), ('B', 'Nancy'), ('C', 'Tom'), ('D', 'Linda'), ('E', 'Anny'), ('F', 'Lucy'), ('G', 'Vance')])

集合

创建集合
empty_set = set()
print(empty_set)
even_number = {0, 1, 3, 5, 7}
print(even_number)
print(set('letters'))  #值不可重复,同样用列表、元组、字典都可以创建集合。需注意的是字典转为集合,只有键
set()
{0, 1, 3, 5, 7}
{'l', 'r', 's', 'e', 't'}
合并及运算符
a = {1, 2}
b = {2, 3}
  • 交集(共有元素)
print("a & b = ", a & b)
print("intersection(): ", a.intersection(b))
a & b =  {2}
intersection():  {2}
  • 并集(合并但不重复)
print("a | b = ", a | b)
a | b =  {1, 2, 3}
  • 差集(出现在第一个单不出现在第二个)
print("a - b", a - b)
a - b {1}
  • 异或集(仅在两个集合中出现一次)
print("a ^ b", a ^ b)
print("symmetric_difference(): ", a.symmetric_difference(b))
a ^ b {1, 3}
symmetric_difference():  {1, 3}
  • 判断是否为子集
print("a <= b", a <= b)
print("issubset(): ", a.issubset(b))
a <= b False
issubset():  False
  • 判断是否为真子集
print("a < b", a < b)
print("issuperset(): ", a.issuperset(b))
a < b False
issuperset():  False
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值