Thursday, 6 August 2020

Python Complete Tutorial #3 ,Start with Terminal

Hey Guys, welcome to the another post of Python Complete Tutorial, So lets get started.

Terminal

So on Mac and Linux, this will be called the terminal. On Windows this is called the CMD which means Command or Command line. See unit two, search for CMD. Once you install Python, what you can simply do is type Python on the command line and press enter. It opens the Python interpreter. I can write Python code right here and it will work. This is valid Python code. It comes with a help function. If I type help followed by braces, it will launch help for me. If I type in keywords, right, so there are certain things called keywords in programming. Let me just zoom into it, yeah. So I can exit from Python. Look at the definition. Let's get started. I will show to you this is how you can open the Python interpreter, you just need to go on the particular page and type Python and it will open it up. Let's come to Pycharm for a minute. In Pycharm, the way it works is that of course, I already have this, but in case you would not have this. So let's just remove it all together. What you need to do is you need to create a new project and then you need to give it ard thing. Even if I write code I can bet my life on it that three days later I will forgotten about 80% of it. name. The name could be test and you do a create. We do open it in the current window. Once it's in the current window, what I can do is that I can go to new and then I can select Python file. I can give it a name. Automatically creates a test as pv extension by itself and it stores it. Even if you go into a system and you'll see that it has been created and you can start writing your Python code over here. Now programming has something called comments. A lot of time what happens is that you will write a piece of code but you will forget what it does. It's not something that only you go through. It's a standard.

Source: Python



I would know what it does, but if it's more than 100 lines I would have to read through it. Now what developers do for this situation is that they add comments. Comments are nothing but simple English explanations for what you have done so that you don't need to remind yourself again and again of what this particular thing does. It just helps to keep at rack of, it's like notes. It's like small notes and the programming languages basically ignore them. Now there are two ways to write comments. This is a new Python file for the class. Now that's it. This is not valid Python code. This is not code, this is English. This is one way to write it. Other way to write it is using three single quotes and you write it like this. This is a new Python file for the class. These are the two ways you can do it. The purpose is basically to just remind yourselves or to other developers. So this is one thing that a lot of people when they're starting off, your code will be viewed by other developers at some point. You will start sharing your code, either within a company or otherwise. So when they look at your code they should be able to understand from your  comments what it does. Not just the whole file, not  like a description that this file says something to the database, but like what each and every step is along the way. So like, for every 10, 20 lines of code, probably one comment should be there. That's like a best practice. So next is indentation. You need to think about this in the way that Python follows indentation like comment system on social network websites.

So if you've ever seen Reddit, let me just open it up actually, okay. So if you look at Reddit, if you go to the comment section, you will see that there's a nesting. You can know that okay, A hah is being responded to by Retarded Clown Face or this guy's being replied to by this but this guy's being replied to by this. This guy's being replied to by this guy, so on and so forth. It's a nesting. You can see that there is a division. You can see that this comment is a reply to this comment. This is something that we see everywhere. We see this on Facebook, we see this on Instagram, everywhere. Now, this is how Python sort of declares blocks of code. So in case you're not familiar with what blocks or closures are, it's okay. You're going to come across it when we go further in Python but just remember that indentation is a very important concept in Python and this is how closures or blocks of code are defined. So how does Python code execution happen? You write code and it is saved with a .py extension, like I just showed it to you.

The code is converted into Byte code for machine to understand. That's done by the Python compiler or the interpreter. You don't need to worry about this step. It happens automatically as you run the file. It gets executed. Just need to care about step one, two and three are taken care of by automatically. It's not something that you have to do much about. Let's created a first "Hello World" program in Python. So how do you write hello world, how do we greet the world? Well, this is how we do it, as simple as this. Print, open braces, then a string. This is a string. You can't write this, this will not work, okay. If you see, this is already showing an error and of course this is where Python is smart. Python is smart. If you notice, it is underlined this file. It has underlined the folder containing it. This is telling you, before you even run it, that this is not going to work. So anytime you're printing string, a string being anything which is like a message, right, it needs be in strings. Use the print statement, use the brace opening and the brace closing like this. If you miss the brace it won't work, you have to include it. Then let's run it. So the way to run it is you right click and then you click on run hello, where is it referenced to the name of the file. This is where it will run.

