In earlier videos, we called out that text written between quotes in Python is called a string. In programming terminology, a string is known as a data type, whether it's a mobile game or a script used to automatically create user accounts. Most programs need to manipulate some kind of data, and that data can come in a lot of different forms, or like we call them data types. A string is only one kind of data type found in Python. There's a bunch of others, like an integer which represents whole numbers without a fraction, like one, and float, which represents real numbers or in other words, a number with a fractional part like 2.5. Generally, your computer doesn't know how to mix different data types. For example, adding two integers together makes perfect sense to computers, like this: print(7+8)
15 Adding together two strings also makes sense: print("hello " + "world") We just end up with the longer strings that contain the two, like so: hello world but your computer doesn't know how to add an integer and a string. If you tell it to mix these two different data types: print(7 + "8") your computer isn't going to know what to do and will raise an error. Check it out: TypeError: unsupported operand type(s) for +: 'int' and "str' Oh, no, our first error, but don't panic. Errors are a common part of programming, and you'll probably have to deal with them a lot. The trick is to think of errors as little clues from your computer to help you improve your programming skills. Read the errors carefully, understand what they're telling you, and then use that new knowledge to help you fix the mistake. In this example, the last line of the error message shows us that we've encountered something called a TypeError. Then we get a bit of explanatory text, that tells us that the plus sign can't be used between an int type and an str type, which are short names for integer and string. Thinking about what we've already learned about strings, integers, and mixing data types, can you guess what the error is trying to tell us? The message unsupported operand type, tells us that we can't add the integer seven and the string eight, because they're different data types, but what if you didn't have an instructor to helpfully point that out? How would you know? You'd need to use your research skills and the resources we called out earlier in the course to do some investigating. For example, you could look for information about the error by pasting the TypeError message into the search bar of your favorite search engine. This is a common trick used by almost everyone learning to code, and even by experienced developers. You'll usually find that other people on the Internet have reported similar errors and solved them too. Back to our example. Maybe you're thinking, aren't we adding two numbers here? Looks a bit like it, right? Well, look carefully and remember that anything wrapped in quotation marks is considered a string in Python. So eight is a string here: "8" while seven is an integer. To the computer, adding seven plus eight is just as strange as adding seven plus A is to us, and seven plus A equals no sense at all. It might be helpful to think about data types in terms of information they can represent. For example, the name of a file would be represented as a string data type, while the size of that file might be an integer data type. If you're ever not 100 percent sure what data type a certain value is, Python gives you a handy way to find out. You can use the type function, to have the computer tell you the type. This might come in handy when dealing with code that someone else wrote and you're not sure what data type it's using. For example, print (type("a")) class 'str' Print(type(2)) class 'int' print(type(2.5)) class "float" pretty neat, right? This tells us that A belongs to str class, which like we said earlier is short for string. The number 2, belongs to the int class, which is short for integer, and 2.5 belongs to the float class. We'll talk more about what we mean by class later in the course. For now, you can just use it as a synonym for data type. So now you know three very common data types in Python. There are plenty of others you'll be using soon, but don't worry about them at the moment. As we continue through the course, we'll come across more data types and learn how to interact with each of them. For now, just remember, mixing your data types will get your computer, well, all mixed up. So keep your strings with your strings, your integers with your integers, and your floats with your floats, and you shouldn't get in too much of a tangle.