Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Thursday, 8 December 2016

Python Funtion : 'return' Statement

Python function 'return' statement - In two of our previous articles, we've learned about Python function definition & calls and Python namespace & scope. Just like these articles, this one is also related to Python as we are going to know about return values and the return statement. I recommend you to go through the mentioned articles, to have a clearer idea about this article, as it is linked to them.

python-function-return-statement


Lets start this discussion with the important takeaways from the earlier articles on Python function :
  1. A function can be defined using the def keyword, the name of the function, followed by a pair of parenthesis.
  2. A function can have parameters to store the value of the argument, when the function is called.
  3. Then follows indented block of statements, where the entire logic is incorporated.
  4. We can call a function with its name and the parenthesis like myFunc().
  5. We can define arguments to send some information to the function.
  6. Any variables used inside a function are stored in its local namespace.
  7. We have global namespace where all the variable names declared in the program body (outside functions) reside.
  8. We have enclosed namespace when two or more functions are nested, the variable names used in enclosing functions are stored in enclosed namespace.
  9. The built-in namespace consists mainly of Python keywords, functions and exceptions.
  10. A name is searched in these namespaces in a specific order as per LEGB rule i.e. Local, Enclosed, Global and Built-in.

Return Values and return Statement

So far, we have used functions to perform some tasks and print the result, may it be a simple string or the table of a number or some power of some number, using the print statement. A print statement writes the output to the screen (stdout), which cannot be used for further processing. Say, if we have two functions power(base, exp) that calculates expth power of base and another function cubert(num) to calculate cube root of a number. We wish to calculate a number using power() and pass it as an argument to cubert() function. As we are printing out the result on the stdout, we cannot use it in another function, even if we had stored it in a variable (remember local namespace and global namespace?). So, we need to have a mechanism with which the result from one function can be used in the program, as and when needed. And we have return statement.

The return statement is optional to use. When used, it comes out of the current function and returns to the position where it was called i.e. the caller. It also sends back a value to the caller, which then can be used anywhere in the program or by another function. The value which needs to be handed back (known as return value), is decided by the programmer and it is the argument to the return statement, e.g. return object_name. When return is not used, the function returns None by default. To demonstrate this, lets check the example of our power() function created in the previous article.

Example 1 : If return is not used, None is returned.

>>> def power(base, exp) :
...     print str(base) + ' to the power of ' + str(exp) + ' is ' + str(base ** exp)
...
>>> myResult = power(3, 4)
3 to the power of 4 is 81
>>> print myResult
None

In above example, the function power() just prints the result on the screen, return statement not being used. Knowing that the print statement doesn't return anything and return not used, the default value None is returned to the caller, which then gets assigned to global variable myResult and displayed.

Example 2 : Returning a value

>>> def power(base, exp) :
...     print str(base) + ' to the power of ' + str(exp) + ' is ' + str(base ** exp)
...     return base ** exp
...
>>> myResult = power(3, 4)
3 to the power of 4 is 81
>>> print myResult
81

In above example, we actually used the return statement, that returns the result of exponentiation operation to the caller. Thus, myResult is assigned with the value returned by the power() and the same is printed. Had we written any statements after return, they would not have executed.

Example 3 : Using return values from another function

# Function fo calculate 'n'th power of a number
>>> def power(base, exp) :
...     return base ** exp
...

# Function to add two numbers
>>> def addition(a, b) :
...     return a + b
...

# Function that user power() and addition() to add squares to two numbers
>>> def myFunction(num1, num2):
...     sqr_num1 = power(num1, 2)
...     sqr_num2 = power(num2, 2)
...     result = addition(sqr_num1, sqr_num2)
...     return result
...
# Function call
>>> myFunction(3, 4)
25
>>> myFunction(8, 6)
100

In this example, we have two functions power() and addition(), those return the results of exponentiation and addition operations respectively. The third function myFunction used these two functions to calculate sum of the squares of two numbers and return the result.

With this, we end our discussion on Python function return values and return statement. In this article, we learned how the result of the operations done by a function can be used to be processed in the program using return values, which we cannot achieve using print statement. We have already discussed on the basics of function arguments, we will be learning about them much more details, in the next article. Please share your opinions and views in the comment section below and stay tuned for more interesting articles on Python. Thank you!

This article is originally published at www.codeninja.in - Python Function : Return Values and 'return' Statement

Wednesday, 7 December 2016

Python Namespace & Variable Scope - Local, Enclosed, Global

