98-381 Microsoft Introduction to Python – Arrays Part 2

  1. Function in Python

Hey guys, welcome back. Now in this lesson, we will see the functions in Python. A function is a block of organized, reusable code that is used to perform a single, specific action. So functions provide better modularity for our applications, and this allows us to use a high degree of code reuse. So, as you already know, Python has a lot of built-in functions.

For example, we can print, as we saw in the previous examples. So these functions are called “user-defined functions,” which we will see today. So we can create our own functions because it’s very, very simple. First and foremost, we will define our function at the beginning of our programme so that we can use it later in our program.

So I will write the keyword “def.” That means that I would like to define a new function. A function block begins with the keyword define followed by the function name. As a result, I’ll refer to this function as the first function used. You can use any name that you would like to use, and then I will give my parameters. So, for example, I will say that I will read the string. I will read the string, and then I will say that this function will print hello plus the string provided.

So this is the function documentation, which tells us what this function will do after it is executed. So then I’m going to use the commands that will be executed each time this function is called. So I will say, “Print I’m sorry, print hello, plus STR.” I will also leave an empty space right here. Print “Hello, SDR return.” So now I’m going to the end of my program, and I’m going to call this function; actually, I will tell you this is a comment we’ll call the first function used to print a string.

So I will use the name of my function hello. I will write world, and then I will use the same function to write hello to all of you. So it’s time to say hello. Plus, for this string, I will use the first function used, name. I can also change the name to something easier, like hello. So I will also change this name to “hello.” And then I will change it here in my comments to “hello.” So I will right-click and run the Python file in the terminal. Okay, I’m making the same mistake that I did in the previous lessons. Now it is okay, as you can see. So I will right-click and run the Python filing terminal. This is from one of my previous examples. As you can see, I say “Hello, world!” and “Hello to everyone!” The phrase “hello, beautiful world” is from this example. So the last two sentences are from my job.

So, as you have already seen, defining a function only gives it a name, specifies the parameters that are to be included in the function, and structures the blocks of code. Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. So following this example, you can call this hello function and print whatever you might want with the hello phrase.

So, as you can see, we can also use functions and pass by reference specific values. So all parameters in the Python language are passed by reference. This means that if you would like to change what a parameter refers to within a function, this change also reflects back in the following function. So we will use another function. I will go again in the beginning. I will use defunct ion with a list right here, and we will change the past list into this function. So I’ll write my list dot append and add 1510 and 15, then print value inside them function, print my list, and then return. So I will call the “change it” function. I’ll write “1020 30” right here on my list, then apologize, and then write “change it on my list,” and I’ll print the values outside of what is now my list. So, as you can see right now, the new values of the list, my list will be, and I will right-click and then run Python filing terminal. Again, in this example, I will write 0. And as you can see, I have four numbers inside this list.

Hey guys, as you can see right here, this changed word is actually underlined. So, as Python says, we will change the past list into this function. So the pass list is going to be changed. But as you can see here, I would like to repeat that with the dev keywords when we define the function. We will say what the name of our function will be, and then inside the parenthesis we will insert the arguments that are going to be used inside our function. So in this changed function, I insert a list, while here I insert a string, and in this list, I append these numbers.

So finally, with the print building function, I print this string, and then I print my list. What I would like to mention is the return statement. The statement actually returns from both exits and functions. So passing back an expression to the caller is optional, and this return statement with no arguments is equivalent to returning none. So all the above examples are not returning any value, but we can return a value from a function, as we can do in the following example. For example, we can define a function called sum that will add two numbers, NUM one and NUM two. Number one, number two. I’m going to say that I’m going to add two numbers and return the sum, so I’m going to use the variable total. Yes, I’m going to use the variable total, and I will write NUM one plus NUM two. Then print with some ease, and then print total. I will leave an empty space right here. The total will then be printed. And then, I’m sorry, I’m going to return the total.

So I’m going to use this building function now and write total equals sum. And I will write the two numbers, one and two. What is really good here is that when I’m writing the sum, you can see that now there is something like a command called sum as the name of my function. And if I open the parenthesis, then you can see that I have to add two numbers and return the sum of number one and number two. So I will write 10 and 20, and I will write “print.” I’m printing once more, and the total of the two numbers is 40. So right-click “run Python file terminal.” Okay, again, here I will type “0.” The sum is 30. And then I’m reading again, and the sum of the two numbers is 30.