This terminal is already present. Hello world is printed two times. odd happens. This gets printed on two different lines. Why is that? We just wrote a single line. So this is where this comes in, \n. This is what it's called is break line character. It will break the line. If you're typing in a Word doc and you press enter it goes to the next line. It's a new line character. If I type it two times, we should expect two spaces. If you notice it is gone two spaces deep. If you do it another time, let's suppose you do it four times and you save and we run. It goes even further. The reason why it is not dividing in the same end is because there's a space character here. If I do it like then, then it'll start on the same line. Now see, it adjusted. So this is how you do it. So it could be single code to double codes, doesn't matter, but I would suggest following one of them. This is the output you will get after running the file. Next is variables, variables are pretty much like mathematics variables over here. You take a particular name that you want to give it. So you want to set A as 10,you want to set B as codes.

This is an integer in Python. This is a string. Notice that now we're using single string as compared to double quote. You can do print AB, A,B and notice the comma. So ever here for example, I could have done this and this would have worked. So print can separate multiple strings or things that you want to print via comma. So you see both of them get printed. Now let's come here and let's print this. The 10 and codes both get printed. Of course they're coming with these braces. So in case you want to just print it, print them individually, that's a separate thing. I mean, you will have to separate it.

But in case you just want to see the values on your terminal this is the way to do it. Now, another example over here is this one. So what this will do is that it will move in order. It will assign X to 10,y to 20, and z to 30. Let's try it out. Okay, now of course this is very convenient way of doing it. Now the alternate way of doing it, of course you can always do it like this as well. That it increases the vertical length of your file. I don't find it to be neat and you will see expedience of level of doing this way. It's just more convenient. It just comes in a single line, that's it. It's just a matter of convenience. Otherwise, this is also correct and this is also correct. Correctness is there, it's just that line number nine is better than doing it in three lines. This is more concise.

Identification of words.

Now, a word about identifiers. This is a line identifiers. When you hear the word identifier, A and B are identifiers. They're identifying a particular variable name for you. Now there are certain rules around it. It is used to identify to variable function, class, or any other object right. It starts with a letter A to Z or A to Z capital or small or an underscore and followed by digits, zero to nine. So you can't start a variable with a digit like this. It shows an error. What you can do it you can do this. Or you can do this. This works, but doing this won't work. So this, this, _B, __b, and all of this, they are valid identifiers. But you can't start it with anything else apart from letter A to Z, capital or small, underscore. You can't start with the digits. Python doesn't allow special symbols, like add, dollar, percentage, star, braces, exclamation mark with an identifier. Now there are certain naming conventions in Python. There is something called class in Python, it's a concept. Maybe some of you are familiar from other program languages. It starts with an upper case letter and all other identifiers start with a lowercase. So only the class name starts with an uppercase. Now there are certain other rules as well. They are related to classes. I think it would be better if we covered them then. All of these things like to identify this as private, strongly private, two trailing underscores. Now all of these are some concepts that we'll learn later. Let's leave them for that. But for now you just need to know that okay, this is how you name a variable in Python or an identifier. Now how are variables stored? There are reserved memory locations. What happens is that in the computer's memory, which is the RAM right, it creates a space and stores that variable in the memory and it reserves some bytes for it, that's it. That's all that's happening. It's just remembering them, storing them. So it's not that while I am writing it is doing that, it is when I'm running the file. So it is not going to do that right now as soon as I create it. So don't think that if you create too many variables your typing speed will slow down or your laptop will start hanging up. No, I mean it might do that, but when you're running the file and it might be for different reasons that just you know, simply having too many variables. So but just notice that this is just an instruction. This is just nothing but instructions. When you run it that's when it really does something on the computer. Right now you're just typing it like a Notepad document and you're pressing control S. But when I run it and by run I mean I go ahead and do this, that's when it goes to memory and it does all of these things. Yeah, so we have already covered this right, where we run it and the values get printed. Now let's look at some standard datatypes available in Python. Even before that, let's try to understand what is meant by datatype. Now, a datatype is basically a way to say that something is an integer, something is a string, something like what kind of data it is, whether it is a numeral or it is a string or it is a Boolean or something else or something else.

Datatype: Mutable and Immutable.