Python Namespace and Variable Scope - In this article, we will be learning about local and global variables. This article is related to our last article on Python function, so I recommend to have a read over it, so that things will be easier to understand. In this article, we are going to understand namespace first, where all the declared variables are actually stored and retrieved as and when required. Then we will be knowing about two important terms - local variables and global variables.

python-namespace-variable-scope

Name and Namespace

We know that, everything in Python is an object and to identify an object easily, we assign a name to it, which we call as a variable. In our article on Python variables, we studied that, a variable is an identifier of an object and the object can be accessed using the name assigned to it. Consider that, we have declared a variable as myString = "I <3 Python!". With this, a str type of object "I <3 Python!" is stored in the memory, which can also be accesses using the name myString. Now, we can perform any operation on the object or call corresponding methods on it, by using it's name. For example, in order to capitalize every letter in the string, we can use myString.upper().

Now, if we create some more objects and assign names to them, those will be stored in memory locations and will be available to be accessed using the names assigned to them. With this, you can consider a 'namespace' to be a place where all these names are stored. In simple words, a namespace is the collection of all the names in the form of dictionary items as name : object. To more understand this, I would like to introduce globals() function here, that will display the mapping. For now, don't bother about why the name globals() and all, we will see it later. This is just to show you how the namespace looks like.

