different behavior in Python when creating set from constructor or literal

python 3 string methods
python dictionary
python bytes
design a class that store the information of student and display the same in python
python built-in functions
python bytearray
python str
python 3 zip

I am trying to create a Set structure in Python by using code below:

a = set ([1,2,3])
print (a)
a ={1,2,3}
print (a)
a = {'FG'}
print (a)
a = set('FG')
print (a)


{1, 2, 3}
{1, 2, 3}
{'F', 'G'}

while the set creation with a list of numbers leads to the same result using the two options, I see that if the input type is a String the result that I expect is the second, i.e. create a set of characters, while the first result seems to ignore that a string is a sequence of characters, therefore I am confused.

The set() constructor takes an iterable as an argument and will create a set of each unique item in that iterable. In this case a string IS the iterable, so you are seeing a set composed of each character in the string.

To get a set strings, places the strings in a list or tuple.

set(['abc', 'efg'])

Is there a difference between using a dict literal and a dict constructor?, the first doesn't need to lookup dict which should make it a tiny bit faster the builtin, so you can switch the behaviour by defining a local called dict for So both {} and dict() produce dictionary but provide a bit different ways of the dict literal as d = {'one', '1'} which in modern python 2.7+ will create a set. Adds an element to the set. clear () Removes all the elements from the set. copy () Returns a copy of the set. difference () Returns a set containing the difference between two or more sets. difference_update () Removes the items in this set that are also included in another, specified set.

When you do a = {'FG'}, you define the set with one single element which is the string "FG".

Then, when you do a = set('FG'), you define a set using an iterable, but in that case the iterable is not a list but a string. The set function will find all unique elements in the specified iterable, so in that case it will find all unique characters in the given string.

If you wanted the two to output the same result, you would have to give the set function a list containing individual strings, for instance: a = set(["FG"]).

Built-in Types — Python 3.8.5 documentation, Objects of different types, except different numeric types, never compare equal. The behavior of the is and is not operators cannot be customized; also they Numbers are created by numeric literals or as the result of built-in functions and operators. The constructors int() , float() , and complex() can be used to produce� A constructor is a special kind of method that Python calls when it instantiates an object using the definitions found in your class. Python relies on the constructor to perform tasks such as initializing (assigning values to) any instance variables that the object will need when it starts.

When passing integers, you're passing it in the form of a list.


Whereas you're not passing the string in list,


so python interprets both of them differently.

If you pass the string as a list, the result would be similar to that of integer.

>>> a=set(['FG'])
>>> print(a)

set() takes iterables as input.

Classes - Object-Oriented Programming in Python, A function performs an action using some set of input parameters. When we create an object of that data type, we call it an instance of a class. There is a different method called __new__ which is more analogous to a constructor, but it is we can just create it when we call Person , just like we create the string literals for� Syntax of constructor declaration : def __init__(self): # body of the constructor. Types of constructors : default constructor :The default constructor is simple constructor which doesn’t accept any arguments.It’s definition has only one argument which is a reference to the instance being constructed.

The set constructor takes an iterable and adds each element of the iterable object to the set. So when you construct the set with the iterable list [1,2,3], you naturally end up with {1, 2, 3}.

A string is actually iterable:

for i in 'abc':



Iterating over a string returns each character of the string for each iteration. So when you construct a set with a string (which you can do because it is an iterable), you end up with a set where each character becomes a separate member of the set:

set('FG') -> {'F', 'G'}

But when you are creating a set by using a literal and not invoking a constructor, as in {'FG'}, you are just creating a set with a single member 'FG'.

Best Python Code Examples, The float constructor is another way to create float objects. No literal exists for creating a complex number with non-zero real and imaginary parts. Compared with other programming languages, Python's class mechanism The behavior of the is and is not operators cannot be customized; also they can� Varun March 11, 2018 Python : 6 Different ways to create Dictionaries 2019-10-22T09:55:54+05:30 dictionary, Python No Comment In this article, we will discuss different ways to create dictionary objects in python.

Python syntax and semantics, The syntax of the Python programming language is the set of rules that defines how a Python There are two types of sets: set and frozenset , the only difference being that set is The empty literal {} results in an empty dictionary rather than an empty set, which is instead created using the non-literal constructor: set() . This tutorial went through creating classes, instantiating objects, initializing attributes with the constructor method, and working with more than one object of the same class. Object-oriented programming is an important concept to understand because it makes code recycling more straightforward, as objects created for one program can be used

Python Bytes, Bytearray, Python Bytes, Bytearray: Learn Bytes literals, bytes() and bytearray() Bytes objects can be constructed the constructor, bytes(), and from literals; use The optional source parameter can be used to initialize the array in a few different ways: #created from a iterable of ints, string, bytes or buffer objects. x� The set() builtin creates a Python set from the given iterable. In this tutorial, we will learn about set() in detail with the help of examples.

JavaScript object-orientated and prototype-based programming , While being familiar with other object-orientated programming(OOP) The JavaScript String data type has its own set of properties and methods provided other constructor function, similar to how classes inherit their behavior from one another Create a new literal language var otherLiteralLanguage = {"name":" Python"� The Python list() constructor returns a list in Python. In this tutorial, we will learn to use list() in detail with the help of examples.