98-381 Microsoft Introduction to Python – Arrays

  1. Arrays

Hey guys, welcome back. Now we’re going to have CRAs consider a situation where we need to store three integer numbers. If we use simple variable and data type concepts in programming, we will need three integer data type variables. And the programme will be something like this: As you can see, we will have three variables that are actually integers. Following that, three values will be assigned. Following that, we have three print commands, three def. actions, and three numbers to print. Actually, it is simple because we had to store just three integer numbers. Now let’s assume we have to store 5000 integer numbers. Are we going to use 5,000 variables?

Actually not. So, to handle such situations, almost all programming languages provide a concept called an array. Actually, an array is a data structure that can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

So instead of declaring individual variables such as “number one,” “number two,” “number three,” “number nine,” etc. You can just declare one array variable with the number of indicator types and use, for example, numbers one and zero. As you can see, we can have an array of numbers, and we can have an index that indicates which variable we are using. So we have zero, one, two, three, and the final element. If we have 100 or if we want 100 variables, then the last element will be 99. So actually, as you can see, zero, one, two, et cetera are indexes associated with variables, and they are being used to represent individual elements available in the array. All the arrays consist of contiguous memory locations.

The lowest address corresponds to the first element, and the highest address corresponds to the last element. So to create an array variable, a programmer specifies the type of the elements and the number of the elements, the type of the elements as we did in variables, and the number of the elements to be stored in that array. So given this, this is actually simple. CDEx A single-dimensional array is an array created in the C programming language. The array size must be an integer constant greater than zero, of course. Type can be any valid data type. For example, in order to declare an element array called “Number of Type Integer,” we use exactly this statement. So here, Number is a variable array, which is sufficient to hold up to ten integer numbers. So you can initialise an array either one by one or using a single statement like this where we declare that we will have an array called Number, that we will have five values, all integers, and that the values are 1020, 1314, and 15. Or we could actually assign a value immediately and say that the fourth element has a value of 50. So as you can see, the number of values between brackets cannot be greater than the number of elements that we declare for the array between square brackets. So this statement actually assigns elements until the fifth element in the array with a value of 50.

All arrays have zero as the index of their first element, as you can see. And this is actually called the base index. And the last index of an array will be the total size of the array minus one. So this is the base index, and the last index is always the total size minus one. As a result, I’ll write it as array size minus one. So actually, as you can see, this statement will take the 10th element from the array and assign the value to a variable. So an element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array, like here. So the array is a number, the index is nine, and the value of this element will be stored in the variable named VAR that actually stores integer values. So this is an example that uses all three of the above-mentioned concepts: creation, assignment, and accessing arrays. Creation Assignment and Accessing Arrays So when this code is compiled and executed, it produces the result that we will have, as we can see. Let’s take it step by step. We have a new array of ten integers. This is a variable I that will have and that will be set to zero. And then we say that as I is less than ten, then each element of this array will have the value of I plus 100. So, if the initial value is zero equals 100, the value will be 1. The new value of variable i, the element number one, would be one plus 100.

So we would have that number zero equals 100, number one equals 101, number two equals 102, and the last would be number nine, as we could see, because it’s the last I that is less than ten. So it will be number nine; that would be 109. So these will be the store values, but these values are actually printed there. So as you can see, we use a loop statement. During the loop statement, we re-initialize variable I to zero, and then we use the same variable again in order to use this print command that we will print the exact number of the index and then the value of this element. So with this command, we actually print the values of an array. So this is the last part of the fundamental elements of programming, coding, programming logic, and algorithmic logic. And with these basic elements, you will be ready to understand every language, every high-level programming language, because all the programming languages use the same structure and the same logic. You will now be able to actually read and understand code in almost every programming language. So thank you very much, and I look forward to seeing you in the next lesson.

  1. 2D Arrays

Hey guys, welcome back. So now let’s see the two dimensional arrays. What does it mean to use a 2D array, and what are the differences from 1D arrays? So first of all, let’s go on with the 1D arrays so that we can go on and spot the differences. An array, as we said in the previous lesson, is actually an indexed set of related elements. So when we want to store several pieces of data at the same time, instead of using many variables, we can use an array. So you can think of an array as a list of data items, a list of elements, each with a number or index so that it can enable you to refer to its item. An array keeps track of multiple pieces of information in linear order in a one-dimensional list.

