Numeric Data Types in Python

Free Video Tutorial and Guide

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.

So 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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

For example, if we have a bunch of 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.

photo of Noble Desktop

Noble Desktop

At Noble Desktop you can learn how to code websites, build iOS apps, make graphics, and more. From our front-end coding bootcamp to Photoshop classes, we offer a variety of comprehensive day, evening, and weekend training classes as well as certificate programs in web design, web development, HTML email, and more. Courses are available in-person (at our training facility in Soho), live online, and customized corporate or private training. Our course materials and workbooks are used by colleges and schools worldwide. Learn a skill today and start using it tomorrow.

More articles by Noble Desktop

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram