- Home
- >
- Software Development
- >
- Data Types – InApps 2022
Data Types – InApps is an article under the topic Software Development Many of you are most interested in today !! Today, let’s InApps.net learn Data Types – InApps in today’s post !
Read more about Data Types – InApps at Wikipedia
You can find content about Data Types – InApps from the Wikipedia website
So far, your adventures with Python have been all about creating things. There was a goal, a tiny app you could use to learn the ins and outs of basic Python programming. That’s where the fun is. But sometimes you have to take a step back to move forward. When that time comes, know you’re going to have to take in some of the more boring aspects of programming. And that’s where we find ourselves now. This step is important because there are certain aspects of programming you must comprehend to get the most out of it and to truly understand what you’re doing.
One aspect of Python you must understand is data types.
What Are Data Types?
As you probably suspect, a data type is a type of data. But it goes a bit deeper than that. How deep? A data type isn’t just a category of data, but it also informs what operations can be performed on a particular set of data.
You’ve already learned how to use variables such as:
x = int(input(“Type a number to start the range:”)) |
The above statement sets the variable x as an integer from input taken from a user. In that case, the variable x will store an integer that is a particular data type. Because it is stored as a specific data type, Python will know what can be done with it. Without data types at play, you could set a variable, but Python wouldn’t know what to do with it. For example, if you didn’t have data types, you might set x = 2 and y = 10, but if you try to add those together with a function, Python wouldn’t know they were numbers so it wouldn’t be able to make the calculation.
That’s why data types are important.
Python has a few built-in data types. One bit of confusion is that you’ll find different lists for these types (so there seems to not be any sort of agreeance on what to either include in this list or to call the types). However, we’re going to go with the following list (because it’s the one most seem to agree upon):
- Numeric
- Sequence
- Set
- Dictionary
- Boolean
Some of those types include different “sub-types” (more on this in a bit). And so, what are the different data types? Let’s find out.
Numeric
This is probably the easiest to understand because we all know what numbers are. A numeric data type is, well, a number. In Python, numeric data types can be integer, floating number, or complex numbers.
Ready to return to elementary math?
An integer (represented as a class by int) is a positive or negative whole number, such as -1, 2, 3, 4, -10, 20, 3000, or 40,000. Integers do not have a decimal point and there’s no limit to how long an integer can be. It’s also important to understand that Python does not allow the comma to be used as a numerical delimiter. Instead, you use the underscore. To understand this, log into the Python console with the command:
python3
At the console, type:
x=int(867_5309)
Next, type:
print(x)
You should see:
8675309
You can leave out the int portion because Python will automatically detect the number is an integer (the same holds true with other data types).
A float class number does include a decimal point and can be either negative or positive and can even be represented by scientific notation. So a float number can be -1.5, 3.14159, 30.2, or 100.7. Unlike integer, float does have a maximum size, which is referred to as inf (which stands for infinity). Here’s an example. The scientific notation “e” stands for “10 to the power of.” So if we were to set a float value of 10e100 (which is 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000, 000,000,000,000,000,000,000,000,000 or 1e+100) with the command:
x=10e100
We could view that value with:
print(x)
Python would report:
1e+100
However, if we were to set x to 10e400, Python would report:
inf
Why? Because the value was beyond the maximum size it can store.
A complex number is a combination of a real number and an imaginary number, represented by the complex class. A complex number could be something like 10*2x. Set that variable with;
a=10+3j
If we then issue:
print(a)
We’ll see:
(10+3j)
Sequence
A sequence is simple because it’s an ordered sequence of characters. For example, you could set x with the following statement:
x=str("Hello, New Stack")
Type print(x)
and Python will present:
Hello, New Stack
The sequence data type includes strings (a string of characters), list (comma-separated values that can be altered), and tuple (comma-separated values that cannot be altered). Another difference between tuples and lists is that tuples use (), whereas lists use []. We’ve already discussed lists in Python, so make sure you read through that tutorial to better understand this concept.
Set
A set is an unordered collection of data that is iterable (capable of returning its members one at a time), mutable (value can be changed), and has no duplicate elements. Sets are comma-delimited and are housed in brackets. A set can be a mixture of numerical and string values. For instance, issue the command (at the Python console);
x={1,2,3,4,5, "New Stack"}
If you then issue:
print(x)
Python will print out:
1,2,3,4,5, 'New Stack'
One very hand feature of sets is that we can perform unions and intersections. For example, we’ll set x and y as such:
x = {"Hello"}y = {"New Stack"}
We’ll then perform a union (joining the two together) with:
print(x|y)
Python will print out:
{'Hello', 'New Stack'}
Dictionary
The dictionary data type is the most flexible type Python has to offer, as they can be used to store large amounts of data. This type is an unordered collection of data values that are stored in key-value pairs.
One very helpful aspect of this type is the ability to use keys and values. Here’s a simple example. Dictionaries are set up like this:
{key : value, key : key : value, key : value}
Let me explain by way of an example. At the Python console, type the following:
dict = { “name”: “Jack Wallen”, “client”: “InApps” “subject”: “Python” } |
With the above, we’ve created a dictionary with the key-value pairs:
- name: Jack Wallen
- client: InApps
- subject Python
Print out the dictionary with:
print(dict)
The output would be:
{'name': 'Jack Wallen', 'client': 'InApps', 'subject': 'Python'}
Now, what if we only wanted to view the client? For that, type:
print(dict["client"])
The output would be:
InApps
Boolean
A boolean is a simple true or false statement, which we’ve already discussed at length in Python for Beginners: And/or Operators. Here are a few simple examples of booleans:
print(10 > 5)
The above will print out True.
However, if we issue the command:
print(5 > 10)
Python will print out False.
Conclusion
And that’s your introduction to data types. This concept is important to understand as you continue your journey with Python. Knowing the fundamentals of a programming language is a sure-fire way to help ensure a successful education. And although you might never consciously think about data types, you will use them with every Python application you create.
Source: InApps.net
Let’s create the next big thing together!
Coming together is a beginning. Keeping together is progress. Working together is success.