So far, the only arrays that we have considered are one-dimensional, with a single line of elements. Now let’s assume that we have five students. We can either use five different variables where we will store the first names of the students or we can use an array and a one-dimensional array. So we can either use the variable student one to assign the value John to this variable, or we can assign the value Nick to the second variable, the value Chris to the third variable, the value Helen to the fourth variable, and the value Maria to the fifth variable. Or we can use a one-dimensional array, telling students that it can store five values. So here is actually what we call the “index” of each of its values. We have index one, index two, and index three. actually the position inside the array. As a result, we can say that the value of John is assigned to the first position. In the second position, its value is Nick; in the third position, Chris; in the fourth position, Vayo Helen; and in the fifth position,

Maria. So actually, let’s assume we have two classes. We don’t have just one class, as we saw there; we don’t have just one class with five students, ten students, or fifty students; we have two classes, and we’d like to store the students’ names in both. But now I would like to know if the student is in the first class or the second class. So I can either use two one-dimensional arrays or use one two-dimensional array. So, for example, I can say that I have two-dimensional arrays right there. The first one is student class one; the second one is student class two. And in the first and in the first position, I have value in John; in the second, value in Nick; and value in Chris in the fourth. But these positions refer to the first class, while in the second class, in the second array, I have again position one, which I say values Mario, and position two, which I say values Nandia. Position three, which I believe is important for John And again, as you can see, I have five students right there. On the other hand, I can only use one dimension; I’m sorry. one two-dimensional array. one two-dimensional array. So that means that the first index refers to classes, and the second index refers to students. So I will say that in the first class, the first student is named John. As you can see, in the first class, the first student is named John.

Again, the second student, but in the first class, is named Nick. And in the first class, the third student is named Chris. The fourth student in the first class is named Ellen. The fifth student in the first class is named Maria. And then I say that in the second class, the first student is named Mario. The second student is called Nandia. And, as you can see, they both have the same value. But instead of using two one-dimensional arrays, I use one two-dimensional array. So let’s go on and assume we have two classes in two different schools, so I can use 1D arrays so that I can say that in school one. As you can see, I have five students in first class, all with different names: John, Nick, Grace, Helen, and Maria. Five students are in the first class. In the first school, I have a second class, that is the first name. The first student is named Mario; the second, Nandi; the third, John; the fourth, Zione; and the fifth, Michael.

But in the second school, as you can see, I have two different classes with five students in each one, and each student has a different name, of course. So I can either use one-dimensional arrays or two-dimensional arrays. For example, I can have two students, as we saw in the previous slide, so that there are all the students from school one and all the students from school two in exactly the same way. I can say the first two adjectives are school and one student, and then say the first index is for class and the second index is for student. So I can have two-dimensional arrays or I can have one three-dimensional array so that the first index refers to the school, the second index refers to the class, and the third index refers to the students. So I’m telling you that the first student, the first student from the first class at the first school, is named John. Nick is the name of the second student in the first class at the first school. The fifth student from the first class at that school is called Maria the Fourth. The first class in the first school is called Mario. The second student from the second class at the first school is called Nandia.

The fifth student from the second class at the first school is called Michael. And then I go to the second school, and I say again that in the second school, the first class, the first student is actually called Mike. In the second school, in the first class, the second student is called Dino. And we can go on until the final student. That is the fifth student in the second class in the second school, who is called Gym. And in the second school, the indexes are two, two, and five. So we have two in the second class, so we have two. The fifth student, which is very important because this is actually the size of this 3D array, Always, the last value of the last position of its array refers to the size. It is also known as the length in other languages. the length or size of its array. So in this 3D array, the size of this array is actually—I will write it right there. I’m sorry for this. I’m sorry. in the red. Very well. I will write again. It’s two multiples of two multiples of five. So this tells us that I have, oh, 20 values stored. I’m sorry; I don’t know why my pen is not writing very well. I will configure it.

Okay? I have 20 values because the size of this array is two multiples of two and multiples of five. This means four times five, which means 20. However, the size of each array in these four 1D arrays is also five. This has a size of five, a size of five. The size is five. Because the last index is the last value, the last position of this RA is just five. I will go to the previous slide and tell you that in this 2D RA, if we assume that we have two classes and only one school is two multiples of five, which means ten, but the size is two multiples of five, this is how we symbolise this array. So I think that with this example, you understood the two-dimensional and the three-dimensional arrays, so that you can easily respond to my question about which is the right dimensional array that I can use. If I’m going to have ten students from four different classes, three different schools, two different cities, and five different countries, I’d like you to answer this question. How many students will I need? So which is the size of the array that you are going to use? And how many dimensions should this RA have? If I’m going to have these students from these classes, schools, cities, and countries think about it and respond in the next assignment. Thank you very much, and I look forward to seeing you in the next lesson.

  1. Multidimensional Arrays & Examples

