Linux Hint Posted June 30, 2023 Share Posted June 30, 2023 The data type is an important concept in programming as it defines the kind of value that a particular variable can hold and what operations can be executed on it. Python has numerous built-in data types, such as str/strings, int/integers, floating-point numbers, etc. The “type()” function can be utilized to check/determine the data type of any value in Python. This blog will present you with a complete tutorial on Python “data types” using appropriate examples. Data Types in Python Data types indicate what type of data can be stored inside variables in programming. In Python, different data types are used in Python shown below: Numeric Data Type String Data Type Tuple Data Type List Data Type Set Data Type Dictionary Data Type Let’s start discussing the discussed data types in Python. Numeric Data Type in Python Python uses numeric data types for numbers. It has three kinds of numbers: “int”, “float” and “complex”. The “Int” or “Integer” is for whole numbers. The “float” is for decimals with up to “15” digits. The “complex” is for numbers with real and imaginary parts. Example The below code is utilized to input different numeric data types in Python: num_value1 = 45 num_value2 = 42.0 num_value3 = 10+20j print(num_value1) print(type(num_value1),'\n') print(num_value2) print(type(num_value2),'\n') print(num_value3) print(type(num_value3)) In the above code: The “integer”, “float” and “complex” data types values “45”, “0” and “10+20j” are assigned to the variables named “num_value1”, “num_value2” and “num_value3”, respectively. The “type()” function takes the variable containing the value as an argument and retrieves the type of data that is assigned to the particular variable. Output The corresponding numeric data types int, float, and complex are shown in the above output. String Data Type in Python The “string” data type is utilized in Python to describe the character’s sequence. All of the string data is enclosed inside the quotation marks either single or double and triple for multiline line strings. Example Here is an example that shows the Python string data type: str1 = 'Python' print('String Using Single Quote: ',str1) print(type(str1)) str2 = "Python Guide for Beginners" print('\nString Using Double Quotes: ',str2) print(type(str2)) str3 = '''Python Step By Step Guide for Beginner''' print('\nMultiline String Using Triple Quotes: ',str3) print(type(str3)) In the above code lines, the string is created and assigned to the particular variable. Output The “single”, “double” and “triple” quotes are utilized to create a single line and multiline string. Tuple Data Type in Python Tuple is an “ordered” and “unchangeable” Python data type that is utilized to store the object’s collections. Tuples are written in round brackets “()” and can hold any type of data. Example Here is an example of a “tuple” data type: tuple_value = ('Joseph', 'Anna', 'Lily') print(tuple_value) print(type(tuple_value)) tuple_value1 = (45, 55, 65) print('\n',tuple_value1) print(type(tuple_value1)) tuple_value2 = ('Anna', 'Jon', 99) print('\n',tuple_value2) print(type(tuple_value2)) In the above code, the string elements are contained in the tuple “tuple_value”, the integer elements in the tuple “tuple_Value1” and both the string and integer elements comprise the tuple named “tuple_value2”. Also, their corresponding types are returned via the discussed “type()” function. Output The tuple values in each case have been displayed in the above snippet. List Data Type in Python The “List” in Python is the “mutable” or changeable, ordered sequence of items/elements enclosed in square brackets. Example Here is an example of the “list” data type: list_value = ["Joseph", 45, 22.25] list_of_string = ["Joseph", "Anna", "Lily"] list_of_int = [45, 98, 66] print(list_value) print(type(list_value)) print('\n',list_of_string) print(type(list_value)) print('\n', list_of_int) print(type(list_of_int)) In the above code, a list containing both integers and strings, a list of strings, and a list of integers is initialized in the code and their respective types are logged. Output Based on the above output, the lists and their types are displayed. Set Data Type in Python The “Set” data type is a non-ordered collection of particular items that contains any data type inside the curly braces “{}”. Example Here is an example of a “set” data type: set1 = {45, 55, 65, 78} print(set1) print(type(set1)) set2 = {'Anna', 'Lily', 'Mary', 'Cynda'} print('\n',set2) print(type(set2)) set3 = {45, 55, 'Lily', 'Jon'} print('\n',set3) print(type(set3)) Here, the set of integers, set of strings, and set containing both integers and strings are initialized and their corresponding types are returned accordingly. Output The sets have been created and displayed. Dictionary Data Type in Python In Python, the “Dictionary” data type is utilized to store/keep the collection of data in the form of a “key-value” pair. In the Dictionary, values are associated with their keys and are accessed using those specific keys. A dictionary uses “curly brackets {}” for its declaration and comprises keys and values separated by a “colon”. Example Here is an example of the “dictionary” data type: marks = {'Joseph': 45, 'Anna': 95, 'Henry': 100} print(marks) print(type(marks)) According to the above code snippet, the “Dictionary” named “marks” has been initialized and verified using the “type()” function. Output The dictionary has been created and displayed with its type. Conclusion In Python, “Data Type” defines what kind of specific data can be stored inside particular variables. Python supports multiple data types such as integer, float, string, dictionary, etc. that are used to take the numerical, sequence collection, or string values from the user, etc. This write-up delivered a thorough tutorial on Python data types including various types. View the full article Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.