Set data type in python
Built-in Data TypesIn programming, data type is an important concept. Show Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:
Getting the Data TypeYou can get the data type of any object by using the ExamplePrint the data type of the variable x: x = 5 Try it Yourself » Setting the Data TypeIn Python, the data type is set when you assign a value to a variable:
Setting the Specific Data TypeIf you want to specify the data type, you can use the following constructor functions:
Test Yourself With ExercisesExercise:The following code example would print the data type of x, what data type would that be? Start the Exercise Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. Following are the standard or built-in data type of Python:
NumericIn Python, numeric data type represent the data which has numeric value. Numeric value can be integer, floating number or even complex numbers. These values are defined as
Note – Python3
Output: Type of a: Sequence TypeIn Python, sequence is the ordered collection of similar or different data types. Sequences allows to store multiple values in an organized and efficient fashion. There are several sequence types in Python –
1) StringIn Python, Strings are arrays of bytes representing Unicode characters. A string is a collection of one or more characters put in a single quote, double-quote or triple quote. In python there is no character data type, a character is a string of length one. It is represented by
Creating StringStrings in Python can be created using single quotes or double quotes or even triple quotes. Python3
Output: String with the use of Single Quotes: Welcome to the Geeks World String with the use of Double Quotes: I'm a Geek Accessing elements of StringIn Python, individual characters of a String can be accessed by using the method of Indexing. Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last character, -2 refers to the second last character and so on. Python3
Output: Initial String: GeeksForGeeks First character of String is: G Last character of String is: s Note – To know more about strings, refer Python String. 2) ListLists are just like the arrays, declared in other languages which is a ordered collection of data. It is very flexible as the items in a list do not need to be of the same type. Creating ListLists in Python can be created by just placing the sequence inside the square brackets Python3
Output: Initial blank List: [] List with the use of String: ['GeeksForGeeks'] List containing multiple values: Geeks Geeks Multi-Dimensional List: [['Geeks', 'For'], ['Geeks']] Accessing elements of ListIn order to access the list items refer to the index number. Use the index operator Python3
Output: Accessing element from the list Geeks Geeks Accessing element using negative indexing Geeks Geeks Note – To know more about Lists, refer Python List. 3) TupleJust like list,
tuple is also an ordered collection of Python objects. The only difference between tuple and list is that tuples are immutable i.e. tuples cannot be modified after it is created. It is represented by Creating TupleIn Python, tuples are created by placing a sequence of values separated by ‘comma’ with or without the use of parentheses for grouping of the data sequence. Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.). Note: Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple. Python3
Output: Initial empty Tuple: () Tuple with the use of String: ('Geeks', 'For') Tuple using List: (1, 2, 4, 5, 6) Tuple with the use of function: ('G', 'e', 'e', 'k', 's') Tuple with nested tuples: ((0, 1, 2, 3), ('python', 'geek')) Note – Creation of Python tuple without the use of parentheses is known as Tuple Packing. Accessing elements of TupleIn order to access the tuple items refer to
the index number. Use the index operator Python3
Output: First element of tuple 1 Last element of tuple 5 Third last element of tuple 3 Note – To know more about tuples, refer Python Tuples. BooleanData type with one of the two built-in values, Note – True and False with capital ‘T’ and ‘F’ are valid booleans otherwise python will throw an error. Python3
Output: Traceback (most recent call last): File "/home/7e8862763fb66153d70824099d4f5fb7.py", line 8, in print(type(true)) NameError: name 'true' is not defined SetIn Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. Creating SetsSets can be created by using the built-in Python3
Output: Initial blank Set: set() Set with the use of String: {'F', 'o', 'G', 's', 'r', 'k', 'e'} Set with the use of List: {'Geeks', 'For'} Set with the use of Mixed Values {1, 2, 4, 6, 'Geeks', 'For'} Accessing elements of SetsSet items cannot be accessed by referring to an index, since sets are unordered the items has no index. But you can loop through the set items using a for loop, or ask if a
specified value is present in a set, by using the Python3
Output: Initial set: {'Geeks', 'For'} Elements of set: Geeks For True Note – To know more about sets, refer Python Sets. DictionaryDictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds Creating DictionaryIn Python, a Dictionary can be created by placing a sequence of elements within curly Note – Dictionary keys are case sensitive, same name but different cases of Key will be treated distinctly. Python3
Output: Empty Dictionary: {} Dictionary with the use of Integer Keys: {1: 'Geeks', 2: 'For', 3: 'Geeks'} Dictionary with the use of Mixed Keys: {1: [1, 2, 3, 4], 'Name': 'Geeks'} Dictionary with the use of dict(): {1: 'Geeks', 2: 'For', 3: 'Geeks'} Dictionary with each item as a pair: {1: 'Geeks', 2: 'For'} Accessing elements of DictionaryIn order to access
the items of a dictionary refer to its key name. Key can be used inside square brackets. There is also a method called Python3
Output: Accessing a element using key: For Accessing a element using get: Geeks What is set () in Python?Python set() Function
The set() function creates a set object. The items in a set list are unordered, so it will appear in random order.
Can you set types in Python?In python there are basically two types of sets. The set and the frozenset. The set type is mutable, whether the frozenset is immutable. We can perform add(), remove() and these kind of operations on set, but it is not possible for frozenset.
How do you write a set in Python?Creating Python Sets
A set is created by placing all the items (elements) inside curly braces {} , separated by comma, or by using the built-in set() function. It can have any number of items and they may be of different types (integer, float, tuple, string etc.).
What are Python data types?Built-in Data Types in Python. Binary Types: memoryview, bytearray, bytes.. Boolean Type: bool.. Set Types: frozenset, set.. Mapping Type: dict.. Sequence Types: range, tuple, list.. Numeric Types: complex, float, int.. Text Type: str.. |