But what I can do is write again, use print sum, and write here 400 and 5450, as well as these two numbers. So I’m going to print the sum of these two numbers now. Run the Python file terminal right here. So I get 1653. So, as you can see right here, each variable has a specific scope. So in this function, I use two variables and a total variable. However, this is outside the scope of my program’s function. And I am using again another variable called “total,” which is accessible to my program. And I am adding and giving the value of the return statement of this function. So this total, this value of this variable, is being printed right here. Then I print the function’s return statement again. So I think these were good examples to understand the functions in Python. I look forward to seeing you in the next lesson.

  1. File Input/ Output

Hey guys, welcome back. So one other thing that is very important that you should learn about programming is file input and file output. What does this really mean? This means that we can either enter data from our keyboard and see the results on our screen, or we can use files to read data from them, process them, and finally print the data to our files. So actually, we can use files as inputs to our programme and as outputs from our program. So, what exactly are files?

How we can define Files on a computer are used to store data in digital format for any content that we might like in digital format, like image data, plain text, or anything else. As a result, computer files can be organised into different folders and directories. Files are used to keep digital data, and directories are used to keep these files. Computer files can be considered the digital counterpart of paper documents. It’s exactly the same thing, but digital. So, in programming, we keep our source code in text files, and the only difference between the text files we use as input, output, and source code is the different extensions. For example, in the C programming language, the extension is C; in C plus plus, it is C plus plus; in Java, it is Java; and in Python, it is pi. Of course, there are a lot of other languages with several extensions, so the extension is used to define the purpose of our programming files.

So let’s see some more things about file input and file output. In reality, we usually create files with Word, Excel, PowerPoint, or even a notepad. Although we need these editors to create files and create content for these files, we can also use computer programmers to write content into these files. So we can modify an existing file using a computer program, and we can use the actual file to read or write data. So file input means writing something into a file, and reading something from a file is file output. You should think that we have a program, and the programme gets the output, and into the programme there is an input. Sorry for my letters. So this input can be either file-based or not, keyboard-based, or something else. So we have an input and an output. So file input means that data is written in a file; file output means that data is read from a file; and input and output terms are actually more closely related to screen input and output. Screen means that we display a result on the screen. It is called output. We provide some input to our programme from the command prompt on the keyboard. This is called input. So it is enough to remember for now that writing in the file is file input and reading something from it is file output. So let’s see some modes before we start.

We work with any file using a computer program. We need to create a file if it does not exist, or we should define which file we should open to read the data, the content, from it. So in each case, we can open a specific file with specific modes. So the first one is the read-only mode. As can be seen, this is the first one that says “Only Mode.” This means that if you’re only going to read an existing file and don’t want to add any new content to it, you can open it in Read Only mode. So almost all the programming languages provide specific syntax so that we can open a specific file in read-only mode. We can only read in read-only mode; we cannot write. If we try to write to a file that has already been opened in read-only mode, an error will occur. The second one is the “Write Only” mode, and you can easily understand that we just write in this file. So we can either write into an existing file or create a new file if there is not one already created. So you cannot read any written content from that file. If you open this file in write-only mode, you can only write, and if there is content already written, then this content is overwritten.

So be careful. You can use this “Write Only” mode if you would like to create a new file and write specific content in it. We can see later that there is the “Append” mode if you would like to write more data in a specific file that already has data in it. The third mode is the Read and Write mode, which means you will read and write into the same file. So you can open the file in Read and Write Mode if you would like to read and write at the same time. The fourth and very important mode that you should learn and use is the append mode. You can open a file for writing. It allows you to start writing from the beginning of the file. It will override any existing content, if any. So this is what happens in write mode. Suppose we don’t want to override any existing content.

If you don’t want to override any existing content, then open this file in append mode and don’t override. This means appending mode. So a pen mode is a write mode that allows content to be appended to the end of the file. And all the programming languages provide specific syntax to open files in append mode or to open files in read mode. read-only mode. Write-only mode or in read-and-write mode So now we will see how to open a new file, how to write into it, and how to read and append more content to the same file. So let’s go on and see specific modes that we can use to read and write data. So, as you can see here, we can use, for example, Cand C plus, or we can use the built-in function F Open. I will write it right there. In this F Open, we define the mode that we would like to use in order to handle the file. And we should also define which is the file, what is the name of the file, where it is located, and, of course, as I said to you, we should define in what mode the programme should handle this file.

So the first mode is the read-only mode. As you can see, we have the R, which indicates that we are opening an existing text file for reading purposes only. With the W, we open a text file for writing. If the file does not exist, then a new file is created, and the programme will start writing content from the beginning of the file. If this file has content, the content will be erased and overwritten. However, if you would like to open a file for writing in appending mode, then we will use the A mode.

