create account

Curso Intermedio de Python N04 Conjuntos (Continuación) by rafaelaquino

View this thread on: hive.blogpeakd.comecency.com
· @rafaelaquino ·
$1.68
Curso Intermedio de Python N04 Conjuntos (Continuación)
Cordiales Saludos

<center>
![caractula04.png](https://files.peakd.com/file/peakd-hive/rafaelaquino/23uR9nP9MBWfzVtmpsHoGVUm4dmYjwcua8piDrU5roUTWYCL8ZL6SJA9oCQDHwGmFpaKv.png)
</center>

#### Operaciones con conjuntos

Para trabajar las operaciones con conjuntos utilizaremos los conjuntos
* conjunto_1 = {1, 2, 3, 4}
* conjunto_2 = {10, 3, 4, 20}

Comencemos declarando los dos conjuntos
~~~
>>> conjunto_1 = set(range(1,5))
>>> conjunto_1
{1, 2, 3, 4}
>>> conjunto_2 = set([4, 3, 10, 20])
>>> conjunto_2
{10, 3, 4, 20}

~~~

## Unión

La unión de dos conjunto nos dará un nuevo conjunto con los elementos de ambos conjuntos.

En el siguiente ejercicio el conjunto_u contendrá los elementos del conjunto_1 y los elementos del conjunto_2. En esta oportunidad utilizamos el método **.union**

~~~
>>> conjunto_1
{1, 2, 3, 4}
>>> conjunto_2
{10, 3, 4, 20}
~~~
~~~
>>> conjunto_u = conjunto_1.union(conjunto_2)
>>> conjunto_u
{1, 2, 3, 4, 10, 20}
~~~

Otra forma de resolver el ejercicio. (Usando el operador | )
~~~
>>> conjunto_u2 = conjunto_1 | conjunto_2
>>> conjunto_u2
{1, 2, 3, 4, 10, 20}
~~~



## Intersección

La intersección de dos conjunto nos dará un nuevo conjunto con los elementos comunes.

En el siguiente ejercicio el conjunto_i contendrá los elementos del conjunto_1 que estén en el conjunto_2. 

En esta oportunidad utilizamos el método .intersection

~~~
>>> conjunto_1
{1, 2, 3, 4}
>>> conjunto_2
{10, 3, 4, 20}
~~~
~~~
>>> conjunto_i = conjunto_1.intersection(conjunto_2)
>>> conjunto_i
{3, 4}
~~~

Otra forma de resolver el ejercicio. (Usando el operador & )

~~~
>>> conjunto_i2 = conjunto_1 & conjunto_2
>>> conjunto_i2
{3, 4}
~~~

## Diferencia

La diferencia de dos conjunto nos dará un nuevo conjunto con los elementos del primer conjunto que no pertenezcan al segundo conjunto.

En el siguiente ejercicio el conjunto_d contendrá los elementos del conjunto_1 que no pertenecen al conjunto_2. 

En esta oportunidad utilizamos el método .difference

~~~
>>> conjunto_1
{1, 2, 3, 4}
>>> conjunto_2
{10, 3, 4, 20}
~~~
~~~
>>> conjunto_d = conjunto_1.difference(conjunto_2)
>>> conjunto_d
{1, 2}
~~~

Otra forma de resolver el ejercicio. (Usando el operador - )
~~~
>>> conjunto_d2 = conjunto_1 - conjunto_2
>>> conjunto_d2
{1, 2}
~~~

## Diferencia Simétrica


La diferencia Simétrica de dos conjunto nos dará un nuevo conjunto con los elementos que no son comunes.

En el siguiente ejercicio el conjunto_d_s contendrá los elementos que no son comunes en el conjunto_1 y en el conjunto_2. 

En esta oportunidad utilizamos el método .symmetric_difference

~~~
>>> conjunto_1
{1, 2, 3, 4}
>>> conjunto_2
{10, 3, 4, 20}
~~~
~~~
>>> conjunto_d_s = conjunto_1.symmetric_difference(conjunto_2)
>>> conjunto_d_s
{1, 2, 10, 20}
~~~

Otra forma de resolver el ejercicio. (Usando el operador ^ )
~~~
>>> conjunto_d_s = conjunto_1 ^ conjunto_2
>>> conjunto_d_s
{1, 2, 10, 20}
~~~

## Determinar si un elemento pertenece a un conjunto

~~~
>>> conjunto_1
{1, 2, 3, 4}
>>> 5 in conjunto_1
False
>>> 1 in conjunto_1
True
>>> 
~~~

## Determinar el número de elementos de un conjunto

~~~
>>> conjunto_1
{1, 2, 3, 4}
>>> elementos = len(conjunto_1)
>>> elementos
4
>>> 
~~~
---

Hasta aquí la continuación de la publicación pasada: [Curso Intermedio de Python N03 Conjuntos](https://peakd.com/hive-154226/@rafaelaquino/curso-intermedio-de-python-n03-conjuntos)

### Profundizando en python

##### Usando el guión bajo _

~~~
>>> a = 10
>>> a
10
>>> _
10
>>> 
>>> b = 100
>>> _
10
~~~
~~~
>>> a = 10
>>> a
10
>>> b = 100
>>> b
100
>>> _
100
>>> 
~~~

~~~
>>> a,b,c,d,_ = 1,2,3,4,5
>>> _
5
~~~

~~~
>>> lista = [10,9,8,7]
>>> for _ in lista:
...     print(_)
... 
10
9
8
7
>>> 
~~~

~~~
>>> lista2
[1, 2, 3]
>>> for _ in lista2:
...     print('Son tres impresiones')
... 
Son tres impresiones
Son tres impresiones
Son tres impresiones
>>> 
~~~

~~~
>>> a,_,c = 1,2,3
>>> _
2
>>> _ * 3
6
>>> 
~~~

##### Recurso para aprender Python

[Curso Gratis de Programación](https://siraquino.github.io/pythoncumanes/)
[Curso de Programación Básica](https://siraquino.github.io/pythoncumanes/programacionbasica.html)

[Mi Twitter](https://twitter.com/Rafa_elaquino)
[Mi facebook](https://www.facebook.com/rafael.aquino.5815) 
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 151 others
properties (23)
authorrafaelaquino
permlinkcurso-intermedio-de-python-n04-conjuntos-continuacion
categoryhive-154226
json_metadata{"app":"peakd/2023.7.1","format":"markdown","tags":["developspanish","stem-espanol","stemsocial","linux","python","original-content","developer","spanish","tecnologia","programador"],"users":["rafaelaquino"],"image":["https://files.peakd.com/file/peakd-hive/rafaelaquino/23uR9nP9MBWfzVtmpsHoGVUm4dmYjwcua8piDrU5roUTWYCL8ZL6SJA9oCQDHwGmFpaKv.png"]}
created2023-07-24 22:10:27
last_update2023-07-24 22:10:27
depth0
children2
last_payout2023-07-31 22:10:27
cashout_time1969-12-31 23:59:59
total_payout_value0.858 HBD
curator_payout_value0.819 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,218
author_reputation112,619,069,555,129
root_title"Curso Intermedio de Python N04 Conjuntos (Continuación)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id125,611,185
net_rshares3,453,384,245,560
author_curate_reward""
vote details (215)
@stemsocial ·
re-rafaelaquino-curso-intermedio-de-python-n04-conjuntos-continuacion-20230727t152418092z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-rafaelaquino-curso-intermedio-de-python-n04-conjuntos-continuacion-20230727t152418092z
categoryhive-154226
json_metadata{"app":"STEMsocial"}
created2023-07-27 15:24:18
last_update2023-07-27 15:24:18
depth1
children0
last_payout2023-08-03 15:24:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,927,823,584,327
root_title"Curso Intermedio de Python N04 Conjuntos (Continuación)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id125,692,528
net_rshares0
@visualblock ·
<center>

![](https://images.ecency.com/DQmeUXCY8732hkoJpZcnSxQD7NbHUbhe9d8wKYmqN3rN56e/testigo_hispapro.png)

<sup>
Has sido curado por @visualblock / You've been curated by @visualblock 
Bienvenidas delegaciones / Delegations welcome
 [Encuentra nuestra comunidad aquí / Find our community here](https://discord.gg/tGuctbfYKN) 
[Trail de Curación / Curation Trail](https://hive.vote/dash.php?trail=visualblock&i=1)

[Vota por nuestro Testigo aliado - @hispapro](https://vote.hive.uno/@hispapro) / [Vote for our allied Witness - @hispapro](https://vote.hive.uno/@hispapro) 
[Más información sobre el testigo aquí](https://ecency.com/hive-111111/@hispapro/witness-announcement-descubre-el-nuevo) / [More information about the witness here](https://ecency.com/hive-111111/@hispapro/witness-announcement-descubre-el-nuevo)

</sup>

</center>
properties (22)
authorvisualblock
permlinkre-rafaelaquino-2023724t22311930z
categoryhive-154226
json_metadata{"tags":["developspanish","stem-espanol","stemsocial","linux","python","original-content","developer","spanish","tecnologia","programador"],"app":"ecency/3.0.35-vision","format":"markdown+html"}
created2023-07-25 03:31:18
last_update2023-07-25 03:31:18
depth1
children0
last_payout2023-08-01 03:31:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length839
author_reputation152,737,702,239,424
root_title"Curso Intermedio de Python N04 Conjuntos (Continuación)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id125,619,077
net_rshares0