Hey, guys, welcome back. Now let’s see some examples with 2D arrays with multiple dimension arrays. So, as we said, two-dimensional arrays are indexed by two entries, one for the row and one for the column. So, for example, as you can see, we have a rating for its movie. So we have two reviewers. The first index is actually for the reviewer, and the second index is for the movie. So we’re going to say that we’re naming the RA as a rating. The first index, which is actually the row in this table, refers to the reviewer. So the row refers to the reviewer, and the column refers to the movie. So we’re saying right there that the reviewer with the number zero.

Also, we should actually think that in table-based arrays, we start from the zero position. As a result, the first reviewer will have a zero index. So the first reviewer gave the first film a four, the second film a six, the third film a two, and the fourth film a three, but he actually has the index three with a five. The second reviewer has the index one. Sorry, the second actually rated the first movie with a seven, the second with a nine, the third with a four, and the fourth with an eight. So the third reviewer with index two rated the first movie with six, the second movie with nine, the third movie with three, and the fourth movie with seven.

Actually, there are a lot of similarities with 1D arrays, similarities that mean that each element in the 2D array must be the same type. This is critical, regardless of whether the primitive or object type is used. So subscripted variables can be used just like a variable. So we can say that the first reviewer rated the fourth movie with ten Indexes must be integers and can be a literal variable or expression. So indexes should be integers by default; this cannot change. We cannot say, for example, that the 2.5 movie is either the second movie or the third movie. So we can say that the indexes are always integers, but the values can be anything as variables. So we can say that the third movie was rated 3.4. For example, if any array element does not exist, I’m sorry for this mistake there.

The Java runtime, for example, will tell you that the RA index is out of bounds exception. So if you actually tell your programme that you will use an array that will have a size of five and you’re going to use a sixth element, this means that you are out of bounds, and you will get an exception that you are out of bounds in every language. This is an example for Java, but you will get almost the same methods for C, for Python, for Ruby, for Flutter, for every language. So in declaring 2D arrays, we declare a local variable representation that actually references a 2D array of integers. So I’m going to say that I will use a two-dimensional array that will store integer values, and I will name it rating. I also declare a field family that reference a 2D array of gift cards. So as you can see here, I have a 2D array of gift cards that I could say are of a certain type is gift cards.

I should say that at the beginning of my program, I can create a 2D array with three rows and four columns. This means four columns right there, and assign the new array’s reference to rating. As you can see, there is a shortcut for declaring and creating a 2D array right there. You can say that you will use a rating. It will be a two-dimensional array with three rows and four columns. So the size will be three multiples of four, and it will store twelve values. So let’s see an example. Let’s try to find the average rating by the reviewer in row two. So I will use a variable called sum that will store integer values, and I will initiate my variable with the value zero. So I’m going to use a sum, and I’m going to add up all of the values. So I will use an index right there. I name it “Cold” from a column once more; it’s an undefined variable because I’ll use it as an index. As you can see, I begin with zero and end with zero. Then I’ll say that if the column if variable call is less than three, then add one to coal and continue by adding this actually plus and equal means is equal to sum. I apologise for my letters, ratings, and so on. So I’m going to add to the value of some ratings from the second row and all the columns.

So I’m going to scan all the columns, but only from the second row. So the row will be two, and I’m going to change all the columns. So initially, the call is zero. So, for a rating of two, call zero. Here’s the value of six. I’m going to add six to the sum, and the sum was zero. So, with zero plus six, the new value of the variable sum is six. I’m going up the column; the call is zero. I will add one to zero, so the new value of call is now one. So a rating of one is actually a value of nine. I’m going to add the value nine to the value of the variable sum. The sum is six. Six plus nine makes 15. So I’m going up to 15 once more. The value of the variable call is one. I will add one because two is less than three. So I’m going back right here to the next command and rating two. Two is now three. Three. I will add three to the value of the variable sum that already has 15. So 15 plus three makes 18. I’m going up the variable call; here’s the value of two. I will add one more. Three is either less than or equal to three; three is the same. So it’s also true that it’s less than or equal to three. So I will add my rating to three. Rating two, three, that is the value seven. In the second row of the third column, I have the value seven. So I’ll multiply the variable sum by seven.

