Monday, 25 April 2022

Some of the Basic Commands, Variables, Statements, and Other Things That You Can Do with Python | CMD TO

 Some of the Basic Commands, Variables, Statements, and Other Things That You Can Do with Python

 There are so many things that you are able to do in order to get a code up and running on Python. Many people may avoid using Python because they think that it is too simple or it just isn’t going to get the job done. But in reality, it is simple just for the fact that even a beginner is able to learn how to use it, but that doesn’t mean that you aren’t able to do a lot with it. This chapter is going to take some time to look at the different commands that you can do with Python programming in order to make your programs and codes come to life. Variables Variables may sound like something that is too complicated to learn, but they are basically locations in the memory that are reserved in order to store the values of your code. When you work on creating a variable, you are reserving this spot in the memory. In some cases, the data type that is in the variable will tell the interpreter to save the memory space and can even decide what you are able to store on your reserved memory. Assigning values to your variables The value is going to be one of the basic things that your program will need to work with. it can be a string, such as Hi World, 3.14, which is considered a type float, or a whole number like 1, 2, 3 which is considered an integer. Python variables will not need an explicit declaration in order to reserve the space in the memory that you need. This is something that is going to happen automatically whenever you place a value with the variable. For this to work, simply place the (=) so that the value knows where it is supposed to go. Some examples of this include: X = 10 #an integer assignment Pi = 3.14 #a floating point assignment Y= 200 #an integer assignment Empname = “Arun Baruah” #a string assignment Keep in mind that when you are working on codes, you are able to leave a comment with your wok by using the # sign. This allows you to explain what is going on in the code, leave some notes, or do something else within the program. It is not going to be read by the interpreter since it is just a little note that you are leaving behind for yourself or for someone else. The next part is going to depend on which version of Python you are using. Python 2 is fine with you writing out print and then the information you want to talk about but Python 3 is going to require you to place the parenthesis in to make it work. An example would be: Print(“y = %d” %y) Print(“x = %d” %x) Pring(“Employee Name is %s” %empname) These would then be put through the interpreter and the outputs that you would get should be X = 10 Y = 200 Employee Name is Arun Baruah Now go through and put in this information to your program and see what comes up. If you didn’t get the right answers like listed above, you should go and check that the work is done. This is a simple way to show what you are able to do with Python and get the answers that you need. Multiple Assignments In addition to working with the single variables that were listed above, you will also be able to work on multiple assignments. This means that you are going to be able to assign one value to several different variables at the same time. To do this, you would just need to place the equal sign between all of them to keep things organized and to tell the computer that the value is going to be with all of the variables together. You can keep them separated out if that is easier for you, but using this method is going to help you to send everything to the same memory location on the computer and will give the code a clearer look on your screen. A good example of how to give more than one variable the same value includes: a = b = c = 1 This is telling the code that you want all of them to be tied with the value of 1 and that all of these variables should have the same value and that you want to assign them all to the same location within your memory. Standard Data Types Another thing that you are able to work on when doing Python is the various data types. These are going to be used in your code in order to define the operations that you can do on each data type as well as explain to others the storage method that will be used for this kind of data. Python has five data types that are considered standard including: Numbers Dictionary Tuple List String Numbers Number data types are the ones that will store the numeric values. They are going to be created as objects once you assign a value to them. There are also four different types of numericals that Python will support including Complex (such as complex numbers) Float (floating point real values Long (long integers that can also be shown as hexadecimal and octal.) Int (signed integers) One thing to note is that while Python will allow you to use the lowercase l when doing the long form of a number, it is best to go with an uppercase L whenever you are using the letter. This is going to help you avoid confusion in reading the program between the l and the 1 as they look really similar. Any time that Python is displaying a long integer that has the l in it, you will see the uppercase L. Strings Strings are identified in Python as a contiguous set of characters that will be shown by the use of quotations marks. Python is going to allow for either double quotes or single quotes, but you do need to keep things organized. This means that if you use a double quote at the beginning of your string, you need to end that same string with the double quote. The same goes when you are using a single quote. Both of these will mean the same thing, you just need to make sure that you are using the proper quote marks to make the code look good and to avoid confusing the Python program. In addition to being able to print off the string that you would like, you are also able to tell the program to print just part of the string using some special characters. Let’s look at some of the examples of what you are able to do with the strings, and the corresponding signs that you will use at well, to help illustrate this point. str = ‘Hi Python!’ print(str) #prints complete string print(str[0]) #prints the first character of the string print(str[2:5]) #prints characters starting from the 3rd to the 5th print(str[2:]) #prints string starting from the 3rd character print(str*2) #prints the string two times print(str+”Guys”) #prints concatenated string For the most part you are probably going to want to print out the whole string to leave a message up on your program so the first print that you do is going to be enough. But if you just want to print out Hi or some other variation of the words above, you may find that the other options are really useful. You can do any combination of these, they are just examples to help you get started! Lists Lists are one of the most versatile data types that you can work on in Python. In this language, the list is going to contain different items that are either enclosed with the square brackets or separated out with commas. They are similar to the arrays that you would see in C if you’ve worked with that program. The one difference that comes up with these is that the items that are in a list can be from different data types. The values that are stored inside the list can be accessed with a slice operator as well as the [:} symbol with the indexes starting at 0 at the beginning of the list and then working down until you get to -1. The plus sign will be the concatenation operator while you can use the asterisk as the repetition operator. For some examples of what all this means and how you can use the different signs within your programming, consider some of these examples: list = [‘mainu’, ‘shainu’, 86, 3.14, 50.2] tinylist = [123, ‘arun’] print)list)#prints complete list print(list[0]) #prints the first element of the list print(list[1:3]- #prints elements starting from the second element and going to the third print(list [2:]) #prints all of the elements of the list starting with the 3rd element. Print(tinylist*2) #prints the list twice. Print(list + tinylist) #prints the concatenated lists. Tuples The next thing that we need to learn about for the Python language is about tuple. This one is pretty similar to what you are going to find with a list, but it is going to use some different signs. The main difference though is that lists will use brackets and the elements, as well as the size, can be changed through the program. On the other hand, the tuples are going to use parentheses and you will not be able to update them. A good way to think about tuples is that they are going to be like a read only page. As long as you don’t try to make changes to the tuple in the program, you are going to be able to use it in the same way as you did the list examples above. This makes it a great option to use if you’re looking for something that is simple but won’t let anyone make changes to the program after you are done. Dictionary Dictionaries are another kind of tool that you can use when you are working in Python. They are similar to a hash table type and they are going to work similar to the hashes or the arrays that you can find on other programming languages like C# and Perl. They will also consist of key value pairs and while the key can be almost any type on Python, you will notice that they are usually going to be strings or numbers. For the most part, when it comes to values, you will find that they are an arbitrary object in python. Some examples of how this will work include the following codes: #dictionary stores key-value pair, later to be retrieved by the values with the keys dict = {} dict[‘mainu’] = “This is mainu” dict[10] = ‘This is number 10” empdict = {‘name’: ‘arun’, ‘code’:23, ‘dept’: ‘IT’} print(dict[‘mainu’])#this will print the value for the ‘mainu’ key print(dict[10]) #this will print the value for the 10 key print(empdict) #this will print the complete dictionary print(empdict.keys()) #this will print out all of the keys print(empdict.values()) #this will print all the values One thing to keep in mind is that these dictionary values are not going to be stored in an order that is sorted. They aren’t going to have the concept of ordering among the elements. This does not mean that you can say that the elements are out of order, they are just going to be unordered. Keywords Most of the types of programming languages that you will deal with will have some keywords or words that are reserved as part of the language. These are words that you really shouldn’t use in your code unless you absolutely can’t help it. There are 33 keywords found in the most recent version of Python and you will need to spell them properly if you want them to do the job that you lay out. The 33 keywords that you should watch out for include: False Class Finally Is Return None Continue For Lambda Try True Def From Nonlocal While And Del Global Not Yield As Elif If Or Assert Else Import Pass Break Except In Raise Keep this list on hand if you are worried about learning the language. It will be able to help you out any time that you have issues with the interpreter about the names that you are giving the variable. You may be confused about why it is giving you some issues with the words you chose, you can go through with this list and see if you used one of the keywords inappropriately within your code. Statements When you are writing your code in the Python language, you are going to be making expressions and statements to get it done. Expressions are going to be able to process the objects and you will find them embedded within your statements. A statement is basically a unit of code that will be sent to the interpreter so that it can be executed. There are two types of statements that you can use; assignment so far and print. You will be able to write out the statement, or multiple statements, using the Python Shell to do so interactively or with the Python script using the .py extension that we talked about later. When you type these statements into the interactive mode, the interpreter will work to execute it, as long as everything is properly in place, and then you can see the results displayed on the screen. When there are quite a few lines that you need to write in code, it is best to use a script that has a sequence of statements. A good example of this is: #All of these are statements X = 56 Name = “Mainu” Z = float(X) Print(X) Print(Name) Print(Z) Operands and operators There are a lot of great symbols that are going to show up when you make a code in your Python program. It is important to understand what parts you are able to work with and what they are all going to mean. Operators are often used to mean subtraction, addition, division, and multiplication. The values of the operator will be called operands. You can use many different signs for these in order to get the values that you would like to see. While you are using the operators and operands, you need to remember that there is going to be an order of evaluation that is followed. Think about going back to math class and how this all worked. You had to look for specific signs in order to figure out which tool you were supposed to use in order to come up with the right answer. This is the same when using these operands within your code. When you have more than one of these operators in the expression, you will need to do the order of evaluation based on the rules of precedence. For anything that is arithmetic, Python is going to use the acronym PEMDAS which is parenthesis, exponentiation, multiplication, division, addition, and subtraction. If there are a number of these that are the same, such as two sets of numbers that need to be multiplied together, you will need to work from left to right to get the correct number. Another important operator that you should look for is the modulus operator. This one is going to work with integers and is going to yield the remainder once the first operand has been divided by the second one. . for Python One thing that you will find useful with Python is that you are able to send out . in the program. This allows you to add in something to explain what you are working on rather than just leaving it up to the other programmer to figure out. You will simply need to bring in the # sign in order to denote that you are going to leave a little bit of a comment or statement about the code. Any time that you use the # symbol, the program interpreter is going to ignore what you say rather than trying to write it. While the computer program is ignoring it, it is still there for the programmer to look at if needed. Using the Python language does not need to be difficult, but you do need to be able to understand what is going on in each part and how all of them are going to help you to get the results that you need. Each one will work slightly differently so that you are able to get the right codes done in Python.

No comments:

Post a Comment