![]() ![]() Print(x*2) # prints AA - repeatition operator The triple quotes (“””) can be used for writing multi-line strings. 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.The string can be defined as the sequence of characters enclosed in single, double, or triple quotes. For now, just remember, mixing your data types will get your computer, well, all mixed up. Python basic data types how to#As we continue through the course, we'll come across more data types and learn how to interact with each of them. There are plenty of others you'll be using soon, but don't worry about them at the moment. So now you know three very common data types in Python. For now, you can just use it as a synonym for data type. We'll talk more about what we mean by class later in the course. The number 2, belongs to the int class, which is short for integer, and 2.5 belongs to the float class. 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. Python basic data types code#This might come in handy when dealing with code that someone else wrote and you're not sure what data type it's using. You can use the type function, to have the computer tell you the 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. 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. It might be helpful to think about data types in terms of information they can represent. Python basic data types plus#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. So eight is a string here: "8" while seven is an integer. 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. You'll usually find that other people on the Internet have reported similar errors and solved them too. This is a common trick used by almost everyone learning to code, and even by experienced developers. For example, you could look for information about the error by pasting the TypeError message into the search bar of your favorite search engine. 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. 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. In this example, the last line of the error message shows us that we've encountered something called a TypeError. Read the errors carefully, understand what they're telling you, and then use that new knowledge to help you fix the mistake. The trick is to think of errors as little clues from your computer to help you improve your programming skills. ![]() Errors are a common part of programming, and you'll probably have to deal with them a lot. Check it out: TypeError: unsupported operand type(s) for +: 'int' and "str' Oh, no, our first error, but don't panic. 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. For example, adding two integers together makes perfect sense to computers, like this: print(7+8)ġ5 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. Generally, your computer doesn't know how to mix different data types. 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. ![]() A string is only one kind of data type found in Python. 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. 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. In earlier videos, we called out that text written between quotes in Python is called a string. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |