Python Tutorial : Sets

Sets are unordered, unique elements. The objects in the set must be unique ,duplicates can not be used in sets.

set_objects = {"boat", "sock", "phone","lamp"}
print(set_objects)

Check if certain objects are in set. If object is in the list system will print true and if object is not will print false

set_objects = {"boat", "sock", "phone","lamp"}

print("boat" in set_objects)
print("pen" in set_objects)

Strings can be pass in Set. It will iterate through the characters of the string as an object boat becomes “b”,”o”,”a”,”t”

set_object = "boat"

print(set_object)

Add objects to Set with add() or update()

add() is used to add one object

update() is used to add more than one object

# add() one object 
set_objects.add("bike")
print(set_objects)

# update() more than one object
set_objects.update(["kite", "ball", "hat"])
print(set_objects)

To remove an item in a set, use remove() or discard()

set_objects.remove("ball")
print(set_objects)


set_objects.discard("kite")
print(set_objects)

Get number of objects in set, use len()

print(len(set_objects))

loop through set objects

# loop through set
for x in set_objects:
  print(x)

Checkout more Python tutorials here!

Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the…

Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value…

Python Ethereum Block Chain Interaction with Web3

This tutorial will show how to interact with the Ethereum blockchain using Python. To start, the Python library Web3 will need to be installed. The Web3 library allows you to interact with a local or remote Ethereum node, using a HTTP or IPC connection. Using Web3 you will be able to create smart contracts on…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s