>>> myString = "I <3 Python!"
>>> myList = [1, 2, 3, 4]
>>> myTuple = ('A', 'B', 'C')
>>> globals()
{'myTuple': ('A', 'B', 'C'), 'myList': [1, 2, 3, 4], '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'myString': 'I <3 Python!', '__name__': '__main__', '__doc__': None}

In above example, we have created three objects and assigned names myString, myList and myTuple to them. To display the namespace, we called Python built-in function globals(), which returned a dict type, with items in name : object format, 'myTuple': ('A', 'B', 'C') being one of them. Thus, as soon as we create a name and assign a value to it, it becomes a part of namespace. We have zero or more local namespaces and a global namespace. Based on where in the code the assignment happens, the namespace which the variable will be a part of, is decided. Let us discuss this in the next section.

Global Namespace and Local Namespace

As mentioned above, a name is a part of which namespace, is decided by where the variable is declared in the program. This arises one more question in our mind that, Where can we declare a name?. Answer will be, a name can be defined in the main program body, in a function or in a module (a module can be considered as a pre-existing Python program file, from where an object can be imported). If a name is declared in the main program body, it will be a part of global namespace, while a name definition inside a function makes it a part of local namespace. Every function declared has its own local namespace, so that the values assigned to a name inside a function do not get overwritten with the names declared in other functions or main program body (global namespace). In conclusion -

1. We have a global namespace, where all the names declared in the main program body reside.
2. Every function is associated with its own namespace, known as local namespace. We can have multiple local namespaces in a program, as we can have zero or more functions in our piece of code.
3. A name declared in a function can only be accessed within that function
4. A name can be a part of one or more namespaces, but their values do not collide with each other. For example, name myVar declared in main program body (global namespace) and a name myVar declared in a function myFunc are absolutely different entities and their value do not clash with one another.
5. This also means that, in case of nested functions (function inside another function) will have individual namespaces associated with each of them. In this case, the namespace of enclosing function is often called as Enclosed namespace.
6. Every call to a function creates a new local namespace.
7. We also have Built-in namespace, which is reserved by Python, consisting mainly of exceptions and built-in functions.

There might be numerous namespaces created in a program, but all the namespaces are not available to be accessed from anywhere in the program. This introduces a new term Scope, that defines the section of the program from where the namespace can be accessed. We discuss this in the next section.

Scope

In the above section, we have come to know that a program may have a number of namespaces, which are independent from each other and not accessible from everywhere in the program. A scope decides the part of the program from where the name can be accessible. So, based on where the name is referenced in the program, the name is searched in the namespaces in a specific order as per LEGB (Local -> Enclosed -> Global -> Built-in) rule.

As per LEGB rule, the order of search should be -
1. Local scope (or namespace) - The name is searched in the local namespace, if it is referenced inside a def.
2. Enclosed scope - If not present in local namespace, the namespace of enclosing def is searched.
3. Global scope - If not found, global namespace - the top level namespace in a file, is looked into.
4. Built-in scope - If still not found, it searches the Built-in scope, which is nothing but the Python builtin module.
5. If the name is not found in any of above mentioned scopes, a KeyError exception is raised. We see some examples in order to verify everything we studied in this article, so far.

Example 1 : Local and Global Scope

>>> myVar = "Global"
>>> def myFunction() :
...     myVar = "Local"
...     print 'Value of myVar inside function = ' + myVar
...
>>> print 'Value of myVar outside function = ' + myVar
Value of myVar outside function = Global
>>> myFunction()
Value of myVar inside function = Local

In above example, we have declared a name myVar = "Global" in the main program body and myVar = "Local" inside a function myFunction. When we use the variable in the main program, its value from the global namespace is referred, whereas when the function is called, it's value from local namespace is referred, as per LEGB rule.

Example 2 : Local, Enclosed and Global Scope

>>> myVar = "Global"
>>> def outerFunction():
...     myVar = "Enclosed"
...     print 'Value of myVar in outerFunction() = ' + myVar
...     def innerFunction():
...             myVar = "Local"
...             print 'Value of myVar in innerFunction() = ' + myVar
...     innerFunction()
...
>>> print 'Value of myVar in main program = ' + myVar
Value of myVar in main program = Global
>>> outerFunction()
Value of myVar in outerFunction() = Enclosed
Value of myVar in innerFunction() = Local

In above example, we have myVar declared thrice with values "Global" in the main program body, "Enclosed" inside outerFunction and "Local" in innerFunction, to indicate which namespace value myVar takes when referenced at different locations in the program.

Example 3 : Local, Enclosed, Global and Built-in Scope

As discussed earlier, the __builtin__ module contains all keywords reserved by Python. To check the all of them, we import the module and provide it as an argument to dir() function. We will observe some exceptions and built-in functions in the long list returned.
# Snipping the long list
>>> dir(__builtin__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', ... , 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']

Now, we create our own pow() function that does the reverse operation i.e. Exponent to the power Base and check if it gets executed before the one in built-in scope.

>>> def pow(base, exp):
...     print 'Result from the pow function = ' + str(exp ** base)
...
>>> def myFunction(base, exp):
...     print 'Calling pow() from myFunction...'
...     pow(base, exp)
...     print 'Result from myFunction = ' + str(base ** exp)
...
>>> myFunction(3, 4)
Calling pow() from myFunction...
Result from the pow function = 64
Result from myFunction = 81

From the above results, we observe that, the pow() function from the global scope is executed before the pow() function in the built-in scope.

With this, we've come to an end of this article. In this article, we learned mainly about namespace and scope. Essentially, we understood how the names are searched in the scopes, based on where they are referenced in the program. In the next article, we study about return values and return statement. Please share your views and opinions in the comment section below and stay tuned for more articles. Thank you.

This article is originally published at www.codeninja.in - Python Namespace and Scope

Tuesday, 6 December 2016

Python Function - Definition, Call, Arguments and Parameters

Python Function - In our recent articles, we have discussed on loop statements - for-else loop and while-else loop. In this article, we will be learning about what functions are, how we can create our own function and how we can use it.

python-function-definition-call-argument-parameter

Functions in any programming language is a group of statements, identified by a name, that perform a specific task. Whenever we want to re-use these statements in the code, instead of writing them all every time, we can invoke them using the name specified. This will not only save our copy-paste efforts but also reduce the code redundancy in the program. Also, for any correction to be made to any statement in the group, one need not correct every occurrence of it. Instead, it would be sufficient to make appropriate corrections in the defined function. Things will be clear when we go through some examples of functions. But to start with, lets learn how functions are created or defined.

Function definition

To define a function, Python has a keyword def, which gives us a function type or an object of class function. Please have a look at the basic syntax first, then we will try to unfold everything.

def function_name():
    # Block of code starts
    ...
    ...
    ...
    ...
    # Block of code ends

In the syntax above, the line starting with def is often called as function header and it consists of the keyword def, the name of the function and a pair of parenthesis. In the parenthesis, we can mention zero or more parameters, which we will discuss about later. After that, we have an indented block of statements, which we can call as 'function body', that will be executed at every run of the function. So, if we were to define our own function myFirstFunction which does nothing, we can have a pass statement in the function body as shown below:

>>> def myFirstFunction() :
... pass
...

Congrats, you've just created your first Python function. Had we replaced the pass statement with print 'Hello World', it would have printed Hello World on the screen. Shall we check the what type object a Python function is? We require a Python built-in function for this, which is dir() function.

>>> type(myFirstFunction)
<type 'function'>

As I said earlier, the resultant object is of function type. Now, we have created a function, but how to use it? Lets see this in following section.

Function Call

A function call is nothing but making use of the function you have created. Well, to do this, you just need to mention the function name followed by a pair of parenthesis. Remember, with the def keyword, you just created a function. Unless you create a function, you cannot call it. So, the function definition must come before the function call or you will face an error message. Lets try defining a function myFunction, that prints Congrats! You have just executed a function, and calling it.

>>> def myFunction() :
...    print "Congrats! You have just executed a function"
...

>>> myFunction()
Congrats! You have just executed a function

As mentioned above, in order to call a function, we just mentioned it's name followed by the parenthesis and we have the function executed. Next, we need to know about two important terms related to Python functions, which I might have mentioned earlier, but have to be understood in detail - Function Argument and Function Parameter

Function Argument and Function Parameter

We have already used some functions like dir() and len() in our previous articles, in which dir and len are the function names. We have used them to get a list of attributes and methods associated with the object and to find length of the object respectively. So, in order to get the attribute list of a list myList, we would use dir(myList) or to find its length, we would use len(myList), where we call myList as an Argument to dir() and len() functions. With an argument, we send some information, in the form of variable or a value, to a function. On the other hand, function receives these values using another variable, declared in the function header, which is called as a Parameter.

To understand this, lets take an example of any Python built-in function, say dir() function. The built-in functions are defined somewhere in the Python library and they must be defined in somewhat similar syntax as seen earlier. So, the dir() function might look like :

def dir( param ) :
    ....
    ....
    ....
    ....

Thus, the variable param is the parameter of the function. When we actually use the function as dir(myList), by providing an argument myList to that function, the function reads the value stored in the variable myList and assigns it to the variable param. After some processing on it, we finally get appropriate output. Take a look at below examples.

Example 1 : Table of a number

>>> def table(myNum) :
...     print '1 * ' + str(myNum) + ' = ' + str(1 * myNum)
...     print '2 * ' + str(myNum) + ' = ' + str(2 * myNum)
...     print '3 * ' + str(myNum) + ' = ' + str(3 * myNum)
...     print '4 * ' + str(myNum) + ' = ' + str(4 * myNum)
...     print '5 * ' + str(myNum) + ' = ' + str(5 * myNum)
...     print '6 * ' + str(myNum) + ' = ' + str(6 * myNum)
...     print '7 * ' + str(myNum) + ' = ' + str(7 * myNum)
...     print '8 * ' + str(myNum) + ' = ' + str(8 * myNum)
...     print '9 * ' + str(myNum) + ' = ' + str(9 * myNum)
...     print '10 * ' + str(myNum) + ' = ' + str(10 * myNum)
...

>>> table(5)
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

In above example, we have created a function with name table which prints the table of a number provided as an argument. It uses the parameter myNum to store the number. After some operations on myNum, we print the result. We also checked calling it with number 5 as an argument and it prints the table of 5. Now, in order to print the table of 7, we need not write the statements again, just call the function as table(7) and you are done. Conclusion - Functions improve the re-usability of the code and reduces redundancy.

Example 2 : Power of a number

>>> def power(base, exp) :
...     print str(base) + ' to the power of ' + str(exp) + ' is ' + str(base ** exp)
...

>>> power(2, 3)
2 to the power of 3 is 8
>>> power(3, 4)
3 to the power of 4 is 81
>>> power(6, 3)
6 to the power of 3 is 216

In this examples, we have created a function with name power that has two parameters base to store the value of base and exp to store the value of exponent. This function will calculate the expth power of base and print it on the screen. While calling the function, we have mentioned two arguments every time and got appropriate results in the output.

In this way, we have come to an end of this discussion of this article. This article was intended to provide you very basic knowledge about functions, in which we studies to define a function and call a function. Meanwhile, we also learned about arguments and parameters and used them in our function definition and function call. Although, we are not finished with functions, I recommend you to practice on whatever we have studied and start writing some functions of yours. Please write your reviews and feedback in the comment section below. In the next article, we will cover scope of the variables. Till then, stay tuned. Thank you.

This article is originally published at www.codeninja.in - Python Function Basics

Monday, 5 December 2016

Python 'while-else' loop with 'break' & 'continue' statements

Python while-else loop - In the last article, we have covered the first loop statement in Python, for-else statement. In this article, we are going to learn about another loop statement - while-else loop. There are some differences as far as syntax and their working patterns are concerned, which we will be studying in this tutorial. Lets begin!


Python while Loop

The key difference between for and while loops is that, where for requires a Python iterable object to form a loop, while loop, we do not have any such prerequisites. It just needs a condition to be provided, which is tested at every iteration. As long as the condition is True, the block of statement is executed repeatedly. Once the condition becomes False, while loop is exited. Have a look at the while loop syntax below.

while (condition) :
    # Block of statements starts
    ----
    ----
    ----
    ----
    # Block of statements ends

In this, when we enter the while loop for the first time, condition is checked, if it evaluates to False, it does not enter into the loop. If the condition evaluates to True, the block of statement is executed to finish the first iteration. After this, control goes back to the while (condition) : statement to re-check the condition and the process repeats. The block of statements will keep executing repeatedly as long as the condition evaluates to True, thus forming a loop. An immense care should be taken in order not to get caught into an infinite loop, while using while loop. Lets check out some examples of while loop.

Example 1 : Printing numbers and their squares

>>> myNum = 11
>>> myVar = 0
>>> while myVar < myNum :
...     print 'Square of ' + str(myVar) + ' is ' + str(myVar ** 2)
...     myVar += 1
...
Square of 0 is 0
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 36
Square of 7 is 49
Square of 8 is 64
Square of 9 is 81
Square of 10 is 100

In above example, we have a variable myVar which begins counting from 0 until its value is less than another variable myNum = 11. In the first iteration, myVar = 0 which is less than myNum and evaluates the condition to True and executes the print statement. After printing, the value of myVar is incremented by 1 to finish the first iteration. Now, control goes back to while statement where condition myVar < myNum is checked. Here, myVar = 1 and myNum = 11 evaluates the condition to True and block of statement is executed. This process will repeat till myVar reaches 10. At this point, myVar = 10 and myNum = 11 evaluates the condition to True, prints the statement and increments myVar by 1 to take a value 11. Now, when the control goes back to while statement, it evaluates the condition to False which terminates the loop.

Example 2 : Factorial of a number

# Calculating 7!
>>> myNum = 7
>>> fact = 1
>>> while myNum :
...     fact *= myNum
...     myNum -= 1
...
>>> print fact
5040

# Calculating 5!
>>> myNum = 5
>>> fact = 1
>>> while myNum :
...     fact *= myNum
...     myNum -= 1
...
>>> print fact
120

In above example, we use while loop such that the variable myNum takes the values ranging from it's initial value to 1 (Please observe the myNum -= 1 statement). We use these values to continuously keep multiplying another variable fact, that stores the result. Eventually, fact will contain 1 * 2 * 3 * ... * myNum which is printed on the last line. Please note that, when the variable myNum reaches the value 0, condition evaluates to False to come out of while loop.

break and continue Statements Revisited

As we've already seen in the last article on Python for loops, break statements are used for terminating a loop prematurely, based on certain condition. The break statement can also be used in the same way in case of while loops. We also have continue statement, that is used to skip executing further code, by jumping straight to the next iteration, based on certain condition.

Example 1 : Breaking the iteration

>>> myNum = 11
>>> myVar = 1
>>> while myVar < myNum :
...     print 'This is iteration #' + str(myVar)
...     if myVar == 6 :
...             print 'This is the last iteration and we stop here.'
...             break
...     myVar += 1
...
This is iteration #1
This is iteration #2
This is iteration #3
This is iteration #4
This is iteration #5
This is iteration #6
This is the last iteration and we stop here.

In above example, we created a loop which starts with myVar = 1 and is expected to run till myVar reaches myNum. But, with the if condition mentioned, the loop is terminated prematurely when the value of myVar reaches 6.

Example 2 : Printing only Even numbers using 'continue' statement

>>> myNum = 15
>>> myVar = 1
>>> while myVar < myNum :
...     if myVar % 2 != 0 :
...             myVar += 1
...             continue
...     print 'This number = ' + str(myVar)
...     myVar += 1
...
This number = 2
This number = 4
This number = 6
This number = 8
This number = 10
This number = 12
This number = 14

In above example, we create a loop which is expected to print values from 1 to 14. But, with if statement, we check whether myVar is an Even number, if it is, we print the statement. If myVar is Odd, continue statement is executed, which jumps to the next iteration by skipping the execution of subsequent lines of code. As a result, we get only even numbers printed on the screen.

Python while-else Loop

As in case of for loop, we have an optional else block in case of while loops. It does work in exactly the same way it works in case of for loop. The else block gets executed only when the break statement is not executed. This also means that, absence of break statement will execute the else block once. Please take a look at below examples.

Example :

# 'break' is executed, hence 'else' will not be executed
>>> myNum = 6
>>> myVar = 1
>>> while myVar <= 10 :
...     if myVar == myNum :
...             print 'Breaking out of the loop'
...             break
...     print 'This number = ' + str(myVar)
...     myVar += 1
... else:
...     print 'Break statement is executed, printing "else" block'
...
This number = 1
This number = 2
This number = 3
This number = 4
This number = 5
Breaking out of the loop

# 'break' is not executed, hence 'else' will be executed
>>> myNum = 12
>>> myVar = 1
>>> while myVar <= 10 :
...     if myVar == myNum :
...             print 'Breaking out of the loop'
...             break
...     print 'This number = ' + str(myVar)
...     myVar += 1
... else:
...     print 'Break statement is executed, printing "else" block'
...
This number = 1
This number = 2
This number = 3
This number = 4
This number = 5
This number = 6
This number = 7
This number = 8
This number = 9
This number = 10
Break statement is executed, printing "else" block

In above examples, we try to print numbers from 1 to 10. In first example, we introduce a break statement when myVar reaches 6. Due to this, the program will print numbers from 1 to 5 and as soon as myVar takes value equal to 6, program executes break, due to which else block is skipped from being executed. In the second example, the loop will be broken when myVar reaches 12, which it will never reach and break statement will never get executed. As break is not executed, else block is executed.

With this, we have come to an end of this discussion on Python while loops. In this article, we've learned how while are constructed and how the control flow works. We have also revised how break and continue statements work in while loops. After that we studied the while-else loop along with its examples. Having covered for and while loops, we have finished the loop statements in Python. In the next article, we will be discussing on Python functions. Please share your views and feedback on this article in the comments section below and stay tuned. Thank you!

This article is originally published at www.codeninja.in - Python while-else Loop with break and continue Statement

How To : Install or Upgrade to Linux Kernel 4.9-RC8 in Ubuntu/Linux Mint

    The Linux Kernel 4.9-RC8 is now available for the users, announced Linus Torvalds. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.9-RC8 in your Ubuntu or Linux Mint system.

linux-kernel-4-9-rc8

Installation

For 32-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9-rc8/linux-headers-4.9.0-040900rc8_4.9.0-040900rc8.201612051443_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9-rc8/linux-headers-4.9.0-040900rc8-generic_4.9.0-040900rc8.201612051443_i386.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9-rc8/linux-image-4.9.0-040900rc8-generic_4.9.0-040900rc8.201612051443_i386.deb
Install them.
$ sudo dpkg -i linux-headers-4.9*.deb linux-image-4.9*.deb
Reboot the system.
sudo reboot

For 64-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9-rc8/linux-headers-4.9.0-040900rc8_4.9.0-040900rc8.201612051443_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9-rc8/linux-headers-4.9.0-040900rc8-generic_4.9.0-040900rc8.201612051443_amd64.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9-rc8/linux-image-4.9.0-040900rc8-generic_4.9.0-040900rc8.201612051443_amd64.deb
Install them.
$ sudo dpkg -i linux-headers-4.9*.deb linux-image-4.9*.deb
Reboot the system.
sudo reboot

To uninstall,
sudo apt-get remove linux-headers-4.9* linux-image-4.9*

How To: Install or Upgrade to Linux Kernel 4.8.12 in Ubuntu/Linux Mint

The Linux Kernel 4.8.12 is now available for the users, announced Linus Torvalds. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.8.12 in your Ubuntu or Linux Mint system.

linux-kernel-4-8-12

Installation

For 32-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.8.12/linux-headers-4.8.12-040812_4.8.12-040812.201612020431_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.8.12/linux-headers-4.8.12-040812-generic_4.8.12-040812.201612020431_i386.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.8.12/linux-image-4.8.12-040812-generic_4.8.12-040812.201612020431_i386.deb
Install them.
$ sudo dpkg -i linux-headers-4.8.12*.deb linux-image-4.8.12*.deb
Reboot the system.
sudo reboot

For 64-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.8.12/linux-headers-4.8.12-040812_4.8.12-040812.201612020431_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.8.12/linux-headers-4.8.12-040812-generic_4.8.12-040812.201612020431_amd64.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.8.12/linux-image-4.8.12-040812-generic_4.8.12-040812.201612020431_amd64.deb
Install them.
$ sudo dpkg -i linux-headers-4.8.12*.deb linux-image-4.8.12*.deb
Reboot the system.
sudo reboot

To uninstall,
sudo apt-get remove 'linux-headers-4.8.12*' 'linux-image-4.8.12*'