The previous value of the variable sum was 18 plus seven, which makes 25. So I’m going up. The value of the variable call is three. I will add one, and it will go to four. However, I can see that four is not less than or equal to three. So I will go off this loop, I will go next to this command, and I will actually declare a new variable as double. And then I will say that the final value of the variable sum divided by 425 divided by 4 makes about four point something, and four point something is the final value in the double format. This means that in the double format, that will be stored in the final variable average. So actually, as we said, the size of the RA or its length is the final value of its RA. So a 2D RA is a 1D array of references to a 1D array. So we can say that. As you can see right there, if I have, for example, three schools, the first school, the second school, and the third school, each school has a number of classes. This is what I would like you to remember. So that’s enough for two- and three-dimensional arrays. I think that you could understand the concept in order to use it in our examples. So I’m looking forward to seeing you in the next lesson.

  1. Functions

Hey guys, welcome back. Now let’s see what a function is. So every programming language lets you create some blocks of code that, when called, perform specific tasks. Imagine a dog that does the same trick when asked. Except you do not need a dog to be treated to make your code perform in a specific way. In programming, these code blocks are called functions. So all programming functions have input and output. So we can say that it has specific input in its programming function, and I apologise once more for my letters. That can result in a specific output. So all programming languages, sorry, all programming functions, have specific input and output. The function contains instructions used to create the output from the specific input. It’s similar to drinking milk: the grass is the input, which its body converts to milk with the help of a dairy farmer, and the milk is the output. So, for example, programming functions might take input as an integer or number.

The function might create output by multiplying the input times two. So therefore, the output of the function will be double its input. So imagine this dog that we would like to make a trick with, and it’s time that we call this function right there to create a trick right there. The dog then performs the same trick each time. So I call the function “make trick,” and the dog “makes the jump.” So imagine a short “hello” message you sometimes see in online software applications at the top right corner of the end page: “hello” and your name. The function would take as input your name or maybe your user number from the application, and then it would drop your name with your user number into the face-hello your name.So if your name is, for example, Mike, the output of this function would be “hello, Mike” or “hello, Chris,” if your name is Chris. So the question is, can entire software applications be built solely with functions? Programming languages that primarily use functions are called functional programming languages, and most of them are primarily functional languages. You build software by assembling blocks of code that perform specific tasks. This is the logic. So what is a function? A function is a block of organised, reusable code that is used to perform a single related action. So I’m sorry; it is very important that a function be a block of organised and reusable data.

The key word here that you should remember is “reusable code.” Whenever we would like to reuse our code more than once, we should create a function that we will call to execute exactly the same commands, the same code, each time that we are going to call this function. So functions provide better modularity for your application and a high degree of code reduction. The most basic functions are actually the print function that we would like to print something on the screen or the main function that, in many languages like Java and C++, begins to execute the commands from the main function. These functions are referred to as “built-in” because they are provided by the language itself. So we can write our own functions as well. And in this course, we will see how to write and use these functions in the programming language. One of the advantages of functions is that they have many famous names. Different programming languages name them differently. As a result, you could listen to them or hear them through functions, methods, subroutines, or procedures. If you come across any such terminology, then just imagine the same concept that we’re going to discuss in this lesson. I repeat, you might also hear it as functions or methods, or as subroutines or procedures. So it’s almost the same as the functions we’ll be discussing. So talking about functions is a great time to talk about how they perform with specific inputs and specific outputs.

As you can see, we have a HelloWorld function that, when called, produces a specific Hello World output that is printed on our screen. So in this hello world example, our test is very simple, and we should confirm that the function outputs the phrase “hello work” and “hello world.” I’m sorry about the way we designed the function. So as noted, we could design our helloworld function to display the phrase by actually having three different ways we can call the function, and the function would display the phrase. So, whether you call the first or second function, it returns a value that can be assigned to a variable, such as the hello variable. Actually, to show you that this is a variable, I used the dollar sign in front of the word “hello.” But this is not something very common in a lot of languages. You might see it in languages like PHP. By creating a global value, we could assign it to a variable like “hello,” which is the third one. So by creating a global variable, we could assign it to a variable like “hello.” We say here that we could create and use a global variable because a global variable means that it could be assessed everywhere inside our program. So whatever method we might choose, our unit test would confirm our function performed as we intended because all three ways are correct and we can use them to display the phrase Hello World. So what is a function, at last?

