Data types in Python
Datatypes are the attributes or group of specific characters in computer science which tells the compiler or the interpreter about the type of data in which the user want a specific character to be used. Whenever a specific datatype is mentioned by the user, the compiler or the interpreter converts the data to the specific type to be read by the computer. It plays a very vital role in the conversation between the computer and the programmer and without having sufficient types of data it would be really confusing to program any code at all.
The built-in core datatypes provided in Python are:
- String
- Numbers
- List
- Tuple
- Dictionary
String
A string datatype literally holds the actual string data (which may be letters, numbers or special symbols) inside "" or inside '' ,i.e. inside double or single quotation marks.
For example: All the following strings are legal and acceptable in Python:
"Hello","Holla",'I am great.','My age is 16.',"!@#$%^&*"
For extra knowledge: All the string characters in python(3.x) are unicode characters.
Numbers
A number datatype as it sounds is used to store numeric values in python. Number datatypes can undergo mathematical operations like addition, subtraction, multiplication, division, etc. (whose article will be provided later in the blog)
Numbers are further classified in python into the following categories:
1. Integers- Integers(numeric or normal integers)
- Booleans(having only two values)
2. Floating-point numbers (or Floats)
3. Complex numbers
Integers:
Integers as general mathematical term mean the set of whole numbers along with the negative ones. They don't have any fractional parts. For example: 23,-45,0,etc.
- Integers: These are the normal integer representations and can be of any whole value, i.e. negative, positive or 0. In python(3.x), the integers can be of any length, only dependent on the memory available. For example: 23,-45,0,etc.
- Booleans: These types of integers accept only two values, i.e. True(1) or False(2). To see the representation of the boolean values in python, you may have to type bool(1) or bool(0) which would give an output of True or False respectively.
Floats:
The numbers which have a fractional or decimal part can be represented using a float. All the floating point numbers always consist of a decimal point. For example: 2.346757, 12.376, 67.332, 0.0463, etc. Another interesting fact about the floats is that these numbers have a precision of 15 digits.
Note: In python, any number, say 17, represents an integer type of number, but the same value with a decimal point, i.e. 17.0 represents a float type number.
Complex Numbers:
A complex number is as it sounds a complex type of number consisting of two components, viz. real and imaginary components. It can be represented in the form A + iB where A stands for the imaginary part whereas B stands for the imaginary part and i represents the Greek letter iota.
Good for knowledge: i = √-1
Lists
A list in python is generally a list of different values separated by commas enlisted with square brackets. The values may be string, numbers, lists,etc.
For example: [1,34,56,3]
["hello","world","name"]
["aeiou',1,2,3,[4,5],6]
Tuples
Tuple are those lists of values as elements which are immutable separated by commas and enlisted within (). The values may be string, numbers, tuples, etc.
For example: (1,34,65,87,98,543,56)
("holla","amigo","good")
("night","morning",23,45,43,(23,"hey","hi"))
Dictionary
Dictionary is that type of data in which there is a unordered list of keys and values
within {}.
For example: {"Name" : "Raj", "Age in years" : 16, "Fav. fruit" : "Mango"}
Thanks for reading!
©SGPython
very good
ReplyDeleteThank you buddy.
Delete