<?xml version="1.0" encoding="gbk"?>
<rss version="2.0">
  <channel>
    <title>21番茄网 - Python基础语法</title>
    <link>http://www.21fanqie.com/forum-38-1.html</link>
    <description>Latest 20 threads of Python基础语法</description>
    <copyright>Copyright(C) 21番茄网</copyright>
    <generator>Discuz! Board by Comsenz Inc.</generator>
    <lastBuildDate>Fri, 30 Oct 2026 11:39:09 +0000</lastBuildDate>
    <ttl>60</ttl>
    <image>
      <url>http://www.21fanqie.com/static/image/common/logo_88_31.gif</url>
      <title>21番茄网</title>
      <link>http://www.21fanqie.com/</link>
    </image>
    <item>
      <title>def函数都称为方法吗</title>
      <link>http://www.21fanqie.com/thread-729-1-1.html</link>
      <description><![CDATA[不是的，def 定义的函数是否称为 “方法”，取决于它的位置和作用场景。

简单说：
在类里面用 def 定义的函数，才叫方法（Method）
在类外面用 def 定义的函数，就叫函数（Function）

举个例子：

# 类外面的def：叫函数
def 计算面积(长, 宽):
    return  ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Wed, 06 Aug 2025 01:25:20 +0000</pubDate>
    </item>
    <item>
      <title>类的下面是一定要有def函数吗</title>
      <link>http://www.21fanqie.com/thread-728-1-1.html</link>
      <description><![CDATA[类的下面是一定要有def函数吗??

类的下面不一定要有def函数，但大多数情况下我们会在类里定义方法（用def定义的函数）。
可以把类想象成一个 \&quot;容器\&quot;：这个容器里可以放工具（方法，即def函数），也可以放数据（变量），甚至可以什么都不放（空容器）。

1. 可以没 ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Wed, 06 Aug 2025 01:00:03 +0000</pubDate>
    </item>
    <item>
      <title>类名称的括号里可以有多少个参数，这些参数的位置是固定的吗，有没有什么要求或讲究?</title>
      <link>http://www.21fanqie.com/thread-727-1-1.html</link>
      <description><![CDATA[在 Python 中，类名称后面的括号 () 用于指定父类（也叫基类），这是实现继承的语法。比如 class 子类(父类): 表示子类继承了父类的功能。
1. 括号里可以有多少个参数？
括号里的参数数量没有固定限制，既可以是空（不继承任何类），也可以有多个父类（称为 “多继承 ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Wed, 06 Aug 2025 00:46:51 +0000</pubDate>
    </item>
    <item>
      <title>python中，class类和def函数有什么区别，用通俗易懂的类比方法来解释</title>
      <link>http://www.21fanqie.com/thread-726-1-1.html</link>
      <description><![CDATA[要理解 Python 中class（类）和def（函数）的区别，我们可以用日常生活中的 \&quot;工具\&quot; 和 \&quot;工厂\&quot; 来类比：
1. def 函数：像一个专门的工具
比如一把 \&quot;榨汁机\&quot;：

[*]有明确的用途（榨水果）
[*]输入原料（水果），输出结果（果汁）
[*]每次使用都是独立的，用完就结 ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Wed, 06 Aug 2025 00:20:46 +0000</pubDate>
    </item>
    <item>
      <title>Python的类中，有很多默认的特殊方法，常见特殊方法汇总</title>
      <link>http://www.21fanqie.com/thread-630-1-1.html</link>
      <description><![CDATA[Python 中除了 __init__ 方法是特殊方法外，还有其他一些特殊方法，以下是一些常见的特殊方法汇总：

1.__init__方法：用于实例化对象时进行初始化操作。
2.__del__ 方法：当一个对象被销毁时自动调用的方法。可以用来释放对象占用的资源，例如关闭文件、网络连接等 ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Thu, 27 Apr 2023 13:31:19 +0000</pubDate>
    </item>
    <item>
      <title>一段字符串中，截取指定字符串的之前或之后的字符</title>
      <link>http://www.21fanqie.com/thread-560-1-1.html</link>
      <description><![CDATA[一段字符串中，截取指定字符串的之前或之后的字符


link=\'http://www.21fanqie.com/thread-560-1-1.html&amp;ns3216546541\'
weizhi=link.rindex(\'&amp;ns\')   #获取字符串“&amp;ns”在字符串中的位置，返回数字。
    link2=link[:weizhi]     #对字符串切片，去掉指定字符串 ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Wed, 20 Apr 2022 07:37:52 +0000</pubDate>
    </item>
    <item>
      <title>python中构建按一个固定的数字递增数列</title>
      <link>http://www.21fanqie.com/thread-559-1-1.html</link>
      <description><![CDATA[python中构建按一个固定的数字递增数列


start=5     #定义起始数字
number_list=[]
while start]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Wed, 20 Apr 2022 07:32:25 +0000</pubDate>
    </item>
    <item>
      <title>python 替换字符串中的字符，比如替换文字</title>
      <link>http://www.21fanqie.com/thread-558-1-1.html</link>
      <description><![CDATA[python 替换字符串中的字符，比如替换文字


total_sale=\'总销量：10万+\'
if \'万\' in total_sale:
    total_sale=total_sale.replace(\'万\',\'0000\')
print(total_sale)]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Wed, 20 Apr 2022 03:44:41 +0000</pubDate>
    </item>
    <item>
      <title>函数如引用可变参数时，最好用深拷贝复制一个出来</title>
      <link>http://www.21fanqie.com/thread-519-1-1.html</link>
      <description><![CDATA[函数如引用可变参数时，最好用深拷贝b = copy.deepcopy(a)把参数复制一个出来，然后下面都用这个复制出来的参数操作，以免下次引用参数时，参数变化。


      深拷贝的方法详见：http://www.21fanqie.com/thread-518-1-1.html]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 15:13:20 +0000</pubDate>
    </item>
    <item>
      <title>Python 直接赋值、浅拷贝和深度拷贝解析</title>
      <link>http://www.21fanqie.com/thread-518-1-1.html</link>
      <description><![CDATA[Python 直接赋值、浅拷贝和深度拷贝解析

b = a: 赋值引用，a 和 b 都指向同一个对象。

b = a.copy(): 浅拷贝, a 和 b 是一个独立的对象，但他们的子对象还是指向统一对象（是引用）。


b = copy.deepcopy(a): 深度拷贝, a 和 b 完全拷贝了父对象及其子对象， ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 15:06:48 +0000</pubDate>
    </item>
    <item>
      <title>循环遍历时不能修改字典</title>
      <link>http://www.21fanqie.com/thread-517-1-1.html</link>
      <description><![CDATA[循环遍历时不能修改字典，否则会报错：RuntimeError: dictionary changed size during iteration

for k in func_dict.keys():
    if func_dict[k] is np.nan:
        del func_dict[k]
        continue

解决办法：将遍历条件改为列表

for k in lis ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 15:02:20 +0000</pubDate>
    </item>
    <item>
      <title>删除字典中的元素</title>
      <link>http://www.21fanqie.com/thread-516-1-1.html</link>
      <description><![CDATA[删除字典中的元素

1. Python字典的clear()方法（删除字典内所有元素）

dict = {\'name\': \'我的博客地址\', \'alexa\': 10000, \'url\': \'http://blog.csdn.net/uuihoo/\'}
dict.clear();  # 清空词典所有条目

2. Python字典的pop()方法（删除字典给定键 key 所 ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 14:57:26 +0000</pubDate>
    </item>
    <item>
      <title>两个字典相加的方法</title>
      <link>http://www.21fanqie.com/thread-515-1-1.html</link>
      <description><![CDATA[两个字典相加的方法

A = {\'a\': 1, \'b\': 2, \'c\': 3}
B = {\'b\': 4, \'c\': 6, \'d\': 8}
for key,value in B.items():
    if key in A:
        A[key] += value
    else:
        A[key] = value
dict(sorted(A.items(), key=lambda d:d[1]))

#结果：{\'a\': 1, ..]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 14:54:20 +0000</pubDate>
    </item>
    <item>
      <title>字典中，根据值的大小，进行排序的方法</title>
      <link>http://www.21fanqie.com/thread-514-1-1.html</link>
      <description><![CDATA[字典中，根据值的大小，进行排序的方法

一、sorted高阶函数
  这种方法更为简洁，更为推荐。

d={\'a\':1,\'c\':3,\'b\':2}    # 首先建一个字典d
#d.items()返回的是： dict_items([(\'a\', 1), (\'c\', 3), (\'b\', 2)])
d_order=sorted(d.items(),key=lambda x:x[1],reve ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 14:37:55 +0000</pubDate>
    </item>
    <item>
      <title>统计列表中各元素出现的次数</title>
      <link>http://www.21fanqie.com/thread-513-1-1.html</link>
      <description><![CDATA[1.利用Python字典统计
2.利用Python的collection包下Counter类统计
3.利用Python的pandas包下的value_counts类统计


一、字典统计

a = [1, 2, 3, 1, 1, 2]
dict = {}
for key in a:
    dict[key] = dict.get(key, 0) + 1
print(dict)

#结果：{1:3,2:2, ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 14:23:11 +0000</pubDate>
    </item>
    <item>
      <title>快速查看字典键值对数量的方法</title>
      <link>http://www.21fanqie.com/thread-512-1-1.html</link>
      <description><![CDATA[快速查看字典键值对数量的方法

dict={\'A\': 77, \'B\': 4, \'C\': 3, \'D\': 39}
print(len(list(dict.items())))

#结果：4

思路：将键值对转换成列表中嵌套的元组，然后用len函数统计元组的个数。
思路来源：https://blog.csdn.net/cxcxrs/article/details/8 ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 06:05:22 +0000</pubDate>
    </item>
    <item>
      <title>把一个列表顺序颠倒反转过来的方法</title>
      <link>http://www.21fanqie.com/thread-511-1-1.html</link>
      <description><![CDATA[把一个列表顺序颠倒反转过来的方法：

使用reversed 函数，reversed返回的结果是一个反转的迭代器，我们需要对其进行 list 转换
例如：

listNode = [1,2,3,4,5]

newList = list(reversed(listNode))
print(newList)

#结果：
#[5,4,3,2,1]



其 ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Sat, 17 Apr 2021 05:43:00 +0000</pubDate>
    </item>
    <item>
      <title>Python-替换或去除不能用于文件名的字符</title>
      <link>http://www.21fanqie.com/thread-506-1-1.html</link>
      <description><![CDATA[Python-替换或去除不能用于文件名的字符，主要用到了正则表达式。

import re

def validateTitle(title):
    rstr = r\&quot;[\\/\\\\\\:\\*\\?\\\&quot;\\\\|]\&quot;  # \'/ \\ : * ? \&quot; &lt; &gt; |\'
    new_title = re.sub(rstr, \&quot;_\&quot;, title)  # 替换为下划线
    print(new_title)
    retur ...]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Fri, 19 Mar 2021 05:19:25 +0000</pubDate>
    </item>
    <item>
      <title>利用json将字典和字符串互转</title>
      <link>http://www.21fanqie.com/thread-501-1-1.html</link>
      <description><![CDATA[利用json将字典和字符串互转

1.字典转换为字符串：json.dumps(dic)
**** 本内容被作者隐藏 ****]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Fri, 19 Mar 2021 00:43:34 +0000</pubDate>
    </item>
    <item>
      <title>把列表中的数字相加求和</title>
      <link>http://www.21fanqie.com/thread-482-1-1.html</link>
      <description><![CDATA[把列表中的数字相加求和

list1 = [91, 95, 97, 99, 92, 93, 96, 98]
count=0
for i in range(len(list1)):
    count=count+list1
print(count)]]></description>
      <category>Python基础语法</category>
      <author>21fanqie</author>
      <pubDate>Wed, 03 Mar 2021 02:09:44 +0000</pubDate>
    </item>
  </channel>
</rss>