In this section, you will explore data types, variables, input and output functions, and discover more about calculations and mathematical operators in Python.
Sarah: Hello. In this video we're going to look at data types and variables, which are important in all programming languages because generally we're writing programmes to process data in some way. And the data might be audio recordings, it might be pictures, it might be numbers, readings from sensors, documents, anything. And, the programming language needs to know what sort of data it's got, so that it can understand the contents of the memory which is where we store while, while we're processing. The basic data types in Python are integers, floating point numbers, strings and Booleans. There are other data types, but these are the, kind of, fundamental four. So, we're going to look at those in Python and we're going to also look at the IDLE programming environment which is what we're going to use in these videos. So, now I'm going to open IDLE and this is a, a simple environment. It's shipped with Python, so if you've got, downloaded Python, you've got this. We're using it for the first videos because there's not much going on apart from the programming, so you can concentrate exact-, just on the Python code. The Python code will be the same whatever you use. This is the interactive part of it where we've got a little prompt with three arrows and we can type a Python command and it will do something. And, what I'm going to do is set up four variables, one of each of the basic data types and then use a couple of Python useful functions to have a look at them. So, first of all I'm going to say, 'a equals 6.' Same. So, what I've done there, is I've said I want to call-, have a variable called a and put 6 in it. 'a', was my choice, I could have called it anything. Normally you'll be starting with a letter. You can start with an underscore. You can have letters or numbers after and in most Python programmes, you expect it to write meaningful variable names, but here I'm calling it 'a', and that's my choice. It's now telling me that it's got a 6 in some part of the memory and the 6 is an integer which I've put there. I'm now going to do, 'b equals 77.56'.
That's another variable with a different kind of a number in it, a number with a decimal point. I can do a string variable, which is I'm going to put my name. In Python you tell it it's got a string by putting quotes round it. They could be double quotes or single quotes, in Python it's the same. You could also have triple quotes for a string, but you do that for strings with many different, many lines in them. And, the last basic data type is Boolean and that's a thing that can only be true or false. So, if I say, 'd equals True'. You see how it turns orange? It turns orange because this means something to Python. 'True', is a, an actual thing in Python. So, now I've got two kinds of numbers, string and a Boolean variable and I can use-, there's three very useful Python functions that we use all the time, to find out what we've got and what it can do, built in, which is 'type', and, 'dir', and, 'help.' 'type', tells you what kind of thing you've got. So, if we try that with these variables, we can do, 'type' of 'a' and it says it's, 'class 'int'. It says, 'class', because Python is an object-oriented language and everything has a class. Everything is a type of a thing with all kind of properties, and this one is an integer and Python calls that, 'int'. If I do, 'type' of 'b', that's the one that had the decimal point in, it's, 'class 'float', which means it's a floating point number, which means it's the way a lot of programming languages call these kind of numbers with a decimal point, floating point numbers. Because the point, it could be, have any amount of things before it or after it. It's not just a, a stable whole number like the integer was. And if I do, 'type' 'C', it's going to tell me, 'class 'str', which means string, which means text. And, in Python the string class is called, 'str'. And, if I do, 'type d', it'll tell me Boolean, 'class bool'. And, that can only be true or false if it's, 'class bool'. Those are the four absolutely most basic data types in Python. Now apart from knowing what it is, our next function, this, 'dir', here can tell us what it can do or what it knows, things about it.
It's like a directory of the thing. So, if we do, 'dir' of 'a', it comes up with all that stuff. The ones with the double underscores at the beginning are-, we'll talk about some of them much later in, in the course. They're internal Python things that you want to ignore for now. The ones that don't have the double underscore towards the end of the list, is functions or properties that this, 'a', variable knows about or can do just because it's a, an integer. We get a more interesting list of stuff because integers don't do all that much, if we do, take the string and we do, 'dir' of the string. Here you see there's, there's fewer of the double underscore ones but there's a lot more of the plain usable ones that don't start with a double underscore, 'capitalize', 'casefold', 'center', 'count', 'startswith', 'strip'. And, these are all things I can do with my string just because it's a string. So, if I do-, how I do it, is I put a dot, 'c', dot. If I choose, at least it's giving me a choice, 'upper'. If I choose one of these, to tell it to actually do the function I put brackets around it and, 'c', Dot 'upper', is, 'Sarah', in uppercase. So, that's all built in, you can use it without writing any extra code and there's a lot of things for strings, because there's a lot of processing you often want to do with strings. The other one we mentioned that comes in useful is, 'help', and if I didn't know what, 'c.upper', was going to do, I could use, 'help'. And I can ask what is it and a little bit of help comes up. It says a, 'built-in function upper'. It's an upper method of the string and it returns a copy of the string converted to uppercase, so that help will tell us. You notice that when I put, 'upper' and 'help', I didn't put the brackets. It just asks-, I'm just giving its name and saying what is it? So, these three functions are very useful finding your way around. 'type', tells you what something is, 'dir', tells you what it can do and, 'help', explains the, the way things behave. So, they're good useful ones to know.
So, what we've done in this video, is we've set up a few variables, we've talked a little bit about variable names which normally should be more meaningful than, a, b, c and d. We've looked at the four different basic data types and we've looked at the sort of properties that you might have or methods that you can use with the data types, and that you can find out using, 'dir', and you can find the explanation using, 'help.' We've also had a look at the, the IDLE interactive shell that we can use to just type in Python commands (mw 07.12)
A world-leading tech and digital skills organization, we help many of the world’s leading companies to build their tech and digital capabilities via our range of world-class training courses, reskilling bootcamps, work-based learning programs, and apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.