Monday, 25 April 2022

A Bit More on Comments On Python Programming | CMD TO


 A Bit More on Comments On Python Programming | CMD TO

 There are a lot of things that you can do in Python. It is one of the most interactive options that you will run into when getting started in programming and since it is so easy to use. In this chapter, we will take some time to discuss more about comments and some of the other aspects of Python so you are able to get started and make your codes amazing in no time. In Python programming a comment is one that will start with the # sign and then will continue on until you get to the end of the line. For example: # This would be a comment print(“Hi, how are you?) This would tell the computer to just print “Hi, how are you?” All comments are ignored in the Python interpreter because it is more of a footnote in the program to help the programmer, or others who may use the code, special things about the code. They are basically there to say what the program is supposed to do and how it will work. It is a bit more detailed and can be helpful without getting in the way of how the code works. You will not need to leave a comment on every line, just when it is needed. If the programmer feels that something needs explained better, they would put in a comment but don’t expect to see it all over the place. Python doesn’t support any comments that will go across several lines so if you have a longer comment in the program, figure out how to split it up into different lines with the # sign in front of each part. Writing and Reading Some programs are going to show the text you want on the screen, or they can request certain information. You may want to start out the program code by telling the reader what your program is all about. Giving it a name or a title can make things easier so the other coder knows what is in the program and can pick the right one for them. The best way to get the right information to show up is show a string literal that will include the “print” function. For those who don’t know, string literals are basically lines of text that will be surrounded by some quotes, either a single or double quote. The type of quote that you use isn’t going to matter that much, but if you use one type in the beginning of the phrase, you should use it at the end. So if there are double quotes at the beginning of your phrase, make sure that you keep up with the double quotes at the end as well. When you want the computer to display a word or phrase on the screen, you would simply have “print” and then the phrase after it. For example, if you want to portray “Welcome!” you would do Print(“Welcome!”) This will make it so that “Welcome” shows up on your program for others to use. The print function is going to take up its own line so you will notice that after putting this in, the code will automatically place you on a new line. If you would like to have the visitor do a certain action, you can go with the same kind of idea. For example, say you want the person to input a specific number so that they can get through the code you would use the string: first_number = input(‘put the first number in’) When using the input feature, you won’t automatically see it print on a new line. The text will be placed right after the prompt. You will also need to convert the string into a number for the program to work. You don’t need to have a specific parameter for this either. If you do the following option with just the parentheses and nothing inside, you will get the same result and sometimes makes it easier. Files For the most part, you will use the print function to get a string to print to the screen. This is the default of the print function, but you can also use this same function as a good way to write something onto a file. A good example of this is With open(‘myfile.txt’, ‘w’) as myfile: Print(“Hello!”,file=myfile) Now this may look like a simple equation, but there is quite a bit that is going on in the string above that you should watch out for. In the spot with you opened up the myfile.txt to write on and then assigned it to the variable called myfile. Then in the second part, you wrote in Hello! To the file as a new line and then the w told the program that you will only be able to write the changes when the file is open. Of course, you don’t have to use the print function to get it to do the work that you want. The write method will often work well too. For example, you can replace the print with write like the example below to get the same things. With open(‘myfile.txt’, ‘w’) as myfile: myfile.write(“Hello!”) So far we have learned how to print a string of words into the program and even how to save them to a specific file. In addition to those options, you can use the read method in order to open a specific file and then to read the data that is there. If you would like to open and read a specific file, use this option: With open(‘myfile.txt’, ‘r’) as myfile: data = myfile.read() with this option, you will be able to tell the program to read the files contents into variable data. This can make it easier to open up the programs that you would like to read. Built In Types Your computer is capable of processing a lot of information including numbers and characters. The types of information that the Python program will use are known as types and the language will contain many different types to help make things easier. Some of these include string, integers, and floating point numbers. Programmers can even define these different types using classes. Types will consist of two separate parts. The first part is a domain that will contain a possible set of values and the second part is a set that contains the possible operations. Both of these can be performed on any value. An example of this is that if you have a domain that is a type of integer, it can only contain integers inside it including addition, division, multiplication, and subtraction. One thing to note with this is that Python is a dynamically typed program. This means that there really isn’t a need to specify the types for the variables when you create it. The same variables can be used to store the values of different types. Despite this, Python still needs you to have all the variables with a definitive type. For example, if the programmer tried to add in a number to a string, the Python program would recognize this and show an error. It won’t try to figure out what you wanted; rather it will just exit without trying. Integers If you want to use integers as a type, you need to keep them as whole numbers. These can be positive or negative numbers, as long as there are no decimals with these numbers. If you have a decimal point in the number, even if the number is 1.0, you will need to use it as a floating point number instead. Python is able to display these integers in the “print” function, but only if it is the sole argument. Print(3) # Let’s add two numbers together Print(1+2) If you are using integers, you will not be able to place the two right next to each other. This is mainly because of how Python is a strongly typed language and won’t recognize them if you combine them together. If you would like to put the number and the string together, you need to make sure that the number has turned into a string. Operator Precedence One thing that you need to keep track of when you are working in Python is operator precedence. For example, if you have 1+2//3 Python could interpret it as (1+2)//3 or 1+(2//3). Python has a method that will help you to order the operation properly so that you get the right information to come up. For example, when it comes to integer operation, Python is going to handle everything that is brackets first. Then it will handle the things that have**, then *, and then //, then %, +, and finally -. If you are writing an expression that has a number of operations in it, you will need to keep those signs in mind. This will tell Python how to go through the numbers so that you can get the right answers at the time. Keep in mind that most arithmetic operators are going to be left associative so write it out that way for Python to read. The only exception is the ** feature. For example: # ** is right-associative 2**3**4 # will be evaluated right to left: 2**(3**4) Strings While a string may seem like something complicated, in Python they are basically a sequence of characters. They are going to work the same way as a list does, but they will contain a bit more functionality that is specific to the text. Formatting strings can be a challenge when it comes to writing out your out your code. There are some messages that aren’t going to be fixed string and sometimes there are values that are stored inside variables inside it. There is a way to get this to work right for string formatting. An example of this is: Name = “Janet” Age = 24 Print(“Hello! My name is %s.” % name) Print(“Hello! My name is %s and I am %d years old.” % (name, age)) The symbols that have a % first are called placeholders. The variables that go into these positions will be placed after the % in the order where they are placed in the string. If you are doing just a single string, you will not need a wrapper, but if you do have more than one of these, you need to place them into a tuple, with a () enclosing it. The placeholder symbols will start with different letters, depending mostly on the variable type you are using. For example, the age is going to be an integer by the name is a string. All of these variables are going to be converted into the string before you can add them into the rest. Escape Sequences Escape sequences can be used as a way to denote special characters that can be hard to type on your keyboard. In addition, they can be used to denote characters that can be reserved for something else. For example, using and in the sequence can confuse the program so you may use the escape sequence to replace that like the following example: Print(“This is a line. \nThis is another line.”) Triple Quotes We have spent a bit of time talking about both single and double quotes, but there are times when you may need to bring in the triple quote. This is used when you need to define a literal that will span many lines or one that already has a lot of quotes in it. To do this, just use a single and double together or three singles. The same rule applies with the triple quote as with all the others. You will need to star and end the phrase with the same one. String Operations One of the string operations that you may use a lot is a concatenation. This is used in order to join a pair of strings together and you will notice it is there with the + symbol. There are a lot of functions that Python is able to help you with and they will work with the strings to create a variety of operations. They are going to have some useful options that can do a lot more in the Pythons program In Python program, strings are called immutable. This means that once you create the string, it is not capable of being changed. You may have to assign a new valuable to a specific variable that exists if you are looking to make some changes. There is so much that you are able to learn about when it comes to getting started with Python. It may be a simple language, but you want to be able to learn how it works, how to write things down properly, and even how to leave a comment for others to understand when they are looking through the code. It may seem a bit intimidating in the beginning, but before too long, and with some practice, you will get it down and be writing your own code in no time.

No comments:

Post a Comment