0% found this document useful (0 votes)
58 views2 pages

Datatypes

This document discusses different data types in Python including strings, integers, and floats. It provides an example of a TypeError that occurs when an integer and string are added together, which is unsupported because they are different data types. It encourages the reader to carefully read error messages to understand what went wrong and how to fix issues, and also explains how to use the type() function to check the data type of a value. In general, the computer does not know how to mix different data types.

Uploaded by

Pavankumar Trvd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

Datatypes

This document discusses different data types in Python including strings, integers, and floats. It provides an example of a TypeError that occurs when an integer and string are added together, which is unsupported because they are different data types. It encourages the reader to carefully read error messages to understand what went wrong and how to fix issues, and also explains how to use the type() function to check the data type of a value. In general, the computer does not know how to mix different data types.

Uploaded by

Pavankumar Trvd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 forums, 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. Adding together two
strings also makes sense. We just end up with the longer strings that contains the
two, like so, 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, your computer isn't
going to know what to do and will raise an
error. Check it out. 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. When we get a bit of
explanatory texts, 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
pointed 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. Well, look carefully and
remember that anything wrapped in quotation marks is considered a string in Python.
So eight is a string here, 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 types, while the size of that file might be an integer data type. If
you're ever not 100 percent sure what data types
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 types it's using. For
example, pretty neat. 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.

You might also like