This means that if the file does not exist, a new one will be created, but the programme will start appending content if we have existing file content in an odd file. So the fourth one is the Rplus. This R+ means that we can open a text file for reading and writing. So with R Plus, we can use a specific file for reading and writing. W plus almost means the same thing as when we open a text file for both reading and writing. It first converts the file to zero length if it exists, so it overrides all the content; otherwise, it creates the file if it does not exist.

So in the first case, we start reading the file and then we start writing in this file, while in the second one, we first set the file to zero and then delete everything we can write in this file. And then we can use this file to read the content that we wrote before. And finally, we have here the letter A, where the A stands for appending mode, as you can see. So this opens a text file for reading and writing. It creates a file if it does not exist, and the reading will start from the beginning. But the important thing here is that writing can only be appended.

This is the important here’s you can use A+ to use a text file for reading and writing. The reading will start at the beginning, while the writing can only be appended. So let’s see an example. In writing a file in C++, let’s see some code. As you can see right here in C++, we have the default library input/output header that is included in almost every programme and has all the basic functionality for handling the most basic built-in functions. Input Output So then we use our main function for our main program, and we define a new variable, a new file variable. The type of the variable is a type of file called FP.

And then we say that we would like to open the file that is stored in this location in the temp directory. And the file is named “test.txt.” The mode that I would like you to use is W Plus. This means that for both writing and reading, I would like to print. This is test number one. Where in FP does this mean in this file with WPLUS mode? So everything is okay. This means that you can print this line to the programme, change the line with a slash, and then print the second line. This is test number two, changing a line in the same file. But as you can see here, I use the built-in function f printf, while here I use the built-in function f put SF file, put swing f printfile, and then I close the file with the FC. Here I have “f open,” here I have “fclo,” and I say which file I would like the programme to close.

When this code is compiled and executed, it creates a new file test TXT in the temporary directory TMP and writes two lines using two different functions, the built-in functions f printf and put s. The first line that will be printed means this isest number one, as you can see right here. Then we change lines, and the second line is, “This is test number two.” And again, we change lines. So this is a simple example in C++; let’s see the same example in Java. Again, you can see that we have imported into Java a specific input/output library. As you can see right here, input-output Java actually provides an even richer set of functions to handle file input and output. So in this simple Java program, which is actually the same as the previous one, we will open a text file. As you can see, this is actually a programme called demo Java. We again have a main function. This is almost the same for a simple input-output program. And we have a new file in the temp directory. The file is called “j test TXT.” We create a new file.

Again, we use Java’s built-in faction to create a new file with the name we specified in this variable file. And in Java, we need a file writer object that uses the file object to start writing. So we define a writer, which is a new file writer, and we say that this is a file that you would like to handle to write in this file, and then we start writing. We say writer write and then the methods again, then we change line a second time, and then we clean the memory and close the file. We clean the memory with this one with Write Flash, and we close the file with Write Flash close.

As you can see, we don’t define specific modes in Java; I simply use to create a file, and then I define a specific object, a file writer or file reader, to write or read data in that file. So this will give us the output in this file. In this J test text file, the first line will be “This is the test,” as you can see; I will change lines, then print “This is the second line of the test file,” and then change lines again. And finally, let’s see exactly the same functionality in Python. So I would like to create a new file, and then, as you can see, I define a variable called foe, which is the name of the file in the directory that I would like to use. The W means that it is in the right mode. I would like to use this file and use the building function write to write in this file. This is the test, and then change lines and write again in this file.

This is the second line of the test file, and then close the file. And here it is actually for writing the first one, while here is the example that I hadn’t seen before for reading. So here I define again which file I would like to use, and I store it in the variable Fo.

This is the file I would like to open in read mode, read the content of the file, and store it in the variable STR. So we read the content of the file. We print STR and then close the open file. So in the first line, we have, “This is the test.” In the second line, we have the second line of the test file. So I think that with these two examples, with three examples, I’m sorry, in the most popular languages like Python, Java, and C++, you understood the value of file handling, and at the same time you understood that this is something very simple and very valuable. Because you can use files to store memory that you can reuse later as an input to the same programme or another program, So you can use files to permanently store specific data output information that can be reused in the future as specific input.