So Python has two kinds of different datatypes, or any program language for that. One is immutable and the other one is mutable. So the word mutable and immutable comes from, you need to think of mutation. The one that you might have studied in biology. Mutation fundamentally means change. So mutable datatype would say that it can be changed, that the data can be changed.
Immutable is that it cannot be changed. So lists, dictionaries, or sets, these are something that you're going to study. They can be changed, but immutable datatypes, numbers, strings, or tuples, they cannot be changed. Now one source of confusion that you might have is you might be thinking so, if I do a=121 like this and then I do a = 121,or let's suppose right, I do this and then I do a = 3, then this should not happen. Because a is an integer and you're changing it. I'm first setting it to a random value 121, and then I'm setting it to three. But that is not what it means. What it means is that, see this is just an identifier. This is not the data. The data is on the right side of this.

So when it says that it is immutable, what it means to say is that whatever you do, you cannot turn 121 into three. Which if you think about it makes sense, right? So this is the level of depth by the way, that happened in programming. Something as obvious that one is not equal to two or 121 is not equal to three, cannot be three. It is just that. But this is a different thing in the world and this is a different thing in the world. There is no way that it can convert this into this. Like you cannot convert gold into silver. Gold is immutable. Gold is immutable, where you cannot turn it into silver. You cannot turn it into any other element. But then there are certain things in the world which are mutable, that you can take them and turn them from one thing to another thing. So for example, you can take a seed and turn it into a tree. That's mutation. But if you're trying to mutate gold into a tree or gold into silver, it will not happen. It will remain gold, even if you mix impurities in it.



It will be impure gold, but it will still be gold. So you need to think of it like that. It's a very basic concept but it's something which is there across programming, that the entity cannot change. Now let's talk about the immutable datatypes, numbers, string and tuples. No Python supports three different kinds of numeric values, integers, which are signs. So you can add a plus or a minus in front of that. There are floats, which are real numbers, decimals. Then there are complex numbers. When it comes to representing numbers, you can represent them in three different ways, binary, octal or hexadecimal. However, please note that you typically will not be using these representations. Typically, you will be using numbers like you use them normally. So to define an integer you set base of 10, to define a float base of10.65 and for complex numbers. In case you remember this or you're familiar with this, this is 10+6j. This is the imaginary value and will operate like this. Let's quickly have a look. We have the same file here, let's run it, and it outputs like 10+6j. If I do 10=5+4j and I print D-C, let's see what do I get.

So if you see, it does done the subtraction, 5-10 is minus five, 4-X is minus two and a get a result. Next is strings, a continuous set of characters represented within any quotation is called a string. So whether it's singular or double quotes. Now, Python does not support a character type. This is basically coming from other languages which have something called its char, as a character. Python doesn't have that. Python just has continuous strings. In case you're not familiar with character it is okay. But character datatype is no different to character in the generic way of alphabets. It is referring to it in the programming context, where there is something called a character, it's char, datatype, in Java, C++, so on and so forth. Python doesn't have it. Python just has strings. It cannot differentiate between single and double quotes or does not differentiate between single and double quotes. As you've already tried it, this works for us right. We have already done the print statements using strings. Next is tuple, so tuple is a fixed list. It comes within parentheses. For example, even this right now, the way it is printing, it's a tuple. If you look at this, this is a tuple. I can do this, now notice this. I can print this and this will run. Now I can also do the following with a tuple, I can print and individual element. I can print the zero index. These are zero index, meaning or let me change it, so let's suppose these are fours, right. Start off saying that this is the first and the second and the third. It works with a zero index. It says this is a zero, this is the first and this is the second. Now I can do this as well. So this is me reading an individual element of the tuple, four, five, six. However, notice if I try to do this. Python has already highlighted this. It has a problem with it. It you hover over it, it's just saying that tuples don't support item assignment, which is basically meaning that hey, you can't do this, you cannot change it. I cannot change this, let me run it. It was throwing an error. It's throwing an error tuple object does not support item assignment. So I'm trying to change this datatype. It's like I'm trying to change gold into silver and saying sorry, can't do that, that's not possible. You cannot modify this. Now let's look at the mutable datatypes. The first one is a list. So we just saw a tuple, it seemed like a list of items right but it was called a tuple.

