98-381 Microsoft Introduction to Python – Variables & Data Types Part 2

  1. Python Data Types

Hey guys, welcome back. Now we’re going to see what are Python tools, what are lists, and actually, what are Python dictionaries? So let’s start with Python lists. Lists are the most versatile of Python’s compound data types. A list contains items separated by commas and enclosed with square brackets. It’s the same with arrays. It’s the same thing that we have seen before in arrays. So to some extent, lists are similar to arrays in CC Plus and Java. The only difference between them is that all the items belonging to a list can be of different data types. In arrays, all the items that belong to an array should be of the same data type.

While in Python, all of the items in a list can be of different data types. The values stored in the list can be assessed using the slice operator, with indexes starting at zero at the beginning of the list. So let’s see an example. I will create a list, with the first one being a yellow world value, a string value. The second one will be 100, no, 150. The third one will be 1.25. The fourth one will be my name, Kristos String, and the final one will be 100.25. So as you see right here, I have a string, I have an integer, I have a float, I have a string again, and I have another float.

So then I will go, and I will create a second, tiny list that has only two values. The first one will be 300, and the second one will be crystals. So I have two values. Then I’ll print the list as well as the tiny list multiplied by two. But one common mistake that I always make is that I forget to put the parameter in parentheses. Actually, I forget to enclose the attribute in parentheses because, in the previous versions of Python, this was not something that was necessary. So this is actually in the new versions of Python combiners. So now I will see what will be printed. But actually, because I have all these printed before the lists, I will create a print variable right here and I will write something right there. So I will actually right-click and run the Python file in the terminal.

So let’s see what we created right there. Oh, nice. So here’s a list of them. Here we have actually printed the complete list. Here we print the complete list, and here we print the tiny list two times. I can also print the first element of this list, and while I can also print as I did in the characters before, as you can see right here in the string, I can also print only the second and third ones. So I can tell from the first to the third. This means that the second through third items on the list will be printed. I can also print a list plus a tiny list. So let’s see what the outcome will be the outcome. So these commands will produce the following result: So I’ll right-click a Python file and select Run in Terminal. Let’s see the result. Right here, lists are being print of lists actually.

So I have the complete list printed. Then there’s the tiny list—300 crystals twice. Then I have only the first element of the list. And you can see that actually it’s only the value of the first element being printed. While here, because I have two values, my actual subsist is being printed as 150 and 125. And finally, I have the list plus a tiny list. So I’m printing all of these, plus 300s and crystals. So as you can see, this is a good example of how I can use lists and how I can use arrays in Python. and it’s something really valuable. So I will create a comment here, I will write lists, and I will write strings. So we’ll go ahead and see what tuples are. So I go right here, to tuples, and see some examples right there. Actually, a tuple is another type of sequence data that is very similar to lists. A tuple consists of a number of values separated by commas. But unlike lists, tuples are enclosed with parentheses.

So the main differences between lists and tuples are that lists are enclosed in brackets and their elements and size can be changed, while tuples are enclosed in parentheses and cannot be updated. So tuples can be thought of as something like read-only lists. So a tuple is a read-only list. So let’s see some examples. I will create a sample right here.

I will name it Sample Tubal. And I will create my name, “Hello World,” again. No, I will write my name as “hello, Christmas.” Then I will create the number 185, then 3.14, then I will create something like “high” again and then 150.23. So again, I can create a tiny sample there for Tubleright, and I will write 450 and John. So now I’ll write my name here, Christopher’s. So I can take all this, copy it, and paste it, and I will change this to tuples. As you can see, I’ll change all of the lists right there, copy them, and paste them right here. I’ll also take a small sample and replace the small list. Okay, so I have the same example but with tuples and tiny tuples right here.

So these commands produce the result that you will see right there. So I will right-click and then click “run python file” in the terminal. So, as you can see, we have exactly the same results. So I have a sample tuple right here: “Hello, Crystals,” “185,” and all these other values. Then I print the same tiny sample tuple two times. Then I have the first value from the sample tuple. I have the second and the third value right here, and I finally have a sample tuple and a tiny sample tuple at the same time. So if I try to taint something in this tuple, I will have an error.

So if I’m trying to change something like a tuple, the second value of the tuple will be 500, and as you will see, I will have an error. While this is not a problem for a list because I said that a tuple is a read-only list, as you can see here, I have a type error. Type objects do not support item assignment. While here, I don’t have a problem.

