codecamp

Python3 集合 add()方法

Python3 列表 Python 集合

描述

add() 方法用于给集合添加元素,如果添加的元素在集合中已存在,则不执行任何操作。

语法

add()方法语法:

set.add(elmnt)

参数

  • elmnt -- 必需,要添加的元素。

返回值

无。

实例

以下实例展示了 add() 方法的使用:

实例 1

fruits = {"apple", "banana", "cherry"}
fruits.add("orange") 
print(fruits)

输出结果为:

{'apple', 'banana', 'orange', 'cherry'}

已存在的元素,则不执行添加操作:

实例 2

fruits = {"apple", "banana", "cherry"}
fruits.add("apple")
print(fruits)

输出结果为:

{'apple', 'banana', 'cherry'}

Python3 列表 Python 集合


温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

相关教程

Python基础教程

python内置库

python3 math模块

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }