98-381 Microsoft Introduction to Python – Variables & Data Types
So after seeing a sequence, which is actually the most foundational concept in programming, we are going to see the most foundational data type in programming: variables. So, what exactly is a variable in computer programming? A variable is a storage location with an associated symbolic name. Actually, this is an identifier that contains some known or unknown quantity of information referred to as a “value.” So, for example, we could say that, as in mathematics, we have a variable named “identifier,” which is actually “x,” and this identifier has a value of five. This means that whenever we call the variable, the value will be five, but the identifier could be anything that is actually a symbolic name or an associated symbolic name. So it could be x, but it could also be that the tax ID is 231450. So this is my identifier, and this is the value.
So as you can see here, this is my identifier, and this is my value. So, in computer programming, a variable is a storage location, an identifier, and only contains some known or unknown quantity for information referred to as a value. It is significant because the variable name is the most common way to refer to the stored value, as well as the variable itself depending on the context.
So this separation of name and content allows the name to be used independently of the exact information it represents. So the identifier in computer science and computer source code can actually be bound to a value during runtime, and the value of the variable may change during the course of programme execution. So this is actually the main concept of a variable: that it has a value that could change during the course of programme execution. Let’s look at what “variable” means in math. For example, as we saw in the previous slide, “x equals 5,” which means that whenever we see x, it equals five. The same is true for programming, but actually, at the same time, it’s a little bit different. In programming, variables mean that the value varies and could change while the programme is being executed. So, for example, we could have that x equals five. Actually, this is something that we will have to do that.
This is a command, and with this symbol the command ends, which is actually a bit common between different programming languages. The second one means that Y variable has the value eight, and this one means that Z variable will have what x has and what y has. As a result, the sum of the values of the variables x and Y will be. So I’d like to pause the video right now and ask you to predict what the value of the variable Z will be. What will be the value of the variable Z that equals the value of the variable x plus the value of the variable y? Actually, x has the value five, and y has the value eight. So z equals five plus eight, which means 13. So this code means that we assign the value x to five, then we will give the variable y the value of eight, and finally the variable z will give the value corresponding to the sum of x plus y, which means 13. So this was an example of a sequence structure where a command is executed once the previous one has been completed.
So, as you can see from this example, variables in programming may not directly correspond to the concept of variables in mathematics. Actually, as you can see, it’s a little bit more abstract, having no reference to a physical object such as a storage location. So the value of computing variables is not necessarily part of any question or formula in mathematics. Variables in computer programming are frequently given longnames, as we saw in the previous paradigm and example, to make their use in our code more descriptive. Whereas variables in mathematics often have one- or two-character names for brevity in prescription and manipulation, As a result, a variable storage location may be referred to by multiple identifiers, a situation known as “allianzing.” So assigning a value to the variable using one of the identifiers will change the value that can be assessed through the other end of the equation. So compilers have to replace variables’ symbolic names with the actual locations of the data. Compilers are actually something that we will see later that actually takes the code and transfers it into our computer in a way that a computer can understand our code. So while a variable’s name, type, and location often remain fixed, the data stored in the location may be changed during programme execution.
Let’s see another example. We have this time variable x as well as a variable x in this command x. Here’s the value of five. So I’d like you to pause the video and tell me what the variable’s value is. It’s pretty obvious; in fact, it’ll be five this time. You can see that this is something that, in mathematics, I cannot say: x equals x plus y. I could only say that in mathematics if, actually, y said the value of zero. So x equals x plus zero, which means x equals x. But in computer programming, it’s something completely different. It means that, as we can see in this example, we had a sequence, which means that each command is executed after the previous one is completed. So the value is “x is five,” I’m sorry, “the value for x is five,” and “x has five” is also in this code, in this command. But there, this means that the value of the variable x will change and will have a new value that equals the sum of what the value of the variable x was until this time and also what the value of the variable Y was until this time.
So what will be the new value of the variable X? Stop the video and write it in the comments, please. As you could see, So the answer is that the new value will be what the value of variable X was, which was five, and what the value of variable Y was, which was also five, as you can see from here. So the new value of the variable X is ten. And if we had other commands in all these commands that we referenced in variable X, then the variable X would have the value ten. If we had other commands and referenced them in variable X, the value of variable X would be five. And here it is again: the value of the variable X is five. But after this command, the value of the variable x changes, and the new value is ten. The same happens here. The new value of the variable Y will be, after this command, two times the value of the variable Y before this command plus ten. So stop the video and write in the comments what the new value will be. And actually, the answer is that the new value would be two multiplied by five.
This is the last assignment in the variable plus ten, which means that it equals ten plus ten, which means that the new value is 20. So, if we refer to this example, the value of the variable Y would be 20. I believe that after this example, you will completely understand what the variable means. Actually, this is an example in C. This example was chosen because it is from a very common programming language in C. But as I told you in the previous section, it has nothing to do with the programming language. Once you’ve been taught and learned all of the programming concepts, you’ll notice that you’re independent of the programming language you use. So let’s try to read this code and understand what we do here. Actually, this one is a comment, which means that whatever we write here between these symbols, it’s something that the computer will not read; we write this for ourselves. So this one is a comment, as we call it in programming languages.
And this one is something that we always use in the C programming language and has to do with the start; it has to do with the fact that we will use our main function in this program; actually, this is our program, and this means that we will use a variable that will have integer values. So this means that we have a variable with the name number, and that variable will have values of an integer data type. So then we are going to give a value to our variable number. The value is seven, and then we want to print this method to my screen, which will print the message from my first program. This means that we will change lines. So print these methods and change the line. And this command means that you can also print these methods. But then I would like to print the value of the variable number. So it will print my first program. It will change lines. It will then print the number Ichoose is, followed by the value of the variable number, which is seven. So it will print the number that I choose, which is seven, and it will again change lines. So this is a very good example to understand the meaning of the most fundamental data type in programming that has to do with variables’ logic. I hope to see you again in the next chapter.
So in the previous lesson, we saw the fundamental concept of variables. In this lesson, we are going to see data types. Actually, value-type variables can be assigned a value directly. The value types directly contain data. As you can see, some examples are integers, which we will analyse later, while others could be characters or floating point numbers, implying that we store numbers, alphabets, and floating point numbers, respectively. When you declare an integer type, the system allocates memory to store the value. So in this table, we will see the most common value types that you will meet in all the programming languages.
These data types can be found in C, C sharp, Java, JavaScript, Angular, and Google Flutter in all languages. So it is important because at the core of any programme are variables. Variables, as we saw, are where the dynamic information is stored, and actually, when you type your name into a web form and send it, your name is a variable. So not all variables are the same, though. In fact, there are many different types of variables that nearly every programming language has. So let’s take a look at a few of the most popular ones, as well as the resort names. The first one, the first under the type, is “Bullet,” which means that we have a variable to represent true or false, or it could also mean zero or one, or it could mean on or off, so this is the simplest data type and commonly used. Actually, it is used as a flag in the programming.
It could have been either true or false. Either zero or one represents the Boolean value, and the default value if we are not going to assign a value to this variable is going to be false. The second one is int, as we call it, which means that it’s actually an integer type, a whole number, meaning there are no digits after a decimal point. So, for example, 65 would be a valid integral, but 65.78 would not. The default value is zero. And this is the range of this data type because it will use, as we said previously, actual memory to store the value when we declare an integer type, and the memory is very specific according to the data type. So we can use the integer type if the integer is between -2 billion and plus 2 billion. The third one is what we call a car. That means that it’s actually a character. And this is a single character. like it could be a letter, a digit, or even a symbol. You don’t often create single-character variables, but they are the core of the language, so you need to know what they are. Actually, this is the range in the Unicode system, and the default value is the escape character zero.
So the next one is about floating-point numbers, which we will encounter in every language. In almost every language, it is represented as “float.” Actually, it is a number that may have digits after the decimal place. So 65.0 is technically a floating-point number, even though it could be represented just as easily as an integer as 65. Actually, it takes more memory to store afloat, which is why there is a distinction instead of just creating a number data type. So it has more memory than an integer. And, as you can see, the range is much wider, with 0.0 as the default value if we don’t assign any value to this variable. So the next data type is actually a double data type. as can be seen here. We had a 32-bit single precision floating-point type, which means there are huge raids between this one and another, but we could also have an even larger number, which is represented as a double data type. It’s also a number with more digits—many more digits after the decimal place—than you might think. So, technically, it’s a floating-point number, but with much greater precision, as the name suggests. So the range is much, much bigger. Also, the default value is 0.0. Finally, string means that there is a string of characters of any length. So for example, as we said in the previous example, our name on a web form would be stored as a string variable.
So a string is a set of characters of any length. For example, we could say that if we say that name, it could have the value “Smith.” So this is a variable that we will say is a string, which means that it could have string values. So, moving on, programming languages can be divided into those that are strongly typed and those that are weakly typed. Actually, a strongly typed language requires that you explicitly declare what type of variable you are creating, and they get very upset if you start trying to do things with them that you shouldn’t. For example, a strong type language would give you errors if you tried to add an integer and a stick together. A weekly typed language, on the other hand, will not have any issues and will actually alert you, but your programme will compile and run. So it might seem at first like weekly typesetting is easier to do, but it can often result in curious errors and unexpected behaviours that take a while to figure out. So let’s go on and see the difference between assigning versus testing for equality.
So in the first state, we have a equals five, or a equals five. Actually, the first one has to do with the assignment. If we want to tell the difference. The first is known as assignment. It means that we assign the value of five to variable A. So we are setting the variable value, while the second statement is one of equality. So what it actually means is an actual five. And the answer given back to you would actually be a Boolean value. It could be either true or false. You will see how this can mess up your programmes in later lessons. So in the first one, we assign the value five to variable A, while in the second one, we check if the value is equal to five. If this were a program, then this would be true because in the previous command we assigned the value five to the variable A. Thank you very much, and I look forward to seeing you in the next lesson.
Hey guys, welcome back. Now we will see variables and variable types in Python. So variables are nothing more than reserved memory locations to store values. So this means that when we create a new variable, we actually reserve some space in memory. So Python variables do not need explicit declarations to reserve memory space. Declaration happens automatically when we assign a value to a variable. The equal sign is used to assign values to variables. So let’s start by using an integral value variable. I’ll give it a value of 100 and leave a comment with the actual sharp sharp symbol. I will write here that an integer variable is declared.
So I will zoom in a little bit. So an integral variable is declared very nice. And then I will write another one, an astring variable with the message “Yellow World.” So this is a string o this is a string Then I would like to print counters, and then I would like to print methods. So I will right-click right here. In the terminal, run the Python file. And as you can see, it says that I have a syntax error with missing parentheses in the call to print. Did you mean print counter? Exactly. Print is actually a built-in function. And as you can see, when I click “Write Print,” I write the left parenthesis here. You can see that this built-in function needs a value. Actually, the value that will print Actually, this value is something that should be in this built-in function, while the other parameters could be omitted because this is underlined. So with PARAM values, we print the values to a stream. And this is the optional keyword argument. I could say if I want a file, if I want a space between values, and the string appended after the last value, which is usually a new line, but it could be anything else. Finally, whether or not to forcibly flash the string. So for now, I will print only the counter, and in the next line, I will print only the message. So right now, I think it would be okay.
Run the Python file terminal. As you can see, my programme printed this message and then printed it in a new line, which is the new line at the end by default. So the counter and print messages So, hello, and welcome to the world. Right here were the values assigned to the variable counter and to the variable methods. So you can see the result right here. But what I can really do in Python is use multiple assignments. Python allows you to assign a single value to several variables simultaneously. What does it mean that I can use this one? A equals b equals c equals the counter. And this means that I assign the value of the variable counter to the variables A, B, and C. So as you can see, I can then print right here, “A,” and then in the next line, “B.” So I’d like to pause the video right now and consider what the output will be after running this program. What will my programme print when I execute it? Okay, let’s see together. Right now, I’m running Python Filing Terminal, and I have these methods. Hello, Python. Hello, world. Hello, students. And then I have three print functions right here. The first one will print the value of the variable A, which is 100. The second one is the value of the variable B, which is 100. And the final one is the value of the variable method, which is “hello world.” Actually, as you can see, an integer object is created with the value of the variable counter.
And variable A, variable B, and variable Care are all assigned to the same memory location. So we can also assign multiple objects to multiple variables. So, on the other hand, I could say that I’d like a, B, and a comma C to be assigned with counter plus message. But this could be an error because the message is actually assigned right here. So I assign the value calendar and the value of the variable methods to the variable ABC. So let’s see what will now be printed after executing our program. Run the Python filing terminal. And as you can see, okay, there is actually an error right here, because what I would like to say is that I would like these three values to take three values right here at the same time. But here I have three variables, and here I have two variables. So I couldn’t do something right here. But if I write counter comma counter comma methods, then this means that the value of the variable counter will be assigned to the value of the variable A. The value of the variable counter will also be assigned to the variable B, and the value of the variable methods will be assigned to the value of the variable C.
So I would like to print A, B, and C, and then I would like to print the methods. So please also pause the video and think about what will be printed after this programme is executed. So I will right-click right here. I will click “run Python file” in the terminal. And as you can see, print A, which means 100, and print B, which means the value of the variable counter, which also means 100. Then I’ll print C, which contains the value of the variable methods. Hello there, world. And again, I will print the value of the variable methods. So you should also remember that the data stored in memory can be of many types. For example, Mansades is stored as a categorical value, and the address is stored as alphanumeric characters as a string. So Python has various standard data types that are used to define the operations that are possible on them and the storage method for each of them. So, Python has five standard data types. The first one is numbers. The second one is string. The third is a list. The fourth one is Tuple. And we will see what we mean with Tuple. Dictionary is the fifth one. We’ll see more of this in the coming lessons.
So actually, I would like to give you some examples right here. So I would say that I have VAR1, and I will assign the value 2000. So this is the first one. This is a number. It is important to know that number datatypes store numeric values that number objects create when we assign a value to them. But we can also delete the reference to another object by using the del statement. So I can use del VAR 1, and then delete the reference to the number object VAR 1. So Python supports four different numerical types. The first one is India. The second one is floating. That means floating-point real values. The third one means that we have a long time. I will use parentheses; long means long integers. The fourth is complex, which refers to complex numbers. So these are the fourth numeric type. Actually, Python also allows us to use strings. So strings in Python are identified as a continuous set of characters that are represented by question marks. So we can actually use this as a STR VAR variable (string variable) and then print STR as we did before.
So, how are you going to be printed? But what I can also do is define what I want to print so that I can type print strength. I’m sorry, STR, and then use this parameter. And I will say that I would like to print only the first character. I would like to remind you that in arrays, the first element of an array is actually the element with the number zero. So what I really do here is convert this string variable to a character array. So I would like to print only the first character. And with this one, I print the first character of the string. What I can also do is actually print characters starting from the third to the fifth character of this string. So we print characters starting from the third to the fourth to the fifth. I’m sorry. I can also print concatenated strings, which allows me to print stripes. So this one will print. How are you? Hello people. So if I try now to execute “oh,” and I would like to show you something more, I can also print stride by four, which means that I would like to print string five times, while this one means that I would like to print concatenated strings. So now I will try to execute. I will right-click and write “Python file internal.” Oh, yes, I have a syntax error in line 29. So I’m going to line 29, and here, of course, it should be inside. Yes, right there. So run the Python file and terminal again. And as you can see here, I have my messages.
How are you? It’s printed here. I have only the first character of the string. Here I have the string, the third through the fifth character, which is w. And I’m afraid I’m missing the third to fourth character. If you want, we can also have the fifth, sixth, and seventh. I’ll write seven here and try to run it again. As you can see, I have the space, which is also a character, and these three characters right here. So forth to the 7th character. And here I would like to have concatenated strings printed. How are you? Hello, people. And here I have to print this string five times. 1234 I’m sorry, I’ve multiplied by four, so four times, I say. And all of this leads to this outcome. So thank you very much, and I look forward to seeing you in the next lesson, where we will see lists, tuples, and a dictionary.
Popular posts
Recent Posts