Let’s see an example; let’s start with an example; let’s start with an algorithm where we will actually define two arrays of numbers and then, from their array, try to find the biggest number, the max. So given these steps, we are going to find the maximum number from a given set of numbers. As you can see, the algorithm, or recipe for success, is actually a list of numbers. So here is the list of numbers from L1 to Ln. So I’ve got n numbers. We assume that L1, the first number, is the largest one. So we set the maximum at one. We take the number li by 1, meaning all the next ones from the list, and do the following: If max is less than li, then li is a bigger number than what we set as maximum. We set maximum as lie there. So if a lie is the last number from the list, then print the value stored in the maximum and come out. So the 8th step is here; I’m sorry for this. We would repeat the same process starting from step three.

So if it’s the last number they lie about, we should stop and print the maximum. Otherwise, we will continue with the next number on our list. So this is the function; this is the algorithm in a more codeable way, in something that is like a programme in C, for example, or in C sharp. What we forgot is that we should have brackets because we have a lot of commands in our function. So, as you can see right here, there are the brackets. I’m not very good at designing the brackets. The first command, in fact, is to create the function for only three numbers. So we’re going to get the maximum value from three different variables: variable A, variable B, and variable C. But this doesn’t mean that we can use only three numbers because these are the inputs from the specific function, which can be called as many times as we might want to execute these commands. So if I have, for example, four, five, or six, I can also use this function with different input. And each time that I call this function, I will get the maximum number; I will get the result that I want for the numbers that are inside this function. So I declare the max variable as an integer and assign the value of the variable A to the variable max. If B is bigger than max, then the value of the variable max is going up. I’m sorry, the value of the variable B is assigned to the value of the variable max. If C is bigger than max, then we do exactly the same.

We assign the value of the variable C to the maximum, and then we are going to return the maximum value. So, if we try to define a function, we will say, like in this example, that the final value that is returned is the value of the variable max, and the variable max has an integer value. So the function find max will return, and this is how we declare it in our program: it will return an integer number. an integer number. So we define a function by the return type, as you can see right there, the function name, and the parameter list. So we have all the parameters right there. Then we have the commands executed in this function. And then we have the return and the expression, of course, of what we’re going to return. But don’t forget that this return would be compatible with the return type. If the return type is the integer, then we should return. An integer. If it’s a string, we should return a string. If it is a double, we must return a double or an exception will be thrown. Error. So the general form of a function is that it consists of a function. Header This is actually the function header right there. As you can see again, I’m sorry for my letters.

This is the function body, so type there turn right here. function may return a value. The return type is the data type of the value the function returns; some functions perform the desired operations without returning a value. In the case that the return type is not “returning something,” then the return type is the keyword void. So if it’s nothing, then we should say that it’s a void function name. Void. Not an int, double, or string. But we write “void.” So then we write the function name.

This is the actual name of the function. The function name and the parameter list together constitute the function signature. The Sheller function In the parameter list, a parameter is like a placeholder. This means that when a function is invoked, you pass a value as a parameter. This value is referred to as the “actual argument.” as the actual parameter. The parameter list refers to the type, the order, and the number of parameters of a function. Parameters are optional. This is a function that may contain no parameters. So then we have the functional body. And the function body is made up of statements that define what the function does. So this is the code. This is how we can use it in different programming languages.

And here we can see an example of calling a function. You can see that I have a function right there. I’m sorry for this. We have a function right there. And here is the main function of the program. So this is an example in C, but it is very similar in almost all the programming languages. So this is an example of calling a function. But you will understand how to use or read functions in almost every code. So we have a function called “get max” that returns a maximum number, and then we have a main function that, as you can see, calls “get max” with “set one” right here. So then we are again using a function, a built-in function called printf, so that we can print this message on our screen. Max is present at D in the first set. means that we will print an integer.

Actually, we will print the value of the variable max. Then we say that the value will be set to equal percent at d max. and then we’ll return zero. This is actually wrong. We’ll return zero and a question mark, and I’m sorry, but this means that even the main function will wait for something to be returned. And we will return at the end of the programme to the zero number. So this is an example of how we can write code to call the getmax function. And here is the getmax function, where the input is an integer array of numbers, a set of numbers, as you can see. And this function returns the maximum number of these numbers. So we omit all the code here. But I would like to show you how this programme is structured. So I think with this programme you can understand how functions are being used inside our programmes and be ready to write your first programmes in a functional way. So, I’m looking forward to hearing you in the next lesson. You.