But Python also has something called a list. Now, lists can contain various elements and number of elements if you want to create an empty list, this is how you create one. Square bracket, open and close, this is an empty list with zero things in it. This is a list with one, two, three, and four things in it. Now if you notice, all of these are different types of data. This is a decimal, this is an integer and this is a string. You can print the list and pretty much make a tuple you can assess any element within it. Let's run this. What you can also do is that you can update it. So we can say A is equal to changed first, second element. So because this is, again, this is zero indexed again, by the way. Like tuple was zero indexed, this is also zero indexed. Zero index being that this is referred to as a zero. It doesn't count it as one. So that is generally how things work in programming. Almost all programming languages have it this way. So if you see, I change this second element. So the same thing was throwing an error in the tuple, but in a list it is doing in this certain way. It is allowing me to modify it. Now a list can contain other lists as well. So you know, you can also do this. And you can also do, you know, four and then third element and it can also contain tuples. It can also contain a tuple inside it. This is a particularly complicated example of, not complicated, but something that you would normally encounter, but I'm just trying to showcase it to make a point. That a list can be of anything. So think about the English wordlist, it can be of anything. What I've done here is that I've said that zero element, which is this, this is a list, give me the second one. So it gives me four. If I say one and then Irun it, it'll give me two. I can run it on the first one as well. Yeah, it will give me four. If I need to access the tuple, think about which element it is, zero, one, two, three, right. I want to print five, then I'll do this. B3 to, I'm just counting. I'm counting zero, one, two, three. Okay, I got the tuple till this point. After that, I want to access five. It is zero, one, two, I go to two. Now slight differences, lists are putting square brackets, tuples are within parentheses. Lists are mutable, tuples are immutable. Tuples are faster than lists, letting that owing to the immutable nature of it, but that's not the real reason why you will use a tuple. You will use a tuple if you want to have a fixed value. So if let's suppose, you are making a simple calculator, you know which can do scientific calculations. Let's suppose by using the value of pi and scientific constants that can calculate the area of a circle or a sphere or a cone or whatever. It needs to use 3.14, the value of pi, and other constants and you need to maintain that and need to ensure that it is not modified accidentally by you or your programming. That is when you will use a tuple. Or you would use a tuple if you had to maintain a list of countries because you don't want the list of countries to get changed. You don't want, accidentally, when you're coding, you end up changing an element within that list because you know, it will cause a lot of problems. So basically, to understand why you need to also think about and understand why immutable datatypes are needed because if you look at lists and tuples, the only difference really here is-- So by the way, I can do this with a tuple as well. I can do the very same thing. I can simply do this, see, and I can run it. It will work as it is. It just did, I just printed the element, right? Now, the only difference is that lists are slower, but more importantly, lists are mutable. So mostly you will find yourself believing that (speech unclear) dealing with lists rather than tuples. Tuples are needed for very specific scenarios. So don't get confused by why do I have two, can I use either one of them. Focus on lists for now. Tuples will naturally come to you when you are presented with a scenario. The tuples are more in the niche, where they are part of a particular case. Next are dictionaries. So dictionaries, as the name suggests, it is like you would go through a dictionary right. What is the meaning of the word naive, what is the meaning of the word precarious, what is the meaning of the word apple, so on and so forth. Against every word you will have a meaning. So the word is like a key and the explanation is like a value. This is a key, age is a key, this is the value. This is the key, this is a value. If you look at is, this is very readable. They're separated by colons, okay, and the need to use this is that it makes your data very readable. Key value pairs is something that comes naturally. So if you look at a fill form, any sign up form that you have every filled, it asks for an email and a password. Imagine that being stored here. So in the sense, let's open this up, so this is being handled like this. Now, suppose you can also argue that hey, you know I can also store it like this, why shouldn't I store it like this? I'm gonna store it like this and instead of, you know, doing, oh by the way, so what you would have done over here is you would have used square brackets and then you would have typed the key. Let's run this once. Right, same output. So you can argue, hey, why wouldn't I use a list or a tuple, why would I use this? So one primary reason why you would not do this is because this is way more readable. Imagine how other lines of code, imagine this was declared on line one and you're using this on line 100, you don't know what is zero. You have to constantly refer to what is being stored at zero.

If you just, want to subscribe to my, newsletter so that you don't Miss my future tutorial Post just go down of this blog and subscribe with email to get all News and as always let, me know in comment below, when you thought of this Post I'm Ankit, from RajCoding, and i'll see you in the next Post? Bye! 


Previous Post
Next Post

0 Comments:

Hi , How can i help you.
If you have any DOUBT let me know.
🙏🙏🙏