Category Archives: Python Basics

Fetching records using fetchone() and fetchmany()

Up until now we have been using fetchall()  method of cursor object to fetch the records. This process of accessing all records in one go is not every efficient. As a result MySQLdb has fetchone() and fetchmany() methods of cursor object to fetch records more efficiently.

Method Description
fetchone() This method returns one record as a tuple, If there are no more records then it returns None
fetchmany(number_of_records) This method accepts number of records to fetch and returns tuple where each records itself is a tuple. If there are not more records then it returns an empty tuple.
Continue reading

MySQLdb Fetching results

In the previous post we have seen how to execute sql queries using execute()  method. execute()  method returns affected rows, it doesn’t return the result. To fetch result we use the fetchall()  method of the cursor object

Syntax: cursor.fetchall()

On success it returns tuple of rows where each row is a tuple. Continue reading

Python String Formatting

format() method allows you format string in any way you want.

Syntax: template.format(p1, p1, .... , k1=v1, k2=v2)

template is a string containing format codes, format()  method uses it’s argument to substitute value for each format codes. For e.g

{0}  and {1}  are format codes. The format code {0}  is replaced by the first argument of format()  i.e 12 , while {1}  is replaced by the second argument of format()  i.e 31 . Continue reading

Python Lambda Function

Python allows you to create anonymous function i.e function having no names using a facility called lambda function.

lambda functions are small functions usually not more than a line. It can have any number of arguments just like a normal function. The body of lambda functions is very small and consists of only one expression. The result of the expression is the value when the lambda is applied to an argument. Also there is no need for any return statement in lambda function.

Let’s take an example:

Consider a function multiply()

This function is too small, so let’s convert it into a lambda function.

To create a lambda function first write keyword lambda followed by one of more arguments separated by comma, followed by colon sign ( : ), followed by a single line expression.

Expected Output:

Here we are using two arguments x  and y , expression after colon is the body of the lambda function. As you can see lambda function has no name and is called through the variable it is assigned to.

You don’t need to assign lambda function to a variable.

Expected Output:

Note that lambda function can’t contain more than one expression.

What is if __name__ == “__main__” ??

Every module in python has a special attribute called __name__ . The value of __name__  attribute is set to '__main__'  when module run as main program. Otherwise the value of __name__  is set to contain the name of the module.

Consider the following code for better understanding.

Here we have defined a new module my_module . We can execute this module as main program by entering the following code Continue reading

Python recursive functions

When a function call itself is knows as recursion. Recursion works like loop but sometimes it makes more sense to use recursion than loop. You can convert any loop to recursion.

Here is how recursion works. A recursive function calls itself. As you you’d imagine such a process would repeat indefinitely if not stopped by some condition. This condition is known as base condition. A base condition is must in every recursive programs otherwise it will continue to execute forever like an infinite loop.

Overview of how recursive function works Continue reading