Monday, 15 July 2019

What are the basic things to know in Python Programming?


     Python is a high-level programming language that are easy to write programs and faster than different languages. If you want to compare C and Python to write the code, the 10 pages in C language will take one or 2 pages in Python. Python languages are used when you really need something done fast. It is a scripting language that allows you to do simple tasks for you. There is something called idle which is similar to a command shell, you can give simple instructions and test programs. Python professionally can be used for back-end development to analyze data and parse data when you have a huge pile of data. There are libraries to extract the things that you need from the file of data. Python3 is widely used on the server side and for scripting. It includes various testing, building, monitoring frameworks, scientific apps etc., It is widely used as an embedded language.
      If you have a newer version, feel free to download the stable release to work perfectly in Python3 website, and not in Python2. Follow the actual process of installation and later on, you can add the Python3 to the desktop directory by adding executable of this program. The interactive development environment used to help with the errors in coding. It simply executes a single line at a time.

Python Programming: We are going to use the idle and write the program in the idle notepad shell.
So, just click on the file, it will open new file same as the idle window and run the program from there. You can try out the programs from the idle and start programming by converting int, string and redesigned_string etc., Importing the module is important in python because it allows importing different python programs into our own program which allows using different functions, methods from that specific programs. In order to access the specific functions on the library like os, random, subprocess etc., you need to specify the name of the library with dot and specify the name of the function For ex, To import the library called os,  just type the command of import os and execute as os.system('mkdir test'). Finally, Go to the python.exe directory in terminal and run the command >> python.exe which will create test directory.

Defining the Functions in Python: The functions will be used once you need to cover a large part of the programs that will actually repeat itself throughout the code itself. So, you want to create a function that will do that certain part. You can actually call that function every time you need it without coding the same. Basically, the function called print and what you specified in between the brackets are parameters of that function. We can write our own printing function like my_print and in the brackets specify what you want to print. In order to define the function in specific name, use def and type the name of the function. For ex, def my_print(string) : print(string). So, this will define our own functions.
             Sometimes, you actually want to return something from the function. For ex, if we want to
pick the random integer between 1 and 5 and print the different statement for each number that the actual random library picks. We can do that in the function and print the actual string. Let's open the new file and start coding by importing the random and create a random number using the random library from the range of 1 to 4. With randint function, the random number will take the value between 1 and 4 depending on what the program picks. Then we create the pick variables that will call this function and store its value of the outcome to the pick variable and then print the pick variable. So, when you run the module the output will be generated from the random library generated number and printed out while returning from the function.

Python Lists, Tuples, and Dictionary: Lists can store multiple values in a specific array. The syntax for lists in python is square brackets.  Lists are used to store different types of element that belongs to the same group. It has the following properties,
  1.  It can store string, boolean, float, and numbers in one list. Also, you can store the list elements into a certain variable and will print out the list that we specified. Remember, the actual counting starts from zero and does not start counting from 1.
 2. You can concat(+ in print) the element in the list. For ex, print( 'This is a ' + stationary_item[2]). It will print the 3rd element in the list with concated  message.
 3. The list can also have multiple symptoms of  an element that is going to be an entire different list. For ex, list = [[1,2,3,4], ['salt','pepper','water','milk']]. Here, the first list contains the number and the second list has the kitchen items. Suppose, if you want to select the pepper, just type the command of print(list[1][1]). This will print pepper.
Tuples: Some of the things that are actually similar to lists are called tuples. If you want to print the letters in the variables, you can specify the element position or use the for loop to retrieve all the letters in the variable. For ex, >>animal = "Rabbit", in order to print the letters in the variable of animal, type the command "for letter in animal : print(letter)". This will print R, a, b, b, i, t.
Dictionary in python is a collection of many values just like the list. In the dictionary, we have two separate types of values. Those are keys and values that are separated by two dots. The syntax for opening the dictionary is curly brackets{} that have key: values. For >> number_list = {'five':5, 'four':4,'two':2}. So, if you want access five, just type number_list['five'] which will print the value 5.

 Files in Python:  We need to import the os, subprocess or other libraries for accessing the files. It
has the functions of getcwd(), chdir() etc for handling the directories. The function mkdir will help you to create the folder in a specific directory. You can also look at different functions in the OS library like os.path.dirname, os.path.getsize, os.path.basename and check whether a certain file exists or if it is direct etc.,
      To open certain files in python, we need to use command open with the arguments of filename and mode of opening the file(W, R, A). The W mode is for writing, A for appending and R for reading the file. For ex,  fn = open('file.txt', r) then function will help to read the contents in the file. If you want to open the file in write mode, and use the write command for overwriting into the file. If you don't want to delete the contents in the file and simply want to add something, the A option will help to append the string into the file. Finally, if you just want to read the text from the file, use the option 'rt'. This will display the text in string format.

Object-Oriented Programming: The first thing, you encounter the OOPs is class. The class is a user-defined prototype for an object that defines a set of attributes characterized of any class. Basically, the class has data members, variables, and instance variables. Also, it has methods or function. The class variables are shared by all instances of the class. Class variables are defined within a class are coded at the beginning of the class. An instance is an individual object of a certain class. The inheritance transfer the characteristic of a class to other class that is derived from it. Data member holds the data associated with a class and its object. The object is the unique instance of that data structure that is defined by the class. 
             Let us create the class defined to our employees of the existing company and have the
variable that will count the number of employees and will set to zero at the beginning. We have the init function which is called the class constructor or initialization method that
python calls when you create the new instance of the class. Basically, this method will take all the variables that are going to be part of the employees. The python will take the self argument to list for you. So you don't need to include it when you call the methods. The actual variables of name, email, age, are three different values of the employees. This class actually used on certain employee count and increase the employee count by one. So, this function should be in the class.
    Now, create the actual method that will take the self argument because it belongs to the class and print the total employee number. The third function will display the information about the specific employees. Now, we can create instances of a class or add the person to the employee class. In order to add it to an actual class, we need to specify employee of their details of name, email, and age. This will create instances of the employee class.

Interacting with HTTP: There are two different modules(lib, libtool) handle HTTP related operations. But, We need to write lot of codes to make simple HTTP request. There is an third party library known as request is available and the developers prefer to use instead of lib library. It does not Pre-installed in Python, So, you need  a "pip install request" in your Pyhton Scripts directory. Once you performed this command, you can go back to the idle and type import requests. then, we use get request to retrieve data from any destination. Suppose, we want to retrieve the contents of the home page and print out the html data, you need to include html page link inside the get command and print the contents of that page.

The post requests are opposite of get requests. Basically, it means of sending data to a server as opposed to retrieving it. We are going to pass it in the data parameter and perform it on the website. In the idle, import requests library , then we need to create the payload which is our data currently and pass the username and password. Now, we need to create the post request and specify the link in the brackets and specify data equals to payload. If print in the text format of the url, you can see the username and password are passed to the form. You can also send the files through post request.

No comments:

Post a Comment