As you can see, everything is in order, correct? And if I do it here, if I write here before the print, you will see that instead of having the value of 1.25, I will have the value of 500. So let’s see, and exactly before here are cubicles, but if I go right up, I have 500. So the third value was changed, and 500 were printed. so very nice. Let’s see what Python’s dictionaries are. So actually, Python’s dictionaries are kind of table-type. Actually, they work like associative arrays, or hashes if you know pedal programming language, and consist of specific key-value pairs. So a dictionary key can be almost any Python type, but usually they are either numbers or strings.

So values, on the other hand, can be any Python object. So dictionaries are enclosed by curly braces, and values can be assigned and assessed using square braces. So let’s see an example. I’ll make a first light here dictionary and assign nothing to it. So then I will assign dictionaries, and I will assign the key one. This is the first element, and then I will use, I’m sorry, dictionaries no. 2 and 3. But be careful: here I have a string, and here I have an integer. So this is the second element. In addition, I will make a dictionary with key-value pairs. So, my name is Christos, my aides is is 33, my ID is 23456, and my job title is instructor. So as you can see, I can print a dictionary.

I will copy and paste actual dictionaries. Oh, there is a mistake; nodictionaries.com is a dictionary, and here we are again with a dictionary. So I’m going to print out a dictionary. So you can see that I will print the value of T 1 here, while I can also print Dictionary 2, copy and paste it, and then print the entire tiny dictionary. While I am here, I will print the value of key two, right? So there are some built-in functions that I can use right here, which means that I can use that I would like to print a diamond dictionary dot, and here you can see what I can really use after the dot.

I would like to use the keys so that I can print all the keys. I can also print Bryn with the tab you can create; you can complete the autocomplete suggested here. So I hit tab, which resulted in print tiny dictionary v; I’m sorry, VN tab. As a result, dot values But keys and values are functions, so they need parentheses. As you can see, the default values need parenthesis, and I will print all the values. So, I forgot to use print copy right here. And instead of tuples, I will also copy them right here. So I’ll say that I’d like printed dictionaries. So let’s see what we did. Right-click here, run the Python file in the terminal, and let’s see what happens; write ops. What we have here is a print dictionary. Okay, what I forgot was to actually use all the parentheses right here. Again, the same mistake as I made in the previous versions of Python—it was not necessary. So I always forget to use this parenthesis.

But it’s normal because printing is actually one of the basic built-in functions in most languages. So, once again, open Terminal and run the Ram Python file. And here we have all the values from the dictionary. This is the first element. This is the second element. as we assigned here, a key and a value. So we would like to print the value of the key one. This is the first element. This is the second element. Exactly. Then we would like to print the complete tiny dictionary. So we have all the tiny dictionaries he complete and then we would like only the keys to the tiny dictionary. And here we have name, AIDS, ID, and job. Well, here we have all the values. So as you can see, the values are crystals 33 for this ID. And the position is instructor. So you can see that, above, gold produced this result. So I think you saw an example of its data type. And we will see some more things in the next lesson about how we can convert types and achieve that type conversion. So I’m looking forward to seeing you in the next lesson.

  1. Python Data Type Conversion

Hey guys, welcome back. Now let’s see some things about data type conversion. So actually, sometimes you might need to perform conversions between the built-in types. So it is important to know how to use the type names as a function. So there are several built-in functions to perform conversion from one data type to another. This is true not only for Python, but for the vast majority of other programming languages, including Java, Flutter, Dart, CC++, and Sharp. In each language, you can use a function to convert to an integer or to a string representation of anything you might want.

So some very basic functions that you can use in order to convert something to an integer are actually the building function int, where you can actually, as you can see right here, convert a number or string to an integer or return zero if no arguments are given. If x is a number, return x, et cetera. So I’ll try to apply a transform 20 to an image, and I’d like to print it. So as you can see, I will right-click run the Python file, and here it is: 20. But what you can really do here is print something like this, and it will print 30 because this is a string, but it’s converted to an integer, and then it’s 20 plus ten divided by 3. You can use long at the same time so that you can convert this string to a long integer and also print it. So long isn’t used in this version, but it’s something you can Google and find the right built-in function to use to convert something from an integer to a floating point number like this.

