The course is part of this learning path
We start with an introduction to an Interactive Development Environment and to the basic concepts of programming with the Python language. We then introduce how data is stored and represented within computers, and how primitive data types are used by Python to represent data within programs. We then learn more about how data is stored and collected by Python, how Strings and Tuples are stored and managed in Python and what the common operators are for manipulating these data types. Finally, we learn how to create a simple function that outputs a string to the computer console.
Hello and welcome back. In this lecture we'll introduce you to the concept of Python Datatypes and Variables. The objectives of this lesson are to introduce you to the Python primitive data types, explain to you how data is represented within a computer, and finally to demonstrate to you what can be done with some of the simpler primitive data types. Make sure you've completed the Hello World lecture where we learnt general and basic troubleshooting techniques for debugging Python scripts. You should have access to a local Python development IDE such as either PyCharm which we use or you can use Cloud9 from Amazon Web Services. Okay, to begin with, at its most primitive level computers store and process data in bits, either zeros or ones. Eight bits together makes up what we call a byte. B-Y-T-E, not B-I-T-E. The question is how does this data get represented within the computer itself?
Now, we have to go back to basic electronics for this. You need to think of computers as a massive pool of light switches, and these light switches either toggle on or off, and as they toggle on, let's say giving a signal of one, and then when they're toggled off, they give a signal of zero. Then this results in a stream of these ones and zeros that will come into a system and be acted on. By using different sequences of these bits, a computer is able to represent any kind of data. As you can see from this slide here, the sequence of zeros and ones could very well represent the number 65, or it could be representing an alphanumeric character such as the letter A. Another representation could be where the stream of bits is representing a hardware instruction which is instructing the computer to add two numbers together, or again it could represent a floating point number, for example 144.0. All of these examples are valid representations of data that a computer will understand. In this course we'll learn that Python itself provides many different data types that can be used to represent various types of data. So for example, Python provides primitive data types to represent booleans true and false, whole numbers or integers such as one, two, or three, et cetera, and also decimal numbers, or floats such as 144.0. Additional to these data types, Python also has data types for strings and various collection types like lists, dictionaries, sets, and tuples which you may not have heard of about before. And we'll learn about those next.
Meanwhile, let's demonstrate how to create and work with some of the Python primitive data types such as booleans and strings. I'm going to go ahead and launch the integrated Python console within the PyCharm application. Now in this first demonstration we'll work the data type known as the boolean. A boolean represents true or false, or in terms of bits, a one or a zero. Now Python internally represents this with the name True and False. Take note that the names True and False start with a capital letter. Now, notice that since I'm working with the PyCharm IDE, PyCharm is clever enough to recognize this data type and give it a color for me. If instead you were to use a lowercase T for True, you'll notice that the word itself doesn't change colors, and in this case the Python interpreter also doesn't know what to do with it, and so it throws and error as can be seen here. Now with True and False values, typical operations you perform on these are logical ones, that is to use logical AND or a logical OR operation between values. For example, consider testing the statement True or False. The outcome of this results in True since the OR operator only requires one side to be true. Alternatively, consider testing True and False. The outcome of this results in False since the AND operator requires both sides to be True. There are several other boolean type resolutions but we will not cover these at this stage. Now, recall that we earlier reviewed the internal representation of all these data types, and when it comes to the boolean values True and False, they can also be represented by the values one and zero, which enables you to do things like adding True with True and this returns you the value two because the internal representation of true is the number one. What about if you take the value true and add it to the value false? This as expected returns the value one. What about false plus false? Again, as expected this returns the result zero. And finally, if I were to take the number one and add True, and we will see the result is two. Okay, enough on booleans for now. Let's move onto numbers. Now for starters, there are two major types of numbers. The first is an integer. An integer represents whole real numbers. And the other type of number is what we call a float, which represents decimal place numbers, okay? With integers, you can perform simple arithmetic operations. So for example, as we see here, you can simply add two plus three which results in the integer five. Now mathematically Python observes the law of BODMAS which stands for brackets, orders, division, multiplication, addition, and subtraction.
This is the order in which mathematical operations are performed. Now what it really means is for an example you present the statement five minus two times two, as Python sees it here, then Python will evaluate this to be the number one by observing the laws of BODMAS. If Python on the other hand were to simply evaluate it from left to right, then this would result in six which is not true. The point here is that Python applies the rule of brackets before orders, before division, before multiplication, before addition, and then finally subtraction. So in this example, if I were to enclose the five minus two within brackets followed by multiplying it by two, then this does change the overall result, which as seen here is now six. That's a lot to take in but just keep that BODMAS acronym stuck on your wall and you'll remember. Brackets, orders, division, multiplication, addition, and then subtraction. Now when working with the division operator, there are some interesting and subtle things that happen to the underlying number data types. To demonstrate, we'll take the integer number 17 and divide it by the integer number three. As we can see here the result is a float number, 5.666 reoccurring, and this result is mathematically correct. Now the point here is that the division needs to convert the result into a float data type to be able to correctly represent the right mathematical result. The last thing we'll consider here when working with division is the case where we attempt to divide by zero. Now this fails expectedly and a zero division error exception is thrown by Python. Due to the fact that there is no mathematical solution to this, more to the point anywhere within your code where there is potential for this to happen, you should enclose this with an error-handling code, and in the case of Python a try-catch block, but more on this later. Just remember you have to handle exceptions. Okay, let's move on to characters or strings. Now for starters, the letter A by itself is a string data type.
If you were to take the letter A together with the plus sign, attempting to add it to a number two, would Python be able to evaluate and compute this? Well the answer as seen here is no. Python has no understanding of how to take a string type and add it to a number type. It's just not that clever. So instead it throws a type error exception. We'll dive much deeper into strings and available string operations in the next lecture, but for now just remember that difference and we'll move on and discuss creating variables and data assignments. So let's clear our console and then we'll create a new variable named test, and we'll assign it the integer value 64567. Now in doing so, Python has taken this value and assigned it to the variable called test. We can recall this variable like so and have Python go fetch the value that was assigned to it and print it out on the screen. Now if you are new to programming, you need to recognize that variable naming is very important. It is important because you need to be able to communicate what the variable is and how it's being used to the rest of the system. So finally, we'll demonstrate the very useful Python builtin print function which can be used to print out the value stored or assigned to a variable, like so, regardless of its data type. Okay, let's now return back to the slides and do a quick review of what we have covered in this lecture. We should now understand how data is stored on computers as bits and how Python represents data, or different data, through its various available data types. We should now understand the basic operations that can be conducted on these various data type, and we've had a basic introduction to Python syntax involved in declaring and assigning data to variables. Go ahead and close this lecture and we'll see you shortly in the next one.
Python Lists, Sets, and Dictionaries
Andrew is fanatical about helping business teams gain the maximum ROI possible from adopting, using, and optimizing Public Cloud Services. Having built 70+ Cloud Academy courses, Andrew has helped over 50,000 students master cloud computing by sharing the skills and experiences he gained during 20+ years leading digital teams in code and consulting. Before joining Cloud Academy, Andrew worked for AWS and for AWS technology partners Ooyala and Adobe.