Comptt

Comptt
  • Home
  • Features
  • _Multi DropDown
  • __Dropdown 1
  • __Dropdown 2
  • __Dropdown 3
  • _ShortCodes
  • _Sitemap
  • _Error Page
  • Documentation
  • _Web Doc
  • _Video Doc
  • Download This Template
February 07, 2023 python

 Sets in Python

What are the sets?

In Python, a set is a collection of unique elements. It is an unordered, mutable data structure that supports operations like union, intersection, difference, and membership testing.

A set can be defined using the built-in 'set' type. Sets are used to store multiple items in a single variable. 

Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.


Sets properties

Set can be enclosed with curly bracket '{ }' and values inside the set are separated with commas

For example:

 a = {1, 2, 3}
print(type(a))

//Output
<class 'set'>

Python sets are immutable

Sets in Python are immutable You can create a set of numbers and add elements to it. but you cannot change the elements once they have been added to the set.

 numbers = {1, 2, 3, 4} 
numbers[0] = 5
print(numbers)

//Output
TypeError: 'set' object does not support item assignment

Sets do not contains duplicate entries 

For example:
 a {10, 10, 10}
print(a) # only one 10 get stored

//Output
{10}

Set can store dissimilar values 

For example:

 a = set()               # empty set, use () instead of {}
b = {20} # set with one item
c = {'John', 3.5, 5} # set with multiple items
print(a)
print(b)
print(c)

//Output
set()
{20}
{'John', 3.5, 5}

It is possible to create a set of strings and tuples, but not a set of lists.

For examples:
 numbers = {12, 23, 45, 16, 52}
print(numbers)

//Output
{16, 52, 23, 12, 45}

Set slicing

Slicing of sets is not possible in Python because sets are unordered collections of unique elements and don't have indices

Set methods

Function Name Description
add() Adds a given element to a set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set that is the difference beetween two sets
difference_update() Updates the existing caller set with the difference between two sets
discard() Removes the element from the set
frozenset() return an immutable frozenset object
intersection() Updates the existing caller set with the intersection of sets
intersection_update() Updates the existing caller set with the intersection of sets
isdisjoint() Checks whether the sets are disjoint or not
issubset() Returns True if all elements of a set are present in another set
issubset() Returns
issuperset() Returns True if all elements of a set occupies another set
pop() Returns and removes a random element from the set
remove() Removes the element from the set
symmetric_difference()) Returns a set which is the symmetric difference between the two sets/td>
symmetric_difference_update() Updates the existing caller set with the Symmetric difference of sets
union() Returns a set that has the union of all sets
update() Adds elements to the set



Share This:
Facebook Twitter Pinterest Linkedin Whatsapp Whatsapp
python
at February 07, 2023
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Tags python

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Popular

Facebook

Subscribe Us

Tags

Android-Hacking Code HTML Programming Language python
Powered by Blogger.

Exclusive content in your inbox

Do you want to be creative? Subscribe to Our Newsletter!
* We promise that we don't spam !

* We promise that we don't spam !

Follow us By Email

Get new post updates by email
Powered by follow.it

FOLLOW US BY EMAIL

Get new post updates by email
Powered by follow.it

Report Abuse

Comptt

Search This Blog

  • March 20236
  • February 202315
  • January 202313

Social Plugin

  • facebook
  • whatsapp
  • instagram
  • youtube
  • twitter
  • linkedin
  • email
  • github
Educational Collaborator

Educational Collaborator

Zeal Education Society Narhe, Pune
Educational Collaborator

Educational Collaborator

JSPM Narhe Technical Campus, Narhe, Pune
Project Manager

Project Manager

Mr. Ayushman Sanjay Dalvi | Project Manager for Technicals
Special Collaborator

Special Collaborator

Cyber Security Mumbai | Special Collaborator for Cyber Security and Contents
  • Home
  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Courses
  • Join Us
  • Affiliate
  • Home-icon
  • Courses
  • Programming Lanuages
  • _HTML
  • _JavaScript
  • _Python
  • _Java
  • Programming Notes
  • Compilers
  • Ethical Hacking
  • About Site
  • _About US
  • _Privacy Policy
  • _Cookie Policy
  • _Terms & Conditions
  • _Contact US
  • Join Us
  • Certificate

Menu Footer Widget

  • Home
  • About
  • Privacy Policy
  • Terms & Conditions
  • Contact Us
  • Instagram
  • Join Us
  • Affiliate

Social Plugin

Our honorable CEO and Experts

Founder and CEO and All the Expert Team Members

Courses Provided by Us

You'll discover all of the most up-to-date bring innovative here.

Special Topics

  • Programming Language

Founder and CEO

Mr. Rohan Kumar Bhoi
Hello, this is Rohan Kumar Bhoi, CEO at CompTT. "Develop more skills to be self-dependent! Learn Rapidly, Be Unique, Live Stylish" facebook twitter youtube instagram linkedin github
    Pune, Maharashtra, India

    Latest posts

    10/recent/ticker-posts
    Project Manager

    Project Manager

    Mr. Aayan Sattar Mulla | Project Manager for Formating and Editing
    Honorable Founder and CEO

    Honorable Founder and CEO

    Mr. Rohan Kumar Bhoi

    "Learn new Skills with Us, and Be SELF DEPENDENT"

    With our easy-to-follow tips and tricks, you'll be self-sufficient in no time! Plus, we have solutions to all your computer-related pro…
    "Learn new Skills with Us, and Be SELF DEPENDENT"

    Courses

    • HTML
    Crafted with by Computer Tips-Triks || Distributed by Mr.Rohan Kumar Bhoi || © Copyright 2019-2023 Computer Tips-Triks All Right Reseved
    Design by - Free Blogger Templates | Distributed by Blogger Templates
    Comptt

    Made for WORLD by

    computertipstricks.tech and designed by Mr. Rohan Kumar Bhoi and Team. It is a one of the best Educational Hub for everyone looking for Computer Knowledge.
    • Home
    • Compilers
    • About Us
    • Privacy Policy
    • Courses
    • Join Us
    • Blog
    • Study Material
    • Home
    • About
    • Contact us
    • Privacy Policy
    • Cookie Policy
    • T & C

    Recent in Posts

    3/Ethical%20Hacking/post-list

    Facebook

    Telegram

    Telegram Group
    Comptt

    Popular Posts

    January 25, 2023

    Pages

    • Contact
    • About
    Created By Themes | Distributed By Gooyaabi Template