I can use print float, which converts an integer to a string, and so on. So if I want to print it exactly plus 23.1, I’ll write 10 points 34. So you can see that it’s 33.5 right here. That is 10.4 plus 23 plus one. Again, you can use the built-in function STR to convert something to a string, or you can use complex, where you can convert a number or string to an integer, et cetera. But I repeat that in each language there are a lot of built-in functions, and you can really use Google to find the right one. Whenever you would like to transform to convert a string to an integer or an integer to a float point, you can also use list to convert a string to a list or something else to a list. You can use DICT so that you can create a new empty dictionary, a new dictionary insert from an mapped object purse, or a new dictionary insulated. As you can see right here, you can use the CHR, which means that it gets a parameter that is an integer and the return value returns a Unicode string of one character with an ordinal. So you can convert a string to unicode and a variety of other formats if you need something more specific, such as converting a single character to an integer or a hexadecimal string. But this is, of course, something specific that you would like to do. So I think that you now have an overview of variables in Python, and we can go on and see some more specific things. So I’m looking forward to seeing you in the next lesson.

  1. Variable Operators

Hey guys, welcome back. As a result, programmes declare variables to store information. Actually, they manipulate, examine, and update this information when they run. So simple variables typically store some value input by the user or some value calculated by the programme from user inputs. When a programme runs, the values of some of its variables change. So the value stored in a variable can vary as the programme runs.

So, what exactly are operators? Actually, an operator is a very vital programme element that is applied to one or more operators in an expression or statement. The two most common operators are either arithmetic or logical. So computer programmes are widely used for mathematical calculations. We can write a computer programme that can do simple calculations, like, for example, adding two numbers: 2 plus 3. And we can also write a programme that can solve a complex equation. For example, four, multiple, seven plus three, et cetera. That is, if you have been a poor student, you should be aware that you can solve very complex questions that may appear difficult to you using these operands. Actually, this is due to the existence of similar concepts in computer programming. So you can see here, for example, that in the variable y it will have the value “two plus three,” which means that we have the value “five,” while in the variable n it will have the value “eleven minus two multiple four,” which is actually “eleven minus eight.”

As a result, one will have the value three. So these two statements are actually called “arithmetic expressions” in the programming language. The two more basic are plus and minus, which are used in the examples that we show. We also have multiplication and division, and this is actually a very important arithmetic operator. That is the remainder. For instance, if there are 28 remainders, there will be five multiples of five, which equals 25. So the remainder of 28 is 312. The remaining ten would actually be two. So 48 once more, with a remainder of 10. Stop the video and write me the answer. This means that ten, or four multiples of ten, equals forty. So the remainder is only eight. To give one more example, 51 minus five equals one. I’m confident that after these examples, we understand the remainder example. You might be wondering when and for what problems it could be used. Believe me when I say that this operator is very common and that you will need it for a variety of problems. Actually, the most commonly used operator is in problems like wanting to know if a number is even or not.

So actually, a programming language provides very charismatic operators. These are the most used. We have addition, as well as string concatenation and delegate combination. So, if x has the value crystals and y has the value crystals are bad, we will have the sentence crystals are bad after this and this, as well as subtraction, which means delegate removal. So we also have conditional operators. Logical operators are actually very important in any programming language, and they can help us take decisions based on certain conditions. Suppose we want to combine the result of two conditions. Then there are two logical operators that help us produce the final result: logical end and logical or. So in this example, we would evaluate Y only if X is true. This is a conditional end. So we have to be true. X and Y are also included. In this conditional, Y is only evaluated if X is false. which means only if one of the two variables is true, the result will be true. For example, here true false will result in true, while here true false will result in false.

Actually, this is true. So consider, in relational operators, a situation where we create two variables and assign them some values. We could actually check in these relational operators if the value of the variable X equals the value of the variable Y, or if the value of the variable X differs from the value of the variable Y, if the value of the variable X is greater or equal, and finally, if the value of the variable X is less or equal. So with these symbols, we call them relational operators in their simplest format form, but actually they produce boolean results, which means the result will be either true or false. If the value of the variable X is equal to the value of the variable Y, then it will be true; if they are different, it will be false. All of these categories are the same. Goodies in all these conditions Thank you very much, and I look forward to seeing you in the next lesson.