Gain insight into the distinction between numerical data types and strings in Python from Noble Desktop's instructor, Art. Learn how these different data types operate, how to use them appropriately, and how to tackle issues that arise when trying to mix different data types.
Key Insights
- Python primarily utilizes two numerical data types: integers and floats. Integers are whole numbers, while floats are numbers with decimal points.
- The choice between using an integer or a float depends on the purpose and context. For instance, when dealing with people count, integers are used. In contrast, when discussing money, floats are more appropriate for precision.
- Numeric data types (integer and float) can be operated with mathematical operations. For example, the operation x + y is possible if x and y are both numeric.
- Errors occur when trying to mix different data types, such as performing a mathematical operation between an integer and a string.
- To circumvent the error from mixing data types, Python built-in functions can convert an integer to a float or a string, or vice versa.
- Even though data types behave differently, using the same operation (+) could yield different results based on the data type used.
In this video, we're going to look at the difference between Numeric Data Types and Strings in Python
Video Transcription
Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to explain the difference between numeric data types and strings. This may seem like a simple subject; however, based on my experience, people usually struggle with these different data types.
So first, what are the numeric data types? In Python, we have two major numeric data types: integers and floats. An integer is a whole number, so any whole number would be stored as an integer. For example, X = 7. A float is a number with a decimal point. Let's create a variable name y and assign it a value of 2.5. Any number with a decimal point will be stored as a float.
Now you might ask which one you should use, and the answer is it depends. It depends on what you're trying to do and how you're trying to use the values down the road.
For example, if we have many people and we want to split them into two teams, it would be silly to say, "3.5 people in Team A and 3.5 people in Team B." When we talk about people, we should use whole numbers (integers). However, when we talk about money, people want to be precise, so we should use floats. Even if it's exactly seven dollars, we still want to see that 7.0.
Since X and y are both numeric data types, we can do something like X + y. Math operations are only possible between numeric data types: integer and integer, float and float, or you can mix and match integers and floats. We can assign this to a variable name, total. Now, total will hold the float for us, and it's 9.5.
The main problem starts when people try to mix different data types. For example, if people do 7 (an integer) + "7" (a string), we immediately get an error. Don't freak out when you get an error; they're here to help us. They're trying to tell us that you cannot use the plus operator between an integer and a string.
But what if we still need to proceed with the separation? How can we fix it? We can use Python built-in functions. There are three options: we could convert the 7 to an integer and then we would get 14; we could convert the 7 to a float (7.0); or we could convert the 7 to a string.
If we convert the 7 to a string, we will be concatenating these two strings because the first integer is now converted to a string. Keep in mind that all data types behave differently, and even though we are using the same plus operation, we are getting totally different results. Please watch my other videos about other data types. Thank you.