This post was made public as part of the Data Science Blogathon
Introduction
language that supports the development and implementation of a wide range of applications. It is an easy-to-use language that is easy to learn and use.. Python is being used in many multiple fields to create applications like systems administration, GUI programs, database applications, scripts, etc.
In this post, we are going to study the basic data types of Python along with their built-in functions and their use.
Table of Contents-
- Numeric data type
- String instruments
- List
- DictionaryY
Numeric data types
There is 4 types of numeric data types in Python: –
1) Int – Stores integer values that can be positive or negative and do not contain any decimal points.
As an example, num1 = 10, num2 = 15
2) Float up – These are real floating point numbers that store the decimal values. These are whole and fractional parts.
As an example, fnum = 25,4, fnum1 = 67,8
3) Complex – These are complex numbers specified as real part and imaginary part. They are stored in the form of a + bj where a is the real part and j represents the imaginary part.
As an example, num3 = 2 + 3j, numcom = 5 – 7j
>>> num1=10 >>> print(num1) 10 >>> type(num1) <class 'int'> >>> fnum = 26.5 >>> print(fnum) 26.5 >>> type(fnum) <class 'float'> >>> num3 = 2 + 6j >>> print(num3) (2+6j) >>> type(num3) <class 'complex'>
String instruments
A string is a sequence of characters enclosed in single quotes, double or triple quotes. The character present in a string can be any digit, lyrics, special symbols or blanks. String in Python is an immutable data type that, una vez que se asigna un valor a la variableIn statistics and mathematics, a "variable" is a symbol that represents a value that can change or vary. There are different types of variables, and qualitative, that describe non-numerical characteristics, and quantitative, representing numerical quantities. Variables are fundamental in experiments and studies, since they allow the analysis of relationships and patterns between different elements, facilitating the understanding of complex phenomena...., can't be changed later.
As an example, str1 = “Apple12”
Str2 = ‘hello’
Str3 = “Hello there, welcome to Analytics Vidya”
Accessing values in a string: –
Each character in a string can be expressed with the help of a technique called indexing. In indexing, cada carácter tiene un valor de indexThe "Index" It is a fundamental tool in books and documents, which allows you to quickly locate the desired information. Generally, it is presented at the beginning of a work and organizes the contents in a hierarchical manner, including chapters and sections. Its correct preparation facilitates navigation and improves the understanding of the material, making it an essential resource for both students and professionals in various areas.... representado por un número entero positivo o negativo a partir de 0.
Syntax:- stringname[index]
P.ej –
Str1=”Python #accessing second character Str1[1] #accessing first four characters Str1[0:4] # it will print the value from index 0 to 3 str1="Python" >>> str1[1] 'and' >>> str1[0:4] 'Pyth'
Positive indexing | 0 | 1 | 2 | 3 | 4 | 5 |
Rope | PAG | Y | t | h | O | North |
Negative indexing | -6 | -5 | -4 | -3 | -2 | -1 |
Chain operations
1) Concatenation – Python allows us to join two different strings using the concatenation operator '+'.
Syntax:– str1 + str2
P.ej-
>>> str1="Analytic" >>> str2="Vidya" >>> str3=str1 +str2 >>> print(str3) AnalyticVidya
2) Repetitions – Python allows us to repeat a given string with the help of the '*' operator.
Syntax:– stringname * integervalue
P.ej-
>>> str2 = "python" >>> str2 * 3 'pythonpythonpython'
3) Membership – The Membership operator helps to check if a given character is present in the string or not with the help of two operators in and not in. The in and not in operator returns the Boolean value True or False.
Syntax: – character in / not in string name
P.ej:-
>>> str1="python" >>> "p" in str1 True >>> "f" in str1 False
Built-in function in strings –
1) Len () – It is used to calculate the length of the given string.
Syntax:- Len(stringname)
P.ej:-
>>> str1="Python" >>> len(str1) 6
2) isupper () – This function is used to check if all letters in a string are uppercase or not. If all characters are uppercase, devuelve True; otherwise, returns False.
Syntax:– stringname.isupper()
P.ej:-
>>>str1= " Hello Everyone" >>> str1.isupper() False >>> str2="HELLO" >>> str2.isupper() True
3) it's lower () – This function is used to check if all letters in a string are lowercase or not. If all characters are lowercase, devuelve True; otherwise, returns False.
Syntax::– stringname.islower()
p.ej:-
>>> s="hello" >>> s.islower() True >>> s1 ="hey Folks!" >>> s1.islower() False
4) superior() – Used to convert all characters in a string to uppercase.
Syntax:– stringname.upper()
P.ej-
>>>str1= " Hello Everyone" >>> str1.upper() ' HELLO EVERYONE'
5) lower() – Used to convert all characters in a string to lowercase.
Syntax:– stringname.lower ()
P.ej-
>>> str2="HELLO" >>> str2.lower() 'hello'
List
A list is a collection of items that belong to different data types like int, float, string, list, etc. All items in the list are enclosed in square brackets and separated by commas. The list is a mutable data type that makes it possible to change the items in the list once it has been created..
P.ej:-
Lista1 =[123,567,89] # list of numbers
Lista2 =[“hello”,”how”,”are”] #string list
List3 = [“hey”,1223,”hello”] # list of mixed data types.
Accessing items on the list: –
List items are accessed in the same way as the chain, In other words, with the help of positive and negative indexing. The division is also used to enter the items in the list in which more than one element is accessed at the same time.
Lista1 =[“apple”,123,”mango”,[2,3,4,]]
Positive index | 0 | 1 | 2 | 3 |
List | “Apple” | 123 | “mango” | [2,3,4] |
Negative index | -4 | -3 | -2 | -1 |
P.ej-
>>> list1=['apple',123,"mango",[2,3,4,]] >>> list1[0] 'apple' >>> list1[3] [2, 3, 4] >>> list1[1:3] [123, 'mango']
List operations
1) Concatenation – Two lists are concatenated with the help of the operator “+”.
Syntax:- list1 + list2
>>> list1=["apple","mango",123,345] >>> list2 = ["grapes"] >>> list1 + list2 ['apple', 'mango', 123, 345, 'grapes']
2) Repetitions – With the help of the list of operators' *’ can be repeated n number of times.
Syntax:- list1* number
>>> list1=["apple","mango",123,345] >>> list1*2 ['apple', 'mango', 123, 345, 'apple', 'mango', 123, 345]
3) Membership – ‘in’ y ‘not in’ They are two membership operators that are used to check if the given element is present in the list or not and return a Boolean value True or False.
Syntax:– element in/not in in list1
>>> list1=["apple","mango",123,345] >>> 'apple' in list1 True >>> 'apple' not in list1 False
Built-in functions in the list
1) Add () – This function is used to add a single item to the end of the list. The only element to be added can be of any data type as integer, ready, chain, etc.
Syntax :- listname.append(element)
>>> list1=["apple","mango","grapes",123,345] >>> list1.append("orange") >>> list1 ['apple', 'mango', 'grapes', 123, 345, 'orange'] >>> list1.append([12,34,55]) >>> list1 ['apple', 'mango', 'grapes', 123, 345, 'orange', [12, 34, 55]]
2) insert () – The function insertThe term "INSERT" refers to the action of aggregating data into a database or system. In the context of programming, is commonly used in SQL languages to insert new rows into a table. This process is essential to maintain the integrity and updating of the information. Proper use of the INSERT instruction contributes to efficiency and effectiveness in data management.... () it is also used to add items to this list, but with the help of insert we can add the element at a particular index in the list. If the index is not present, will add the item to the end of the list.
Syntax :- listname.insert (index,element)
>>> list2=["apple","mango","grapes",123,345] >>> list2.insert(0,234) >>> list2 [234, 'apple', 'mango', 'grapes', 123, 345] >>> list2.insert(6,"papapa") >>> list2 [234, 'apple', 'mango', 'grapes', 123, 345, 'papapa']
3) tell () – The count function is used to count the number of times an item appears in the list.
Syntax:– listname.count(element)
>>> list3=[12,45,12,67,78,90] >>> list3.count(12) 2 >>> list3.count(11) 0
4) reverse () – The inverse function is used to reverse the order of the items in the list.
Syntax:– listname.reverse()
>>> list3=[12,45,12,67,78,90] >>> list3.reverse() >>> list3 [90, 78, 67, 12, 45, 12]
5) order () – The sort function is used to organize the items in the list in ascending order. The list of functions must contain all items of the same data type that are integers or strings.
Syntax:– listname.sort()
list4 =["grapes","banana","apple","mango"] >>> list4.sort() >>> list4 ['apple', 'banana', 'grapes', 'mango'] >>>list5=[12,45,12,67,78,90] >>>list5.sort() [12,12,45,67,78,90]
Dictionary
Dictionary is a special data type that is a mapping between a set of keys and a set of values. The dictionary represents a key-value pair enclosed in square brackets and each element is separated by a comma. Key-value pairs are separated by colons. Dictionary items are out of order and the key is unique for each dictionary value. Dictionary items are mutable, In other words, your items can be changed once created.
Syntax:-
Dictionaryname={key:value}
P.ej:-
Dict1 = {“comp”: “computer”, “sci”: “science”}
Dict2 = {“123”: “computer”, 456: “math”}
Accessing dictionary items: –
Dictionary items are accessed with the help of keys. Each key serves as an index and assigns the value in the dictionary.
Syntax:- dictionaryname[key]
>>> dict1={"comp": "computer" , "sci" : "science"} >>> dict1["comp"] 'computer' >>> dict1["sci"] 'science'
New items are added to the dictionary with the help of a new key.
>>> dict1={"comp": "computer" , "sci" : "science"} >>> dict1["comm"]="commerce" >>> dict1 {'comp': 'computer', 'sci': 'science', 'comm': 'commerce'}
Built-in dictionary functions
1) items () – The items function () returns a list of tuples from the dictionary key-value pair.
Syntax:- dictionaryname.items()
>>> dict1={"comp": "computer" , "sci" : "science","comm":"commerce"} >>> dict1.items() dict_items([('comp', 'computer'), ('sci', 'science'), ('comm', 'commerce')])
2) keys () – This function returns the list of keys in the dictionary.
Syntax:- dictionaryname.keys()
>>> dict1={"comp": "computer" , "sci" : "science","comm":"commerce"} >>> dict1.keys() dict_keys(['comp', 'sci', 'comm'])
3) values () – Returns the list of values in the dictionary.
Syntax:- dictionaryname.values()
>>> dict1={"comp": "computer" , "sci" : "science","comm":"commerce"} >>> dict1.values() dict_values(['computer', 'science', 'commerce'])
4) len () – The length function is used to count the total number of items in the dictionary.
Syntax:- len(dictionaryname)
>>> dict1={"comp": "computer" , "sci" : "science","comm":"commerce"} >>> len(dict1) 3
5) to update () –used to append the dictionary key-value pair passed as an argument to the given dictionary key-value pair.
Syntax:- dictionary1.update(dictionary2)
>>> dict1={"comp": "computer" , "sci" : "science","comm":"commerce"} >>> dict2={"acc":"accounts","bst":"business studies"} >>> dict1.update(dict2) >>> dict1 {'comp': 'computer', 'sci': 'science', 'comm': 'commerce', 'acc': 'accounts', 'bst': 'business studies'}
Conclution
In the post, we can see that Python has many types of data that can contribute in the classification or categorization of data items. Data types represent the type of value that any variable can store and all the operations that can be performed on them.. At the same time, some of Python's built-in data types are mutable which can be changed later, while some of them are immutable and cannot be changed later. These types of data also have many built-in functions that make our tasks much easier and faster..
The media shown in this post is not the property of DataPeaker and is used at the author's discretion.