So, I think that you have already understood that alot of magic things can happen in programming and coding. Actually, you can do everything that you can imagine digitally with coding and programming. So you can create files, delete files, modify files, and create almost anything that you can imagine. You should remember that even viruses are programs. Even viruses that harm your computer are programs. So what do most viruses do? Most viruses delete content, including vital content, from your operating system. And this content is, most of the time, specific vital files that store variable information about your operating system and your computer. So these viruses delete these files and do all of these bad things in your computers, whereas antivirus software recreates these files to restore your computer’s functionality. So, as you can understand, this is something really important to know about how you can create, modify, and append content to files. So it’s something really valuable to understand and be able to use in coding. I’m looking forward to seeing you in the next lesson.

  1. Input/ Output and Files in Python

Hey guys, welcome back. Now in this lesson, we will see all the basic input and output functions that are available in Python. And these functions include files, input, and output. So let’s see the first command, which is actually the print command. The print command is the simplest way to produce output using the built-in print function, where we pass the string that we would like to be printed on the screen. So the examples from previous lessons produced all of these outputs in the terminal on our screen. So we can also use keyboard input.

So Python also provides a built-in function to read a line of text from the keyboard from style input, which by default comes from the keyboard. So let’s try to use the built input function. So the built-in input function reads one line from standard input and returns it as a string. So let’s try to create an STR input here. Enter something here with your keyboard. Okay, I will change enter to type something here with your keyboard and then print what you typed, STR. So let’s try to right-click and run the Python file in the terminal. And as you can see, here is the message. Type something here with your keyboard. And then, if I type here “hello,” my name is Christos. You will see that you typed “hello.” My name is Christos. I could also use this one to make it more clear.

Sorry, not here. Hello. You typed “hello.” So this is a good example where you can see the input function that is used to read one line from style input and return it as a string. I could also use opening and closing files, so I can use the built-in open function, which is actually a basic function that includes all the basic methods necessary to manipulate files by default. So far, we’ve been reading and writing to the Stata input keyboard and screen, as well as the Stata output keyboard and screen.

And now we will use the Open function to use files. So actually, in order to use files, I can use the built-in function Open. So I’ll type “open my file TXT” and explain that I want to use this file for writing. So I will use the attribute w. So I would like to use some built-in functions that I will show you right here. For example, if I want to print the file name in the screen, I can use the built-in function f name printit, which is located in the namespace f dot. And here you can see all of the built-in functions, read about them, and decide whether or not to use them. The f close function is one of the built-in functions. That means that it will return whether it is closed or not. So it will either turn out to be true or false file.I’d like to use it again, whether it’s open or closed. So I will use the FO mode in order to see the opening mode. So this is a good example that I would like to run. In the terminal, run the Python file.

Okay, I have nine. As you can see, the name of the file is My File TXT. So it took the attribute name and printed my screen. It is not closed, so it is open. It is by default open, as I have opened it right here, and the mode is W; it’s not R; it’s for writing. So I can actually then close the file by using the command “I’m sorry, “close,” and then the file is closed. Now, in order to open it and read it from my computer So I will use the right method so that I can write something in my file. So I will tell my computer that I would like to save the file.

Create and save the file myfile.txt in the c directory; in the main directory, I use Windows. If you are using Linux or Mac, you can specify the directory in which you would like to save this file. And I’ll handwrite inside, “Hello, beautiful world.” So I will run it. Right-click and select Run Python Filing Terminal. Type something here with your keyboards. Again, this example above So, ten, you enter ten. Okay? So if I go to the C file and open my file TXT, you will see that “Hello, beautiful world” is printed inside my file. So, as you actually saw there, I can very easily write something to my file. So again, instead of writing, I can also use reading. So I can use STR = f read and specify the number of characters I want to read.

So I will type ten and then print STR on the screen. So let’s try to use it. In a terminal, run the Python file. Again, this example Okay, as you can see, it says that you have an error here. The input file is not readable. And that’s very correct because I said that I would use this file for writing purposes. So I will take this, cut it, and paste it below. I will take this line, copy it, and paste it, but I will change the mode to reading mode, and then I will close the file again. But I will also take this print because I want it right there. So this is the example.

Right click. In the terminal, run the Python file. This is an example again. And as you can see, it took and printed the first ten characters from the string. So we read a string from an open file using the built-in read method. It is important to note that Python strings can contain binary data in addition to text data. So we passed the parameter, the number of bytes to read from the open file. And this method starts reading from the beginning of the file. And if the count is missing, then it tries to read as much as possible, maybe until the end of the file. So if I’m going to delete this, it will read everything that is in this file. And as you can see, everything is stored in this string, which is printed. So reading data from a file and writing data to a file are two different things. Right here are two examples of reading data from a file keyboard. So these are the examples that you can use for a complete input-output experience.