Data types in Python
This tutorial mainly teaches you about the data types in Python.In computer programming,a data type is an attribute of data which tells the compiler,how the programmer intends the use of data.Every value in python has a data type.There are various data types in Python.Some of them are
- Numbers
- String
- List
- Dictionary
- Tuple
- Set
Numbers
This data type stores numerical value.It further has 4 different data types.
1.Integer
- It stores integer type of value.
2.Float
- It stores decimal type of values.
3.Complex
- It stores complex values.
4.Boolean
- It stores boolean values i.e. true or false.
- We can also retrieve the type of the data types by using the type() function.
String
- String data type is used to store a sequence of characters or words.
- It can be declared in single quotes or double quotes.
- There are many predefined functions in python to retrieve the string data.Lets discuss them by using an example.
a=’python’
len(a) = 6 (retrieves the length of the string)
a[3] = h (retrieves the character at the specified index i.e. third here)
a[2:6] = thon (includes the starting index but excludes the ending index)
a.upper() = PYTHON (retrieves the value in uppercase letters)
a.lower() = python (retrieves the value in lowercase letters)
List
- Lists are used to store multiple items in a single variable.
- List is built-in data type in python used to store different types of data in a single variable.
- Lists are declared by using square brackets.
- The 3 main properties of lists in python are
- Ordered (always present in the order how we declared)
- Can be changed i.e. we can alter the values in the list
- .Duplicate entries are allowed (same values can be declared)
- Let us take an example
Mylist = [‘mobile’,’adapter’,’cable’] – declaring a list
print(Mylist) – retrieves the values in the list
append() – used to add the prescribed value to the end of the list
insert() – used to insert the respective value at the given location
reverse() – used to retrieve the values of the list in reverse order
Dictionary
- Dictionary in python is used to store the data in the form of key/value pair.
- We can retrieve the values in the dictionary when the key is known.
- Dictionary is declared by using curly braces.
- Properties of Dictionary in python are
- Unordered
- Can be changed
- No duplicate entries are present
- Let us take an example to know more about dictionary
Tuple
- Ordered
- Can not be changed
- Duplicate entries are present
- Tuple is declared by using parentheses
Set
- Set does not support index.
- Unordered
- No duplicate entries are present.
- Set is declared by using curly braces
- Let us take an example to know about set
Conclusion
In this module we discussed about the data types in python and their usage to retrieve the data.In the next module lets discuss about some other topic of python.For getting all the latest updates regarding computers and electronics please do visit our page http://roboticelectronics.in/