<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<channel>
<title>小夏的猪窝 - Python</title>
<link>https://blog.x-tools.top/category/python/</link>
<atom:link href="https://blog.x-tools.top/feed/category/python/" rel="self" type="application/rss+xml" />
<language>zh-CN</language>
<description>Python随笔</description>
<lastBuildDate>Sun, 05 Jun 2022 08:38:00 +0000</lastBuildDate>
<pubDate>Sun, 05 Jun 2022 08:38:00 +0000</pubDate>
<item>
<title>Python - 函数定义 参数形式</title>
<link>https://blog.x-tools.top/archives/27/</link>
<guid>https://blog.x-tools.top/archives/27/</guid>
<pubDate>Sun, 05 Jun 2022 08:38:00 +0000</pubDate>
<dc:creator>小夏</dc:creator>
<description><![CDATA[&lt;!-- [toc] --&gt;函数def 函数定义关键字形参中 不需要声明类型 方便许多相比其他编程语言，它拥有多种传参方式，非常方便定义def 函数名(形参):    &quot;&...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>&lt;!-- [toc] --&gt;</p><h1>函数</h1><ul><li>def 函数定义关键字</li><li>形参中 不需要声明类型 方便许多</li><li>相比其他编程语言，它拥有多种传参方式，非常方便</li></ul><h2>定义</h2><pre><code class="lang-python">def 函数名(形参):
    &quot;&quot;&quot;说明文档 可以用help（函数名）查询&quot;&quot;&quot;
    #函数体    
    #注意缩进
    
函数名(实参)</code></pre><h2>位置参数</h2><ul><li>形参、实参的位置要对应</li><li>一 一对应</li></ul><h2>关键字参数</h2><ul><li>指定参数赋值</li><li>在给实参时加入一个赋值语句，指定某个形参收到值</li><li>调用时，一定注意，先传入位置变量后，才能关键字参数，使用顺序不能反了，反了报错。</li></ul><pre><code class="lang-python">#如这个例子
def sum(x,y):
    print(x+y)
sum(y=89,x=123)</code></pre><h2>默认参数定义</h2><ul><li><p><code>def sum(a=123,b=345)</code>这样定义函数头</p><ul><li>在调用时，就可以不传参</li><li>因为函数默认a为123 ，b为345</li></ul></li></ul><h2>可变参数</h2><h3>位置可变参数</h3><ul><li>接受所有位置参数，之后返回元组</li><li>形参有多少参数，实参就有多少参数</li></ul><pre><code class="lang-python">def name(*args):
    print(args)
name('这是位置可变参数',1,2,3)

#返回 ('这是位置可变参数', 1, 2, 3)
#一个元组</code></pre><h3><strong>关键字字典可变参数</strong></h3><ul><li>接受所有关键字参数，之后返回字典</li><li>相比位置可变参数，多了一个<code>*</code>号</li><li>函数收到的是一个字典</li></ul><pre><code class="lang-python">def name(**args):
    print(args)
name(a='这是关键字可变参数',b=1,c=2,d=3)
#返回 {'a': '这是关键字可变参数', 'b': 1, 'c': 2, 'd': 3}
#是一个字典 键值对</code></pre><h2><strong>注意</strong></h2><ul><li>变量不可以在函数外调用</li><li><p>变量定义在函数外，则为全局变量，函数内可用</p><ul><li>如果必须更改 通常在函数中 声明变量为外部变量<code>global a</code></li><li>通常不建议在函数中更改外部变量</li></ul></li></ul><h2><strong>函数返回值</strong></h2><ul><li>返回值可以返回多个变量，用逗号分割即可。</li><li>返回多个参数，接受变量为一个时，变量收到的是一个元组形式的。</li><li>同上，可以用多参数接受</li></ul><pre><code class="lang-python">def name(a,b,c,d):
    return a,c

ret = name(a='这是关键字可变参数',b=1,c=2,d=3)
print(ret)
#返回的是 ('这是关键字可变参数', 2)

#字典拆包
a = {'a':1,'b':2,'c':3}
def name():
    return a

x,y,z = name()
print(x,y,z,sep='||')
print(a[x],a[y],a[z],sep='||')</code></pre><h2>函数递归</h2><ul><li>递归就是套娃</li><li>递归一定要有出口</li></ul><h3>递归加法例子</h3><pre><code class="lang-python">def sum(a):
    if a==1:
        return 1
    return a+sum(a-1) #相当于100+99+98+97 ······ +3+2+1

print(sum(100))#返回 5050</code></pre>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://blog.x-tools.top/archives/27/#comments</comments>
<wfw:commentRss>https://blog.x-tools.top/feed/category/python/</wfw:commentRss>
</item>
<item>
<title>Python - For、While循环 推导式 公共方法</title>
<link>https://blog.x-tools.top/archives/13/</link>
<guid>https://blog.x-tools.top/archives/13/</guid>
<pubDate>Sun, 22 May 2022 10:17:00 +0000</pubDate>
<dc:creator>小夏</dc:creator>
<description><![CDATA[&lt;!-- [toc] --&gt;For循环相当于foreach语句 可以遍历 字符串 列表 元组 语法语法#遍历 字符串 列表 元组 语法for i in 列表:    print(i)...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>&lt;!-- [toc] --&gt;</p><h1>For循环</h1><blockquote><p>相当于foreach语句 </p><p>可以遍历 字符串 列表 元组 语法</p></blockquote><h2><strong>语法</strong></h2><pre><code class="lang-python">#遍历 字符串 列表 元组 语法
for i in 列表:
    print(i)</code></pre><h2>跳出语句</h2><ul><li><code>Break</code> 结束循环</li><li><code>continue</code> 结束本次循环</li></ul><h2>for else</h2><p>如果用了Break 则不执行else的代码</p><p>用continue 就执行else的代码</p><pre><code class="lang-python">for i in 列表:
    #执行代码
else:
    #循环正常结束的代码</code></pre><h1>while循环</h1><ul><li><p>相比for循环的特点</p><ul><li>不断运行 直到条件不符合退出</li><li>while同样也有else continue break语句</li></ul></li></ul><h2>语句块</h2><pre><code class="lang-python">i = 1 
while(i&lt;=100):
    print(i)
    i += 1
else:
    print('循环结束啦')</code></pre><h1><strong>公共方法</strong></h1><ul><li><code>len()</code>计算元素长度 ，个数</li><li><code>del()</code>或<code>del</code>关键字 删除元素</li><li><code>max()</code>最大值</li><li><code>min()</code>最小值</li><li><code>range()</code> 遍历语句 <strong>range(开始下标，结束下标，步长)</strong></li><li><p><code>enumerate(列表，起始编号=0)</code></p><ul><li>遍历返回(编号，数据) 返回是元组的方式。</li></ul></li></ul><pre><code class="lang-python">    for i,j in enumerate(a,1):
        print(f'下标{i},数据{j}')</code></pre><h1>推导式</h1><p><strong><em>通常用来简化代码</em></strong></p><h2><strong>列表推导式</strong></h2><p><code>c = [i for i in range(11) if i%2 == 0]</code><br>列表中for循环的每一次的值，都会放到最前面的变量 i 中。</p><pre><code class="lang-python">#创建从0-10的列表 普通方法
a=[]
for i in range(11):
    a.append(i)#向尾部追加数据
    
#列表推导式 将i后面的过程放到i中
b = [i for i in range(11)]

#列表推导式 步长为2 结果为0 2 4 6 8 10
c = [i for i in range(0,11,2)]

#第二种方法 后面有if语句 余数为零（偶数） if为True时 放到第一个 i 中
c = [i for i in range(11) if i%2 == 0]
#  存到i    for循环</code></pre><h2>字典推导式</h2><p><strong><em>可以快速合并列表为字典</em></strong></p><p><code>maxvar = {x:y for x,y in a.items() if y&gt;200}</code><br>同列表推导式，for循环中的每一次值，都会放到前面 x：y 中。</p><pre><code class="lang-python">a1=[&quot;a&quot;,'b','c','d']
b1=[123,456,789,321]
#遍历短的那个
c = {a1[i]:b[i] for i in range(len(a1))}
c = {a1[i]:b[i] for i in range(4)}
#提取列表中目标数据
a = {'a1':123,'b1':234,'c1':345,'d1':456}
maxvar = {x:y for x,y in a.items() if y&gt;200}
#遍历结束后还是字典 键值对的形式出现</code></pre><h2><strong>集合推导式</strong></h2><p>同以上两个推导式<code>i for i in c</code><br>都是使用for循环遍历循环叠加带入。</p><pre><code class="lang-python">c = [2,3,4,4,5,5]
d = {i ** 3 for i in c} #求三次方
d = {i for i in c}</code></pre>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://blog.x-tools.top/archives/13/#comments</comments>
<wfw:commentRss>https://blog.x-tools.top/feed/category/python/</wfw:commentRss>
</item>
<item>
<title>Python - 字符串 列表 元组 字典 集合</title>
<link>https://blog.x-tools.top/archives/12/</link>
<guid>https://blog.x-tools.top/archives/12/</guid>
<pubDate>Fri, 20 May 2022 02:08:00 +0000</pubDate>
<dc:creator>小夏</dc:creator>
<description><![CDATA[&lt;!-- [toc] --&gt;字符串 ' '不可变类型强制类型转化方法 str(数字或变量)​                 int(字符串)字符串输出a ='我爱学Python'p...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>&lt;!-- [toc] --&gt;</p><h1><strong>字符串 ' '</strong></h1><blockquote><p>不可变类型</p><p>强制类型转化方法 str(数字或变量)</p><p>​                 int(字符串)</p></blockquote><h2><strong>字符串输出</strong></h2><pre><code class="lang-python">a ='我爱学Python'
print(f'''多行引号c支持f表达式吗？
这是第二行
这是a的值：{a}
下一行结束啦
exit''')
#f表达式 和c# $表达式一模一样

a = int(a)
＃强制类型转换</code></pre><p>==上面的程序说明多行输出时，支持f表达式，可以带入变量输出！ 非常的实用！==</p><h2><strong>切片</strong></h2><ul><li><strong>a='我爱学Python'</strong></li><li>a[0:9] <em>输出：我爱学Python</em></li><li>a[开始下标:结束下标+1]。</li><li>a[0:9:1] <em>输出：我爱学Python</em></li><li>a[0:9:2] <em>输出：我学yhn</em></li><li>a[开始下标:结束下标+1:步长]  <em>步长默认为1</em></li><li>a[::-1] <em>输出：nohtyP学爱我</em></li><li><strong>步长为 -1 时，倒叙输出字符串。</strong></li><li>a[-7:-2] <em>输出：学Pyth</em></li><li><strong>都为负数时，从右向左，从-1到-n。</strong></li></ul><h2><strong>字符串常用方法</strong></h2><ul><li>查找方法</li></ul><pre><code class="lang-python">#查找方法  find
a=&quot;我要查找以下字符串'我爱学Python'，嗨嗨嗨，第二个字符串'我爱学Python',嗨嗨嗨&quot;
#a.find(查找的串,查询起始位置,查询结束位置)
print(a.find(&quot;我爱学python&quot;))#返回 -1 ，因为没找到
print(a.find(&quot;我爱学Python&quot;))#返回 10 ，找到了返回的字符串中的起始下表位置
#反向查找  rfind
print(a.rfind(&quot;我爱学Python&quot;))#返回 32
#查询出现多少次字串  count
print(a.count('我爱学python'))#返回 0，没查到
print(a.count('我爱学Python'))#返回 2，出现了2次字符串</code></pre><ul><li>修改方法</li></ul><pre><code class="lang-python">#修改方法  replace
a=&quot;我要查找以下字符串'我爱学Python'，嗨嗨嗨，第二个字符串'我爱学Python'，嗨嗨嗨&quot;
#a.replace(旧串，新串，替换次数)
print(a.replace('我爱学Python','Python'))
#分割方法  split
print(a.split('，'))#返回一个列表
#合并方法 只能合并列表
#字符串.join(字符串列表)
a=['字符串1','字符串2','字符串3']
print('*'.join(a))#返回 字符串1*字符串2*字符串3
print(join(a))#报错！ 必须拥有一个合并字符串
print(''.join(a)) #可以用空字符代替</code></pre><ul><li><p>补充方法</p><ul><li><code>字符串.strip()</code>去除空格</li></ul></li></ul><h1><strong>列表[]</strong></h1><ul><li>小知识</li><li>他是可变类型</li><li>强制类型转化方法 <strong>list(序列)</strong></li></ul><h2>列表的定义</h2><pre><code class="lang-python">vs = [123,456,789]#一个列表 中存的是数字
vs=[]#空列表</code></pre><h2><strong>列表方法</strong></h2><h3><strong>查找</strong></h3><ol><li>index()方法 如果不存在则报错</li><li>count()方法</li></ol><pre><code class="lang-python">vs = [123,456,789]#一个列表 中存的是数字
#查找
vs.index(123)#返回0下标 如果不存在则报错
#查询出现次数
vs.count(123)#返回1
#len测量列表长度
len(vs)#返回3</code></pre><h3><strong>增加</strong></h3><ol><li>append() 只能增加一个数据 加在最后</li><li>extend() 只能增加一个列表 在最后</li><li>insert(pos,val) 指定位置追加数据</li></ol><pre><code class="lang-python">#增加列表
vs = [123,456,789]#一个列表 中存的是数字
#只能增加一个数据 加在最后
vs.append(123456)#结果为[123, 456, 789, 123456]
#只能增加一个列表 在最后
vs.extend([1,2,3,4,5])#vs的值为[123, 456, 789, 123456, 1, 2, 3, 4, 5]
#指定位置追加数据 
vs.insert(1,8)#vs的值[123, 8, 456, 789, 123456, 1, 2, 3, 4, 5]</code></pre><h3><strong>删除</strong></h3><ol><li>del(vs) 删除整个列表</li><li>del(vs[0]) 删除指定值</li><li>vs.pop(pos) 删除指定位置并且返回</li><li>vs.remove(123)  删除第一个匹配的数据</li><li>clear() 清空列表</li></ol><pre><code class="lang-python">#删除变量
vs = [123,456,789]#一个列表 中存的是数字
del(vs)#删除整个列表
del(vs[0])#vs 值为 [456, 789]

vs.pop()#删除指定位置的数据，并且返回 修改vs变量
#返回 789 vs值为 [456]

#删除第一个匹配的数据
vs.remove(123) #返回[456,789]

#清空列表
vs.clear()</code></pre><h3><strong>修改</strong></h3><ol><li>reverse() 列表倒叙排列</li><li>sort() 列表升序排列</li><li>sort(reverse=True) 列表降序排列</li></ol><pre><code class="lang-python">#修改数据
a=[1,2,3,4,5,6,7]

#指定下标修改
a[0]=999 #a值为 [999, 2, 3, 4, 5, 6, 7]
#将列表倒叙排序
a.reverse()#a值为 [7, 6, 5, 4, 3, 2, 1]

#排序sort
a=[1,6,7,8,9,0,2,5,4,3]
#默认升序
a.sort()#a 值 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a.sort(reverse=True)#降序 a 值 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]</code></pre><h2>列表的连接</h2><pre><code class="lang-py">lister = ['12','34','354']
strret = ''.join(lister)</code></pre><h2><strong>列表转元组</strong></h2><p>使用<code>tuple()</code>转换</p><pre><code class="lang-python">a=[1,2,3,4]
a=tuple(a)</code></pre><h1><strong>元组()</strong></h1><ul><li>不可变类型</li><li>不可以修改数据</li><li>只能读</li><li>强制类型转换用 <code>tuple(序列)</code></li></ul><h2><strong>单个数组的元组定义</strong></h2><pre><code class="lang-python">a = (1,)
#一定要有一个逗号，不然不是元组类型</code></pre><h2><strong>查询方法</strong></h2><ul><li>和列表相同</li><li>只有index() 和 count()方法</li><li>len()方法</li></ul><h2>元组转列表</h2><pre><code class="lang-python">a=(1,2,3,4)
a=list(a)</code></pre><h2><strong>循环 遍历 迭代</strong></h2><h2>For循环</h2><blockquote>相当于foreach()语句</blockquote><pre><code class="lang-python">a=[867,234,678,0,2,4,6]
for i in a:
    print(i)
#依次输出
#867
#234
#678
#0
#2         
#4
#6</code></pre><h1>条件判断语句</h1><p>python一定要注意缩进！</p><pre><code>    C语言是大括号控制，Python不一样的就是使用缩进。
</code></pre><pre><code class="lang-python">if 条件:
    语句1
    语句2
elif 条件:
    语句1
    语句2
else:
     语句1
     语句2
     
＃三目运算
a = b if a&gt;b else a
＃求两数小值
＃符合条件值 if 条件 else 不符合的值</code></pre><h1><strong>字典{Key:values}</strong></h1><ul><li>可变类型</li><li>字典总是成对出现的， 统称 键值对</li><li>字典的定义： <code>a = {}</code></li><li><p>字典的type类型是：dict</p><pre><code class="lang-python">字典={'huawei':33,'xiaomi':44,'ipad':22}#调用方式
字典['huawei'] #输出33</code></pre></li></ul><h2>字典的方法</h2><h3><strong>增加</strong></h3><pre><code class="lang-python">字典[键]=值
#如果存在 则修改  不存在 则添加</code></pre><h3>删除</h3><p>删除用del进行删除</p><pre><code class="lang-python">#删除指定值
a={'huawei':1,'xiaomi':2,'ipad':3,'sanxing':4}
del a['ipad']
#删除整个字典
del a
#字典的清空
a.clear()</code></pre><h3><strong>查询</strong></h3><ul><li>get()方法  查询</li><li>values()方法  返回字典中的值</li><li>items()方法  返回的是个元组</li><li>keys()方法  返回所有键 ‘dict_keys’</li></ul><pre><code class="lang-python">#查询 get
a={'huawei':123,'ipad':321,'xiaomi':546}
a.get('huawei',9999) #返回123
#如果不存在 就返回参数2中的任意值

#values()
#返回字典中的值，返回的是个列表 类型是 dict_values

#items()
#返回的是个元组</code></pre><h2><strong>字典的循环遍历</strong></h2><pre><code class="lang-python">a={'huawei':123,'ipad':321,'xiaomi':546}
#打印遍历所有字典中的键
for i in a.keys():
    print(i)
    
#打印遍历所有字典中的值
for i in a.values():
    print(i)
    
#打印遍历所有字典中的键值对 元组的形式 
for i in a.items():
    print(i)
    
#打印遍历所有字典中的键值对 键值对的调用形式
#就是临时变量多一个 一个是键 一个是值
for i,j in a.items():
    print(f'{i}={j}')</code></pre><h1><strong>集合{}</strong></h1><ul><li>可变类型</li><li>集合 字典 都是大括号</li><li>空集合 必须用<code>set()</code>的方法定义</li><li>类型为set</li><li>强制转换方法是<code>set()</code></li><li><p>特点</p><ul><li>没有重复，自动去重</li><li>顺序是随机的</li></ul></li></ul><h2>集合的方法</h2><h3>增</h3><ol><li>用<code>add()</code>方法增加新集合，加入数据存在则无视。</li><li>用<code>update()</code>方法追加数据，可以是列表,集合，加入数据存在则无视。</li></ol><pre><code class="lang-python">a={1,2,3,4}
a.add(5)#增加

a.update({1,2,3,4,5,6,7})
a.update([1,2,3,4,5,6,7])
a.update(&quot;abc&quot;)</code></pre><h3>删</h3><ol><li><code>remove()</code> #不存在报错</li><li><code>discard()</code> #不存在不报错</li><li><code>pop()</code> #随机删除集合里的数据</li></ol><pre><code class="lang-python">a={1,2,3,4}
a.remove(5) #会报错
a.discard(1) #不报错

a.pop()#随机删除一个数据 并且返回删除的数据
#可以这么用
b=a.pop() #删的数据存到b中</code></pre><h3>查</h3><ol><li>用关键字 in 查询数据在集合中</li><li>用关键字 not in 查询数据不在集合中</li></ol><pre><code class="lang-python">a={1,2,3,4}
print(1 in a)
print(5 in a)
print(1 not in a)
print(5 not in a)</code></pre>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://blog.x-tools.top/archives/12/#comments</comments>
<wfw:commentRss>https://blog.x-tools.top/feed/category/python/</wfw:commentRss>
</item>
</channel>
</rss>