Author name: Prasanna

Introduction to Python – Executing Python Script

In this Page, We are Providing Introduction to Python – Executing Python Script. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Introduction to Python – Executing Python Script

Executing Python script

As discussed in the previous section, the Python script can be executed using the F5 functional key, from Python’s IDE. It can also be executed using a command prompt by typing the following command:

$ python <filename>

On different platforms, the execution of Python scripts (apart from running from inside Python’s IDE) can be carried out as follows:

Linux

On Unix/Linux system, Python script can be made directly executable, like shell scripts, by including the following expression as the first line of the script (assuming that the interpreter is on the user’s PATH) and giving the file an executable mode.

#! /usr/bin/env python

The ‘# !’ must be the first two characters of the file. Note that the hash or pound character ‘#’ is used to start a comment in Python. The script can be given an executable mode/permission, using the chmod command:

$ chmod + x HelloWorld.py

Windows

On the Windows system, the Python installer automatically associates .py files with python.exe, so that double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed. At MS-DOS prompt, the Python script can be executed by going to the directory containing the script and just entering the script name (with extension).

Introduction to Python – Executing Python Script Read More »

Introduction to Python – Script Mode

In this Page, We are Providing Introduction to Python – Script Mode. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Introduction to Python – Script Mode

Script mode

If the Python interpreter is closed and then invoked again, the definitions that were made (functions, variables, etc.) are lost. Therefore, to write a long program, the programmer should use a text editor to prepare the input for the interpreter and run it with that file as input instead. This is known as creating a “script”. Most of the examples in this book are discussed using interactive mode, but few scripts are also incorporated.

First program

This section will demonstrate to write a simple Python program, which prints “Hello World”. Type the following lines in an IDLE text editor and save it as “HelloWorld.py”.

#! /usr/bin/env python 
print('Hello world')

The first line is called “shebang line” or “hashbang line” (more information in the next section). The second line gives the output: “Hello World”. There are numerous ways to run a Python program. The simplest approach is to press the F5 functional key after saving the program in an IDLE text editor. The output is shown below:

>>>
Hello world

Introduction to Python – Script Mode Read More »

Introduction to Python – Interactive Mode

In this Page, We are Providing Introduction to Python – Interactive Mode. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Introduction to Python – Interactive Mode

Interactive mode

One of Python’s most useful features is its interactive interpreter. It allows very fast testing of ideas without the overhead of creating test files, as is typical in most programming languages. However, the interpreter supplied with the standard Python distribution is somewhat limited for extended interactive use. IPython is a good choice for the comprehensive environment for interactive and exploratory computing.

To start interactive mode, launch Python with no arguments (possibly by selecting it from your computer’s main menu). It is a very powerful way to test out new ideas or inspect modules and packages.

Interactive mode prompts for the next command with the “primary prompt”, usually three greater-than signs (>>>); a continuation line is prompted with the “secondary prompt”, which is by default represented by three dots (…). The interpreter prints a welcome message stating its version number and some additional information before printing the first prompt:

$ python
Python 2.7 (#1, Feb 28 2010, 00:02:06)
Type "help", "copyright", "credits" or "license" for more information.
>>>

Continuation lines are needed when entering a multi-line statement. As an example, take a look at this if statement:

>>> the_world_is_flat = 1 
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
...
Be careful not to fall off!

Invoking Python interpreter

In Unix/Linux platforms, the Python interpreter is usually installed at /usr/local/bin/python. It is possible to start interpreter by typing the following command (same command for MS Windows)

$ python

in the shell. Since the choice of the directory where the interpreter lives is an installation option, other places are possible (e.g/usr/local/python is a popular alternative location).

On Windows machines, the Python installation is available at path C:\Python27, though, this can be changed when running the installer. To add this directory to Path environmental variable, type the following command into the MS-DOS command prompt:

set path=%path%;C:\python27

Inputting the end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the interpreter to exit. If that does not work, you can exit the interpreter by typing the following command:

>>> quit()

Introduction to Python – Interactive Mode Read More »

Introduction to Python – Object

In this Page, We are Providing Introduction to Python – Object. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Introduction to Python – Object

Introduction to Python Object

“Object” (also called “name”) is Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. Every object has an identity, a type, and a value. An object’s identity never changes once it has been created; it can be thought of it as the object’s address in memory. The id () function returns an integer representing its identity (currently implemented as its address). An object’s type determines the operations that the object supports and also defines the possible values for objects of that type.

An object’s type is also unchangeable and the type () function returns an object’s type. The value of some objects can change. Objects whose value can change are said to be “mutable”; objects whose value is unchangeable once they are created are called “immutable”. In the example below, object a has identity 31082544, type int, and value 5.

>>> a=5
>>> id (a)
31082544 
>>> type (a) 
<type 'int'>

Some objects contain references to other objects; these are called “containers”. Examples of containers are tuples, lists, and dictionaries. The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however, the container is still considered immutable because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value.

An object has an attribute(s), which are referenced using dotted expressions. For example, if an object ABC has an attribute PQ, then it would be referenced as ABC. PQ. In the following example, upper () is an attribute of var object.

>>> var= 'hello'
>>> var.upper()
'HELLO'

In the above example, upper () is a function on some object var, and this function is called “method”. More information on “method” is given in chapter 6.

Introduction to Python – Object Read More »

Python Interview Questions on Array Sequence

We have compiled most frequently asked Python Interview Questions which will help you with different expertise levels.

Python Interview Questions on Array Sequence

Low Level Arrays

Let’s try to understand how information is stored in low-level computer architecture in order to understand how array sequences work. Here is a small brush-up on computer science basics on memory units.

We know that the smallest unit of data in a computer is a bit (0 or 1). 8 bits together make a byte. A byte has 8 binary digits. Characters such as alphabets, numbers, or symbols are stored in bytes. A computer system memory has a huge number of bytes and tracking of how information is stored in these bytes is done with the help of a memory address. Every byte has a unique memory address which makes tracking information easier.

The following diagram depicts a lower-level computer memory. It shows a small section of memory of individual bytes with consecutive addresses.

Python Interview Questions on Array Sequence chapter 9 img 1

Computer system hardware is designed in such a manner that the main memory can easily access any byte in the system. The primary memory is located in the CPU itself and is known as RAM. Any byte irrespective of the address can be accessed easily. Every byte in memory can be stored or retrieved in constant time, hence its Big-O notation for the time complexity would be O(1).

There is a link between an identifier of a value and the memory address where it is stored, the programming language keeps a track of this association. So, a variable student_name may store name details for a student and class_ teacher would store the name of a class teacher. While programming, it is often required to keep a track of all related objects.

So, if you want to keep a track of scores in various subjects for a student, then it is a wise idea to group these values under one single name, assign each value an index and use the index to retrieve the desired value. This can be done with the help of Arrays. An array is nothing but a contiguous block of memory.

Python internally stores every Unicode character in 2 bytes. So, if you want to store a 5-letter word (let’s say ‘state’) in python, this is how it would get stored in memory:

Python Interview Questions on Array Sequence chapter 9 img 2

Since each Unicode character occupies 2 bytes, the word STATE is stored in 10 consecutive bytes in the memory. So, this is a case of an array of 5 characters. Every location of an array is referred to as a cell. Every array element is numbered and its position is called index. In other words, the index describes the location of an element.

Table

Every cell of an array must utilize the same number of bytes.

The actual address of the 1st element of the array is called the Base Address. Let’s say the name of the array mentioned above is my_array[ ]. The Base address of my_array[ ] is 1826. If we have this information it is easy to calculate the address of any element in the array.

Address of my array [index] = Base Address +( Storage size in bytes of one element in the array) * index.

Therefore, Address of my array[3] = 1826 + 2*3
= 1826 + 6
= 182C
After that, slight information on how things work at a lower level lets’s get back to the higher level of programming where the programmer is only concerned with the elements and index of the array.

Referential Array

We know that in an array, every cell must occupy the same number of bytes. Suppose we have to save string values for the food menu. The names can be of different lengths. In this case, we can try to save enough space considering the longest possible name that we can think of but that does not seem to be a wise thing to do like a lot of space is wasted in the process and you never know there may be a name longer than the value that we have catered for. A smarter solution, in this case, would be to use an array of object references.

Python Interview Questions on Array Sequence chapter 9 img 3

In this case, every element of the array is actually a reference to an object. The benefit of this is that every object which is of string value can be of different length but the addresses will occupy the same number of cells. This helps in maintaining the constant time factor of order O(1).

In Python, lists are referential in nature. They store pointers to addresses in the memory. Every memory address requires a space of 64-bits which is fixed.

Question 1.
You have a list integer list having integer values. What happens when you give the following command?
integer_list[1] + = 7
Answer:
In this case, the value of the integer at index 1 does not change rather we end up referring to space in the memory that stores the new value i.e. the sum of integer_list[1]+7.

Question 2.
State whether True or False:
A single list instance may include multiple references to the same object as elements of the list.
Answer:
True

Question 3.
Can a single object be an element of two or more lists?
Answer:
Yes

Question 4.
What happens when you compute a slice of the list?
Answer:
When you compute a slice of the list, a new list instance is created. This new list actually contains references to the same elements that were in the parent list.
For example:

>>> my_list = [1, 2,8,9, "cat", "bat", 18]
>>> slice_list = my_list[2:6]
>>> slice_list
[8, 9, 'cat' , 'bat' ]
>>>

This is shown in the following diagram:

Python Interview Questions on Array Sequence chapter 9 img 4

Question 5.
Suppose we change the value of the element at index 1 of the slice just to 18 (preceding diagram). How will you represent this in a diagram?
Answer:
When we say sliceJist[1]=18, we actually are changing the reference that earlier pointed to 9 to another reference that points to value 18. The actual integer object is not changed, only the reference is shifted from one location to the other.

Python Interview Questions on Array Sequence chapter 9 img 5

Deep Copy and Shallow Copy in Python

Python has a module named “copy” that allows deep copy or shallow copy mutable objects.

Assignment statements can be used to create a binding between a target and an object, however, they cannot be used for copying purposes.

Deep Copy

The copy module of python defines a deepcopy( ) function which allows the object to be copied into another object. Any changes made to the new object will not be reflected in the original object.

In the case of shallow copy, a reference of the object is copied into another object as a result of which changes made in the copy will be reflected in the parent copy. This is shown in the following code:

Python Interview Questions on Array Sequence chapter 9 img 6

Python Interview Questions on Array Sequence chapter 9 img 7

It is important to note here that shallow and deep copying functions should be used when dealing with objects that contain other objects (lists or class instances), A shallow copy will create a compound object and insert into it, the references the way they exist in the original object. A deep copy on the other hand creates a new compound and recursively inserts copies of the objects the way they exist in the original list.

Question 6.
What would be the result of the following statement? my list = [7]* 10?
Answer:
It would create a list named my list as follows:
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]

Python Interview Questions on Array Sequence chapter 9 img 8

All the 10 cells of the list my_list, refers to the same element which in this case is 10.

Question 7.
Have a look at the following piece of code:

>>> a = [1,2,3,4,5,6,7]
>>> b = a
>>>b [ 0 ] = 8
>>> b
[8, 2, 3, 4, 5, 6, 7]
>>> a
[8, 2, 3, 4, 5, 6, 7]
>>>

Here, we have used the assignment operator still on making changes to ‘b’ changes are reflected in ‘a’. Why?
Answer:
When you use an assignment operator you are just establishing a relationship between an object and the target. You are merely setting a reference to the variable. There are two solutions to this:

(I)

>>> a
[8,2,3, 4, 5, 6, 7]
>>>a = [ 1,2 3, 4,5 ,6, 7]
>>> b = a [:]
>>>b[0] = 9
>>> b
[9, 2, 3, 4, 5, 6, 7]
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>>

(II)

>>> a = [ 1,2 3, 4,5 6, 7]
>>> b = list (a)
>>>b
[1,2, 3, 4, 5, 6, 7]
>>>b [0] = 9
>>> b
[9, 2, 3, 4, 5, 6, 7]
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>>

Question 8.
Take a look at the following piece of code:

>>> import copy
>>> a = [1 9 9 4 5, 5]
>>> b = copy.copy(a)
>>> b
[l, 2, 3, 4, 5, 6]
>>>b [2] = 9
>>> b
[1.2, 9, 4, 5, 6]
>>> a
[1.2, 3, 4, 5, 6]
>>>

‘b’ is a shallow copy of ‘a’ however when we make changes to ‘b’ it is not reflected in ‘a’, why? How can this be resolved?
Answer:
List ‘a’ is a mutable object (list) that consists of immutable objects (integer).
A shallow copy would work with the list containing mutable objects.
You can use b=a to get the desired result.

Question 9.
Look at the following code:

>>> my_list = [["apples", "banana"], ["Rose", "Lotus"], ["Rice", "Wheat"]]
>>> copy_list = list (my_list)
>>> copy_list[2][0]="cereals"

What would happen to the content of my_list? Does it change or remains the same?
Answer:
Content of my_list will change:

>>> my list [['apples', 'banana'] , [ 'Rose', 'Lotus'],
['cereals', 'Wheat']]
>>>

Question 10.
Look at the following code:

>>> my_list = [ ["apples", "banana"], ["Rose", "Lotus"], ["Rice", "Wheat"]]
>>> copy_list = my_list.copy( )
>>> copy_list[2][0]="cereals"

What would happen to the content of my_list? Does it change or remains the same?
Answer:
Content of my_list would change:

>>> my_list
[ [ 'apples' , 'banana1' ] , [ 'Rose' , 'Lotus' ] ,
[ 'cereals', 'Wheat']]
>>>

Question 11.
When the base address of immutable objects is copied it is called_________________.
Answer:
Shallow copy

Question 12.
What happens when a nested list undergoes deep copy?
Answer:
When we create a deep copy of an object, copies of nested objects in the original object are recursively added to the new object. Thus, a deep copy will create a completely independent copy of not only of the object but also of its nested objects.

Question 13.
What happens when a nested list undergoes shallow copy?
Answer:
A shallow copy just copies references of nested objects therefore the copies of objects are not created.

Dynamic Arrays

As the name suggests dynamic array is a contiguous block of memory that grows dynamically when new data is inserted. It has the ability to adjust its size automatically as and when there is a requirement, as a result of which, we need not specify the size of the array at the time of allocation and later we can use it to store as many elements as we want.

When a new element is inserted in the array, if there is space then the element is added at the end else a new array is created that is double in size of the current array, so elements are moved from the old array to the new array and the old array is deleted in order to create some free memory space. The new element is then added at the end of the expanded array.

Let’s try to execute a small piece of code. This example is executed on 32-bit machine architecture. The result can be different from that of 64-bit but the logic remains the same.
For a 32-bit system 32-bits (i.e 4 bytes) are used to hold a memory address. So, now let’s try and understand how this works:
When we created a blank list structure, it occupies 36 bytes in size. Look at the code given as follows:

import sys
my_dynamic_list =[ ]
print("length = ",len(my_dynamic_list) , ".", "size in bytes = ", sys.getsizeof(my_dynamic_list),".")

Here we have imported the sys module so that we can make use of getsizeof( ) function to find the size, the list occupies in the memory.
The output is as follows:
Length = 0.
Size in bytes = 36 .
Now, suppose we have a list of only one element, let’s see how much size it occupies in the memory.

import sys
my_dynamic_list =[1]
print("length = ",len(my_dynamic_list),".", "size in bytes = ", sys.getsizeof(my_dynamic_list),".")
length = 1 . size in bytes = 40 .

This 36 byte is just the requirement of the list data structure on 32- bit architecture.
If the list has one element that means it contains one reference to memory and in a 32-bit system architecture memory, the address occupies 4 bytes. Therefore, the size of the list with one element is 36+4 = 40 bytes.
Now, let’s see what happens when we append to an empty list.

import sys
my_dynamic_list =[ ]
value = 0
for i in range(20):
print("i = ",i,".", "                                  Length of my_
dynamic_list = ",len(my_dynamic_list),".", " size in bytes = ", sys.getsizeof(my_dynamic_ list),".")
my_dynamic_list.append(value)
value +=1

Output
i = 0 .    Length of mydynamic list = 0 .      size in bytes =36
i = 1 .    Length of my_dynamic_list = 1 .    size in bytes = 52
i = 2 .    Length of my_dynamic list = 2 .    size in bytes = 52
i = 3 .    Length of my dynamic list = 3 .    size in bytes = 52
i = 4.     Length of my dynamic list = 4 .    size in bytes = 52
i= 5.      Length of my dynamic list = 5 .    size in bytes = 68
i = 6.     Length of my_dynamic_list = 6 .   size in bytes = 68
i = 7.     Length of my_dynamic_list = 7 .   size in bytes = 68
i = 8 .    Length of my_dynamic_list = 8 .   size in bytes = 68

i = 9.     Length of my_dynamic_list = 9 .   size in bytes = 100
i = 10.   Length of my dynamic list =10.    size in bytes = 100
i = 11 .  Length of my dynamic list =11.    size in bytes = 100
i = 12 .  Length of my_dynamic_list = 12 . size in bytes = 100
i = 13 .  Length of my dynamic list =13.    size in bytes = 100
i = 14.   Length of my_dynamic_list = 14 . size in bytes = 100
i = 15 .  Length of my dynamic list =15.    size in bytes = 100
i = 16.   Length of my dynamic list = 16.   size in bytes = 100
i = 17.   Length of my_dynamic_list =17.   size in bytes = 136
i = 18 .  Length of my dynamic list =18.    size in bytes = 136
i = 19.  Length of my_dynamic_list = 19.   size in bytes = 136

Now, lets have a look at how things worked here:

When you call an append( ) function for list, resizing takes place as per list_resize( ) function defined in Objects/listobject.c file in Python. The job of this function is to allocate cells proportional to the list size thereby making space for additional growth.
The growth pattern is : 0,4,8,16,25,35,46,58,72,88,

Amortization

Let’s suppose that there is a man called Andrew, who wants to start his own car repair shop and has a small garage. His business starts and he gets his first customer however, the garage has space only to keep one car. So, he can only have one car in his garage.

Python Interview Questions on Array Sequence chapter 9 img 9

Seeing a car in his garage, another person wants to give his car to him. Andrew, for the ease of his business, wants to keep all cars in one place. So, in order to keep two cars, he must look for space to keep two cars, move the old car to the new space and also move the new car to the new space and see how it works.

So, basically, he has to:

  1. Buy new space
  2. Sell the old space

Let’s say, this process takes one unit of time. Now, he also has to:

  1. Move the old car to a new location
  2. Move the new car to a new location Moving each car takes one unit of time.

Python Interview Questions on Array Sequence chapter 9 img 10

Andrew is new in the business. He does not know how his business would expand, also what is the right size for the garage. So, he comes up with the idea that if there is space in his garage then he would simply add the new car to space, and when the space is full he will get a new space twice as big as the present one and then move all cars there and get rid of old space. So, the moment he gets his new car, it’s time to buy a new space twice the old space and get rid of the old space.

Python Interview Questions on Array Sequence chapter 9 img 11

Now, when the fourth car arrives Andrew need not worry. He has space for the car.
Python Interview Questions on Array Sequence chapter 9 img 12
And now again when he gets the fifth car he will have to buy a space that is double the size of the space that he currently has and sell the old space.

So, let’s now take a look at the time complexity: Let’s analyze how much does it take to add a car to Andrew’s garage where there is n number of cars in the garage.

Here is what we have to see:

1. If there is space available, Andrew just has to move one car into a new space and that takes only one unit of time. This action is independent of the n (number of cars in the garage). Moving a car in a garage that has space is constant time i.e. O(1).

2. When there in spot and a new car arrives, Andrew has to do the following:

  • Buy a new space that takes 1 unit of time.
  • Move all cars into new space one by one and then move the new car into free space. Moving every car takes one unit of time.

So, if there were n cars already in the old garage, plus one car then that would take n+1 time units to move the car.
So, the total time taken in step two is 1+n+l and in Big O notation this would mean O(n) as the constant does not matter.

On the look of it, one may think this is too much of a task but every business plan should be correctly analyzed. Andrew will buy a new garage only if the space that he has, gets all filled up. On spreading out the cost over a period of time, one will realize that it takes a good amount of time only when the space is full but in a scenario where there is space, the addition of cars does not take much time.

Now keeping this example in mind we try to understand the amortization of dynamic arrays. We have already learned that in a dynamic array when the array is full and a new value has to be added, the contents of the array are moved on to the new array that is double in size, and then the space occupied by the old array is released.

 

Python Interview Questions on Array Sequence chapter 9 img 13

It may seem like the task of replacing the old array with a new one is likely to slow down the system. When the array is full, appending a new element may require O(n) time. However, once the new array has been created we can add new elements to the array in constant time O(1) till it has to be replaced again. We will now see that with the help of amortization analysis how this strategy is actually quite efficient.

As per the graph above, when there are two elements then on calling append, the array will have to double in size, same after 4th and 8th element. So, at 2, 4, 8, 16… append will be O(n) and for the rest of the cases, it will be 0(1).
The steps involved are as follows:

  1. When the array is full, allocate memory for the new array and the size of the new array should typically be twice the size of the old array.
  2. All contents from the old array should be copied to the new array.
  3. The space occupied by the old array should be released.

 

Python Interview Questions on Array Sequence chapter 9 img 14

The analysis would be as follows:
Python Interview Questions on Array Sequence chapter 9 img 15

Python Interview Questions on Array Sequence Read More »

Python Project Ideas & Topics for Beginners

Python Project Ideas & Topics for Beginners to Try in 2021 | Interesting Python Projects List for Beginners, Intermediate, Advanced Level Students

Python Project Ideas & Topics for Beginners: Python is currently one of the most popular languages for programming. It seems that in 2021 and beyond, this trend will continue. So if you are a Python novice, work on some Python project ideas in real-time is the most acceptable thing you can do.

In this article, you can get 42 best python project ideas for Python beginners to gain practical experience. In addition, project-based learning helps strengthen the understanding of students. Therefore, it is suitable for novices in programming knowledge, but not limited to them. First of all, however, we should address your more relevant question:

Why build Python Projects?

When it comes to software development professions, professionals are required to design their projects. However, developing real-world projects is the best method to improve your talents and transform your theory into practice.

If you work on live projects, it will help:

  • To build your trust – When you use essential tools and technology, you are confident about your abilities and recognize your weaknesses.
  • For experimentation – while working on a Python project, you will need to learn about new tools and technologies. The more you understand state-of-the-art development instruments, environments, libraries, the greater the possibility for experimenting. The more you try out new concepts from the python project, the more knowledge you get.
  • Knowing the nitty-gritty of SDLC – You will obtain a deeper grasp of how the software development cycle works by developing a project from the beginning. Over time, you will learn how to design, execute code, manage the testing process, fix errors, deploy code and occasionally upgrade your software product.
  • Master programming fundamentals – One of the most significant advantages of constructing real-world projects is that you master programming concepts and models in numerous languages with continual practice.

There are, therefore, a few Python Projects for startups here:

Python Project Ideas: Beginners Level

This list is suitable for students, starting with Python or data science in general, and includes a list of python project ideas for students. These concepts for the Python project lead you to all of the practicalities that your career as a Python developer needs to succeed.

In addition, this list should start you began if you are seeking ideas for a Python project for a final year. Therefore, let’s get directly into some suggestions for the project by Python, which will reinforce your base and enable you to climb the ladder.

1. Mad Libs Generator

The Mad Libs Generator is one of the best ideas for testing hands-on python projects for kids. It is an excellent project for novices with the development of software. This project will teach you how to modify user-input data, mainly focusing on strings, variables, and concaténation. The design of the program requires users to provide several inputs, which are regarded as Mad Lib. Mab lib is one of the beginners’ python projects.

Either an adjective, a noun, and a pronoun can be the input. Finally, the request takes the data and arranges the entrances into the tale template form when all the information has been entered. Okay, sound fun?

2. Number Guessing

It is still an exciting one of the easiest python projects. Even a little game, you can call it. Make a software in which your computer chooses a number randomly from 1 to 10, from 1 to 100, or any range. Then provide users an indication to estimate the quantity. Whenever the user conjectures incorrectly, he gains another clue, and his score falls. There can be several, divided, larger or smaller clues, or all of them together.

Functions are also required to compare the input number with the estimated number, calculate the difference between both, and check whether an actual number has been input in this project.

3. Text-based Adventure Game

This is a basic version of the game Adventure. It is entirely based on text. This game version enables users to walk among many rooms in a single setting and provides descriptions for each place based on user input. It is one of the most intriguing projects in Python.

The direction of movement is critical here – you must establish walls and give instructions for users to move around, place limitations on mobility, and include a tracker that can measure the distance a user has gone or carried in a game. Again, mentioning work from Python can help your CV appear far more intriguing.

4. Dice Rolling Simulator

We shall imitate rolling dice, as the name of the application suggests. It is one of the exciting python projects and generates a random number every time the application runs, and users can repeatedly use the dice as long as they like. The program generates a random number between 1 and 6 when the user rolls the dices (as on a standard dice).

The number is then shown to the user. It will also ask users to roll the dice again. The Application should also contain a function that can retrieve and display a number randomly between 1 and 6. These Python projects at the introductory level will help establish a solid basis for basic concepts for programming.

5. Hangman

It’s a “Think Word” game more. Difference: random, integer, string, char, input/output, and boolean are the fundamental principles for this project. In the game, participants must enter letter assumptions and the number of assumptions for each user (a counter variable is needed for limiting the guesses). It is one of Python’s earliest exciting works.

A re-organized list of words from which users can collect words can be created. However, you also need specific functions to verify whether a user enters a single letter or whether the entry letter is in the magic word, whether the user enters a single letter, and print the relevant results (letters).

6. Contact Book

This is one of the fantastic crafts for beginners. Each person utilizes a contact book to save contact information, names, addresses, telephone numbers, and email addresses included. It is a project on a command-line, where you will design a contact book application for saving and finding user contact details. Users should also be able to change contact data, remove contacts, and list stored contacts. The SQLite database is the perfect contacts storage platform. To manage a Python project for beginners, building your career with a good beginning can help.

7. Email Slicer

This is one of the most convenient and valuable Python projects in the future. The Application helps you retrieve the email address for the username and domain name. This information can also be customized for the app and sent to the host.

8. Binary search algorithm

You have ever heard the saying that you “find a needle in a haystack.” This program can be done using a binary algorithm for search. A list of random numbers can be created between 0 to 100, with a difference between two of the following values.

The application checks if the number is contained in the list when the user inputs a random number. It does so by the creation of the list of two halves. If the program discovers the number in the first half of the list, the other half will be removed and vice versa. The search continues until the user’s user number is found or the subfield size is 0 (this means that the number is not in the list). This python project idea is designed to help you construct an algorithm to find an item in a list.

9. Desktop Notifier App

Did you ever wonder how notifications function? That idea will shed some light on my little Python project. The desktop notifier applications run on your machine and provide you with information in a specified time interval. Therefore, we advise that you create such a program with libraries like notifications, requests, etc.

10. Python Story Generator

It is a pleasant yet thrilling python project that works wonderfully with children. In short, the computer will ask for inputs from users, such as a place name, action, etc., and will then create a tale about the data. The tale will always be the same, although the input varies slightly.

11. YouTube video downloader

A YouTube video downloader is the most fantastic way to begin experimenting with practical projects for pupils. Every month over a billion people watch YouTube. Sometimes we want to download videos permanently. Unfortunately, YouTube doesn’t provide you with this choice. Still, you can design an application with a simple user interface that allows you to download YouTube videos in various formats and video quality. It is a tough assignment, but when you start working on it, it is straightforward.

12. Python Website Blocker

Many undesirable websites continue to appear as we visit the Internet. It is one of the handy projects of Python in which you can create software that prevents opening particular websites. This program is suitable for students who wish to concentrate without interruptions in social media. The python project can assist your curriculum vitae to look a lot more fascinating than others.

13. Spin a Yarn

Things become more exciting here because strings at first seem endlessly more complicated to play with.

The Application initially requires the user to input a series. These may be an adjective, a preparation, an appropriate substantive, etc. Once all the information is present, they are placed using concatenation in a predefined tale template. Finally, the whole story has been printed to read a misconceived insanity!

14. What’s the word?

This name emphasizes that the user has to devise the generated term at random. However, you can establish a list from which the term should be conjectured, and you can also limit the number of conjectures permitted.

You may then write your own rules! Whether the printed alphabet is in this specific location can be indicated when the user enters the word. You need to check whether the user has entered alphabets or numbers and to display error warnings correctly.

15. Rock, Paper, Scissors

If you’re bored of not having your gameplay, you will enhance your mood by a 5-minute session of rock, paper, scissors, and computer.

We’re using the random feature here again. First, you move, and then you make the program. You can either use an alphabet or use a whole string to indicate the motion. To check the authenticity of the move, a function must be configured.

The winner of the round is decided to utilize another function. Either you can choose to play again, or you can select in advance a certain amount of moves. It also needs to develop a scoring function to return the winner at the end.

16. Leap it!

You enter one year in this python project and check whether or not it’s a springtime year. To do this, you must develop a function that acknowledges a leap year pattern and can try to match the year you are entering. Finally, you can use a Boolean expression to print out the result.

17. Find out, Fibonacci!

You provide a number, and the constructed function checks if the number is part of the sequence Fibonacci or not. The work underpinning the above ‘read it!’ program is comparable.

One common issue in all the initiatives mentioned above is that they will help you correct your basics. You are the bug fixer and the developer. Not to mention: Working with a variety of functions, working with variables, strings, integers, operators, etc., closes. Just as 2 + 2 is the basis of your understanding of mathematics, these principles will help you comprehend and maintain them in a fun way through doing projects.

These are some of Python’s most straightforward ideas for projects to work on. So let’s continue to the next level once you finish them.

Check Out Other Project Ideas:

Python Project Ideas: Intermediate Level

18. Calculator

However, creating your graphical UI calculator does not make you acquainted with a library like Tkinter that allows you to create buttons for different operations and display results on a screen.

19. Countdown Clock and Timer

It is a second utility application for the user to set a timer, and when the time is up, the Application tells you.

20. Random Password Generator

It’s a strenuous effort to create a strong password and remember it. Instead, you can construct a program that uses some words and then uses those words to create a random password. Then, by using the phrase the user submitted as input, you can remember the password.

21. Random Wikipedia Article

It is a sophisticated yet uncomplicated program. First, Wikipedia is searched for, and a random article is selected. Then the user asks whether or not he wishes to read the paper. If yes, the material will be displayed; else, a random report will be presented.

22. Reddit Bot

It is an excellent idea for novices in the python project. Reddit is a convenient platform, and as many individuals as possible desire to be online. Whenever they uncover something helpful, you can create a bot that monitors subsections and reports. It saves Redditors a great deal of time and offers important information.

23. Python Command-Line Application

Python is known for developing excellent programs on the command line. For example, you can set up your Application to send emails to others. The program will then request your credentials and email content and send the information via your created control line.

24. Alarm Clock

This is one of the python ideas for the project. People throughout the world are using alarm clock apps. The Application for an intermediate-level developer is relatively easy, Command Line Interface (CLI). This project, however, is not your alarm clock run-of-the-mill. You can enter YouTube links to a text file in this program and create the Application to read the file. Then, if you set a specific alarm clock time, a random YouTube link will be selected from the text file, and the YouTube video will be played.

25. Tic-Tac-Toe

We all have good recollections of Tic-Tac-Toe playing with our schoolmates, right? It is one of the funniest games anyplace you can play — a pen and paper is everything you need! Two people can usually play Tic-Tac-Toe simultaneously. The participants are building a grid of 3 tonnes. This is one of the most incredible concepts in Python.

As the first player puts “X” in one of the squares, the second one places an “O” in every square. This process will continue until every player places X and O alternatively. The player with three consecutive X or O on the grid successfully creates horizontal, vertical, or diagonal wins.

To develop this project, you can utilize the Pygame library. All the modules for computer graphics and sound are loaded into Pygame.

26. Steganography

The art of hiding a secret message in a different kind of media is Steganography, hiding an encoded message in a picture or video, for example. You may design a program that secures communications for you in images.

27. Currency converter

You may construct this simple GUI application with Python. As you assume with the name, you’re creating a currency translator that can convert currencies, such as the Indian rupee, into a pound or euro from one unit to another.

The design of this Application will be simple — the primary function should be to convert monetary units from one to the next. The standard Tkinter interface to the Tk GUI toolkit included with Python can be used in conjunction with Tkinter.

28. Post-it Notes

Post-it notes are a great technique to record simple tasks in order not to forget them. We are going to make the actual adhesive post-it notes on this project virtual. The main aim is to allow users to carry their post-it notes wherever they go (since it is on a digital platform).

The program should be able to create an account, create alternative layouts for post-it notes, and classify users’ messages. For this project, you can choose Django, as it offers an integrated user authentication function.

29. Site Connectivity Checker

The site connectivity checker works by accessing the URL and displaying the URL status, whether it is a live URL or not. In general, site connectivity inspectors regularly visit URLs and deliver the results. It works in the same way – the live status of URLs will be checked. One of the intriguing python projects for beginners is the website connectivity monitor.

You have to create the code from scratch for this Application. You can choose between TCP or ICMP for your connections. To add instructions that enable users to add and delete URLs in the list of URLs that they want to verify, you can use click, docopt, or argparse frames.

30. Directory Tree Generator

You may visualize the relationship between files and directories using a directory tree generator, which helps you comprehend the file and directory location. You can use the os library to list the files and folders in a particular directory for this project. Again, the frameworks of docopt or argparse are ideal instruments for the job.

These are some suggestions of Python intermediate projects that you can work on. If you still want to test and undertake demanding assignments.

Refer to our Python Programming Examples with Output Page to get different python project ideas with source code and create one on your own for your academic projects.

Python Project Ideas: Advanced Level

31. Speed Typing Test

Let’s start up ideas for beginners for expert Python projects. Remember the old test taping game that Windows XP and previously used? Similar software can be developed to test your typing speed. First, you have to use a library like Tkinter to develop a UI. Then create a fun typing test that shows user speed, precision, and words at the end of the day. Source code for the software can also be found online.

32. Content Aggregator

Websites, articles, and information are provided on the Internet. It is difficult to get through each of them if we want to find something. You may construct a content aggregator to search famous websites automatically and search for relevant content. The aggregator then meets all the content and enables the user to choose the stuff they are interested in. It’s pretty Google-like, yet impartial. And for your next Python project, this is a perfect idea!

33. Bulk File Rename/ Image Resize Application

This is a sophisticated project that you require in machine education to be well trained. We’ll first teach the software how to pre-process data and then carry out several tasks to resize and rename photographs. As the program begins to learn, bulk functions can be handled at once.

34. Python File Explorer

It is an important project because it tests your knowledge of Python’s different concepts. You need to design an app that anybody uses to scan your system files. Functions like searching and copying can also be added. Tkinter is a practical choice for this project since it makes it easy and quick to construct GUI applications.

To use Tkinter, you must import the file dialogue module from Tkinter to create the Python File Explorer. This module is intended to open and save files and folders.

35. Plagiarism Checker

The writing of content is one of the most prolific web companies. Unfortunately, a free tool for verifying plagiarism in documents is not available on the market. However, the search API may be used to develop software that scans the top few pages of Google and tests for plagiarism utilizing a natural language processing library.

36. Web Crawler

A web crawler is an automatic software script that navigates the Internet and archives the website material. One of the most useful python applications to get current information is a web crawler. For this type of program, you must employ a multi-thread notion. You can construct the Crawler Bot using Python Request Module or utilize Scrapy. It is an explicitly built web crawling open-source framework for Python to scavenge web data and extract it using APIs.

37. Music Player

Everybody likes music; you may also make the Application for your music player. Besides playing music, your program may explore and search for music in your file folders. It is an interactive interface that regular people may utilize as one of Python’s creative projects you could confront.

The software has a nice UI to explore track, increase/decrease volume, display the song name, artist, and album. The primary purpose of this project is to include Python programming, management of the base, building of algorithms, and processing of data.

38. Price Comparison Extension

This may be a valuable idea for a project python. You can design a program like Trivago that looks for a product’s pricing on some great sites and then shows you the most excellent bargain. It’s a practical program, as many companies have launched this tiny program. This extension can be used for foodstuffs, paperwork, etc.

39. Expense Tracker

As the name suggests, a cost tracker is a software tool that allows you to track your cost and evaluate costs. You will develop a simple expense tracker in this python project to keep track of the user’s cost.

Expenditure trackers are one of the trending python projects, which should also be able to analyze the statistics to provide users with precise insights into their costs to plan their costs better. To build an interface for this Application, you can utilize PySimpleGUI, and even modules from Python such as Pandas and Matplotlib can be great project tools.

40. Regex Query Tool

The intended results for specific queries often are lacking in regular search tools. In such cases, a Regex query tool is necessary. In simple words, a regex is a series of strings so that you check the validity of your query while entering a question in this tool.

If the regex can match patterns in the user’s text query, it informs them of all the matched patterns. A Regex Query Tool is one of the trendier projects that allow users to instantly monitor their regex strings’ validity and search process on the Web. The re Library from Python is the ideal instrument for the user-input text to execute query strings.

41. Instagram Photo Downloader

This Application would download all your friends’ Instagram photographs automatically. Since Instagram grows daily, it is one of the most helpful python projects and comparable to the preceding command-line app because it uses your account credentials and searches for the ID and photo of your friend. Therefore, this program is helpful if users wish to eliminate pages and merely save pictures.

42. Quiz Application

This is one of the exciting concepts to develop in the Python project. It is a standard quiz that gives several questions to users (a questionnaire) properly curated and enables them to answer the same questionnaire, and shows the correct answer if they are wrong. The ultimate score of the user is shown for each test. The Application has an option to create an account, where certain users can be named as admins.

These administrators can produce tests for other users. The trials and tests are updated in this way. To record all users’ questions, responses, and scores, this Application needs a database. Further features like timers for testing can also be included.

Which Project Platform Should You Choose?

You may wonder which platform for your Python project you should use. Your software projects must be developed on a particular platform so that others (mainly those without technical know-how) can utilize your product as well. Three primary platforms are available for developers to build python projects – Web, desktop, and control lines.

1. Web

Software projects can be executed on the Web, Web applications. Moreover, anyone with an internet connection can access Web applications on any device – they do not have to be individually downloaded. Therefore, the Web is the perfect platform for such applications to create a software product for public usage.

Web apps are complex, backend and front end tasks. While the backend refers to your program’s business logic for manipulating and storing information, the front end refers to your application user interface – the part you can see and interact with. In addition, you must understand techniques like JavaScript, HTML, and CSS to keep the backend as the center for your Web application.

But if you are working with Python, all of your development demands can be met. The Python library eliminates JavaScript, HTML, and CSS – unnecessary requirements. Besides, there are many additional web frameworks of Python such as Django, Flask, Web2Py, CherryPy, and Pylons.

2. Desktop GUI

As people around the globe extensively utilize desktop apps, the construction of a desktop application for fresh and middle-sized Python developers is a terrific project concept. The best thing about developing desktop GUIs is that you don’t need to master front-end technologies. For desktop applications, Python is all you will need.

Python is supplied with several desktop build frameworks. Although PySimpleGUI is a user-friendly Python framework, PyQt5 is one of Python’s complex GUIs.

Once a GUI has been developed for the desktop, you may compile this into an executable code for the OS you want to operate on, even compatible with all three major operating systems (Linux, Windows, or macOS).

3. Command-Line

Command-line Application is an app/program that depends entirely on the terminal/shell for user interaction. These applications work in a window of the console. As such, consumers can’t view any visuals or visual interface. Therefore, you need to enter specific commands to utilize command-line apps – the Application also provides the output via ASCII, while users can enter their input (commands) via ASCII characters. This is one of the most recent python projects.

Controller applications naturally require a certain level of technical command know-how. While not as easy to use as online or desktop programs, common-line applications are sturdy and robust. Python’s controls include a range of helpful frameworks, including click, docopt, place, cliff, and fire.

Conclusion

We explored 42 ideas of Python’s project in this article. We began with several starting projects that you may quickly resolve. Once the simple python projects have finished, you are encouraged to return, learn some new ideas, and test the intermediate projects. You can then deal with advanced projects when you feel confident. You need to take these concepts from the Python project into account if you would like to improve your python skills. So go now and try all the knowledge you collected with our python project guide to developing your project

Python Project Ideas & Topics for Beginners to Try in 2021 | Interesting Python Projects List for Beginners, Intermediate, Advanced Level Students Read More »

Python Data Persistence – RDBMS Concepts

Python Data Persistence – RDBMS Concepts

The previous chapter discussed various tools offered by Python for data persistence. While the built-in file object can perform basic read/write operations with a disk file, other built-in modules such as pickle and shelve enable storage and retrieval of serialized data to/from disk files. We also explored Python libraries that handle well-known data storage formats like CSV, JSON, and XML.

Drawbacks of Flat File

However, files created using the above libraries are flat. They are hardly useful when it comes to real-time, random access, and in-place updates. Also, files are largely unstructured. Although CSV files do have a field header, the comma-delimited nature of data makes it very difficult to modify the contents of a certain field in a particular row. The only alternative that remains, is to read the file in a Python object such as a dictionary, manipulate its contents, and rewrite it after truncating the file. This approach is not feasible especially for large files as it may become time-consuming and cumbersome.

Even if we keep this issue of in-place modification of files aside for a while, there is another problem of providing concurrent r/w access to multiple applications. This may be required in the client-server environment. None of the persistence libraries of Python have built-in support for the asynchronous handling of files. If required, we have to rely upon the locking features of the operating system itself.

Another problem that may arise is that of data redundancy and inconsistency. This arises primarily out of the unstructured nature of data files. The term ‘redundancy’ refers to the repetition of the same data more than one time while describing the collection of records in a file. The first row of a typical CSV file defines the column headings, often called fields and subsequent rows are records.
Following table 7.1 shows a ‘pricelistxsv’ represented in the form of a table. Popular word processors (MS Word, OpenOffice Writer) and spreadsheet programs (MS Excel, OpenOffice Calc) have this feature of converting text delimited by comma or any other character to a table.

NoCustomer NameProductPriceQuantityTotal
1RavikumarLaptop25000250000
2JohnTV40000140000
3DivyaLaptop25000125000
4DivyaMobile15000345000
5JohnMobile15000230000
6Ravi KumarTV40000140000

As we can see, data items such as customer’s name, product’s name, and price are appearing repeatedly in the rows. This can lead to two issues: One, a manual error such as spelling or maintaining correct upper/lower case can creep up. Secondly, a change in the value of a certain data item needs to reflect at its all occurrences, failing which may lead to a discrepancy. For example, if the price of a TV goes up to 45000, the price and total columns in invoice numbers 2 and 6 should be updated. Otherwise, there will be inconsistency in the further processing of data. These problems can be overcome by using a relational database.

SQLite installation

Installation of SQLite is simple and straightforward. It doesn’t need any elaborate installation. The entire application is a self-contained executable ‘sqlite3.exe’. The official website of SQLite, (https://sqIite.org/download.html) provides pre-compiled binaries for various operating system platforms containing the command line shell bundled with other utilities. All you have to do is download a zip archive of SQLite command-line tools, unzip to a suitable location and invoke sqlite3.exe from DOS prompt by putting the name of the database you want to open.

If already existing, the SqLite3 database engine will connect to it; otherwise, a new database will be created. If the name is omitted, an in-memory transient database will open. Let us ask SQLite to open a new mydatabase.sqlite3.

E:\SQLite>sqlite3 mydatabase.sqlite3 
SQLite version 3.25.1 2018-09-18 20:20:44 
Enter ".help" for usage hints. 
sqlite>

In the command window, a SQLite prompt appears before which any SQL query can be executed. In addition, there “dot commands” (beginning with a dot “.”) typically used to change the output format of queries or to execute certain prepackaged query statements.
An existing database can also be opened using the .open command.

E:\SQLite>sqlite3
SQLite version 3.25.1 2018-09-18 20:20:44 
Enter ".help" for usage hints.
Connected to a transient in-memory database. 
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open test.db

The first step is to create a table in the database. As mentioned above, we need to define its structure specifying the name of the column and its data type.

SQUte Data Types

ANSI SQL defines generic data types, which are implemented by various RDBMS products with a few variations on their own. Most of the SQL database engines (Oracle, MySQL, SQL Server, etc.) use static typing. SQLite, on the other hand, uses a more general dynamic type system. Each value stored in SQLite database (or manipulated by the database engine) has one of the following storage classes:

  • NULL
  • INTEGER
  • REAL
  • TEXT
  • BLOB

A storage class is more general than a datatype. These storage classes are mapped to standard SQL data types. For example, INTEGER in SQLite has a type affinity with all integer types such as int, smallint, bigint, tinyint, etc. Similarly REAL in SQLite has a type affinity with float and double data type. Standard SQL data types such as varchar, char, char, etc. are equivalent to TEXT in SQLite.
SQL as a language consists of many declarative statements that perform various operations on databases and tables. These statements are popularly called queries. CREATE TABLE query defines table structure using the above data types.

CREATE TABLE

This statement is used to create a new table, specifying the following details:

  • Name of the new table
  • Names of columns (fields) in the desired table
  • Type, width, and the default value of each column.
  • Optional constraints on columns (PRIMARY KEY, NOT NULL, FOREIGN KEY)

Example

CREATE TABLE table_name ( 
column 1 datatype [width] [default] [constraint], 
column2 , 
column3..., 
 ) ;

DELETE Statement

If you need to remove one or more records from a certain table, use the DELETE statement. The general syntax of the DELETE query is as under:

Example

DELETE FROM table_name WHERE [condition];

In most circumstances, the WHERE clause should be specified unless you intend to remove all records from the table. The following statement will remove those records from the Invoices table having Quantity>5.

sqlite> select customers.name, products. name, the quantity from invoices, customers, products
...> where invoices.productID=Products.ProductID ...> and invoices.CustID=Customers.CustID;
Name               Name          Quantity
---------           ---------        ---------
Ravikumar        Laptop              2
Divya                Mobile              3
Ravikumar        Mobile              1
John                 Printer               3
Patel                 Laptop             4
Irfan                 Printer              2
Nit in Scanner 3

ALTER TABLE statement

On many occasions, you may want to make changes in a table’s structure. This can be done by the ALTER TABLE statement. It is possible to change the name of a table or a column or add a new column to the table.

The following statement adds a new column in the ‘Customers’ table:

sqlite> alter table customers add column address text (20);
sqlite> .schema customers CREATE TABLE Customers (
CustID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT (20),
GSTIN TEXT (15), address text (20));

DROP TABLE Statement

This statement will remove the specified table from the database. If you try to drop a non-existing table, the SQLJte engine shows an error.
sqlite> drop table invoices; sqlite> drop table employees; Error: no such table: employees
When the ‘IF EXISTS’ option is used, the named table will be deleted only if exists and the statement will be ignored if it doesn’t exist.
sqlite> drop table if exists employees;

SQLiteStudio

SQLiteStudio is an open-source software from https://sqlitestudio.pl. It is portable, which means it can be directly run without having to install. It is powerful, fast, and yet very light. You can perform CRUD operations on a database using GUI as well as by writing SQL queries.

Download and unpack the zip archive of the latest version for Windows from the downloads page. Run SQLiteStudio.exe to launch the SqliteStudio. Its opening GUI appears as follows:(figure 7.3)

Python Data Presistence - RDBMS Concepts chapter 1 img 1

Currently attached databases appear as expandable nodes in the left column. Click any one to select and the ‘Tables’ sub-node shows tables in the selected database. On the right, there is a tabbed pane. The first active tab shows structure of the selected table and the second tab shows its data. The structure, as well as data, can be modified. Right-click on the Tables subnode on the left or use the Structure menu to add a new table. User-friendly buttons are provided in the Structure tab and data tab to insert/ modify column/row, commit, or rollback transactions.

This concludes the current chapter on RDBMS concepts with a focus on the SQLite database. As mentioned in the beginning, this is not a complete tutorial on SQLite but a quick hands-on experience of interacting with SQLite database to understand Python’s interaction with databases with DB-API that is the subject of the next chapter.

 

Python Data Persistence – RDBMS Concepts Read More »

Data Persistence – Python – PyMongo

Python Data Persistence – Python – PyMongo

is NOSQL?

In today’s era of real-time web applications, NoSQL databases are becoming increasingly popular. The term NoSQL originally referred to “non SQL” or “non-relational”, but its supporters prefer to call it “Not only SQL” to indicate that SQL-like query language may be supported alongside.

NoSQL is touted as open-source, distributed, and horizontally scalable schema-free database architecture. NoSQL databases are more scalable and provide superior performance as compared to RDBMS. This primarily because it requires that schemas (table structure, relationships, etc.) be defined before you can add data. Some of the popular NoSQL databases extensively in use today include MongoDB, CouchDB, Cassandra, HBase, etc.

Several NoSQL products are available in the market. They are classified into four categories based on the data model used by them.

Key-Value store: Uses a hash table (also called a dictionary). Each item in the database is stored as a unique attribute name (or ‘key’), associated with its value. The key-value model is the simplest type of NoSQL database. Examples of key-value databases are Amazon SimpleDB, Oracle BDB, Riak, and Berkeley DB.

Column Oriented: This type of databases store and process very large amount of data distributed over multiple machines. Keys point to multiple columns. The columns are arranged by column family. Examples of column-oriented databases are Cassandra and HBase.

Document oriented: Database of this category is an advanced key-value store database. The semi-structured documents are stored in formats like JSON. Documents can contain many different key-value pairs, or key- array pairs, or even nested documents.

Graph Based: Databases of this type are used to store information about networks of data, such as social connections. A flexible graph model can scale across multiple machines. Graph stores include Neo4J and Giraph.
In this chapter, we shall get acquainted with a hugely popular document-oriented database, MongoDB, and how it can be interfaced with Python through the PyMongo module.

MongoDB

NoSQL databases typically have a huge amount of data. Hence, more often than not, the power of a single CPU is not enough when it comes to fetching data corresponding to a query. MongoDB uses a sharding technique that splits data sets across multiple instances. A large collection of data is split across multiple physical servers called ‘shards’, even though they behave as one collection. Query request from the application is routed to the appropriate shard and the result is served. Thus, MongoDB achieves horizontal scalability, (figure 11.1)

Python Data Presistence - Python - PyMongo chapter 11 img 1

The Document is the heart of a MongoDB database. It is a collection of key-value pairs – similar to Python’s dictionary object. We can also think of it being similar to a single row in a table of SQL-based relational databases.

Collection in MongoDB is analogous to a table in the relational database. However, it doesn’t have a predefined schema. The Collection has a dynamic schema in the sense each document may of a variable number of k-v pairs not necessarily with the same keys in each document.

Each document is characterized by a special key called “_id” having a unique value, again similar to a primary key in the entity table of a relational database.

MongoDB server has a command-line interface from inside which different database operations can be performed.

MongoDB – Create Database

To display the current database in use, there’s a DB command. The default database in-use tests.

> db 
Test

With the ‘use’ command any other database is set as current. If the named database doesn’t exist, a new one is created.

> use mydb 
switched to db mydb

However, until you store data(such as collection or document) in it, is the database is not created. The following command inserts a document in the ‘products’ collection under the current database.

MongoDB – Insert Document

Appropriately, insertone ( ) method is available to a collection object in a database. A document is provided to it as a parameter.

> db.products.insertone({"ProductID":1, 
"Name":"Laptop", "Price":25000}) 
WriteResult({ "nlnserted" : 1 })

Result of above (for that matter any insert/update/delete operation) command returns WriteResult object. The insert () method inserts multiple documents if the argument is an array of documents. In that case, the result is BulkWriteResult object.

> var pricelist=[{'productID' :1, 'Name': 'Laptop' , 'price':25000}, 
. . . { 'productID' : 2, 'Name":'TV' , 'price':2000}, 
. . . { 'productID' : 3, 'Name":'Router' , 'price':2000}, 
. . . { 'productID' : 4, 'Name":'Scanner' , 'price':5000}, 
. . . { 'productID' : 5, 'Name":'Printer' , 'price':9000}, 
> db.products.insert(pricelist); 
BulkWriteResult ({
 "writeErrors" : [ ] , 
 "writeConcernErrors" : [ ] , 
 "nInserted" : 5 , 
 "nUpserted" : 0 , 
 "nMatched" : 0 , 
 "nModified" : 0 , 
 "nRemoved" : 0 , 
 "upserted" : [ ] 
 })

The insert () function inserts a single document or array whereas a single document is inserted with inserOne ( ) method and array whereas the insert_many () method is used with an array.

MongoDB – Delete Document

The remove () method deletes one or more documents from the collection based on the provided criteria. The following statement will result in the removal of a document pertaining to price>40000 (in our data it happens to be with name=’TV’).

> db.products.remove({"price":{$gt:40000}}) 
WriteResult({ "nRemoved" : 1 })

Run the find ( ) method in the shell to verify the removal.

Now that, we have attained some level of familiarity with MongoDB with the help of shell commands. Let us concentrate on our main objective – use MongoDB in Python.

PyMongo – Update Document

PyMongo offers two collection methods for the modification of data in one or more documents. They are update_one () and update_many () . Both require filter criteria and a new value of one or more keys. The update_one () updates only the first document that satisfies filter criteria. On the other hand, update__many () performs updates on all documents that satisfy the filter criteria.

collection.update_one (filter, newval)

Following Python script accepts the name of the product from the user and displays the current price. It is updated to the new price input by the user.

Example

> var pricelist=[{'productID' :1, 'Name': 'Laptop' , 'price':25000}, 
. . . { 'productID' : 2, 'Name":'TV' , 'price':2000}, 
. . . { 'productID' : 3, 'Name":'Router' , 'price':2000}, 
. . . { 'productID' : 4, 'Name":'Scanner' , 'price':5000}, 
. . . { 'productID' : 5, 'Name":'Printer' , 'price':9000}, 
> db.products.insert(pricelist); 
BulkWriteResult ({
 "writeErrors" : [ ] , 
 "writeConcernErrors" : [ ] , 
 "nInserted" : 5 , 
 "nUpserted" : 0 , 
 "nMatched" : 0 , 
 "nModified" : 0 , 
 "nRemoved" : 0 , 
 "upserted" : [ ] 
 })

Python Data Persistence – pyMongo – Querying Collection

PyMongo module defines find( ) method to be used with a collection object. It returns a cursor object which provides a list of all documents in the collection.

>>> products=db['products']
>>> docs=products .find ()
>>> list(docs)
[{'_id' : Objectld('5c8dec275405cl2e3402423c'),
'ProductID': 1, 'Name': 'Laptop', 'price': 25000},
{'_id': ObjectId('5c8dec275405cl2e3402423d'),
'ProductID': 2, 'Name': 'TV', 'price': 50000},
{'_id': Objectld('5c8dec275405cl2e3402423e'),
'ProductID': 3, 'Name': 'Router', 'price': 2000},
{'_id': Objectld('5c8dec275405cl2e3402423f'),
'ProductID': 4, 'Name': 'Scanner', 'price': 5000},
{'_id': Objectld('5c8dec275405cl2e34024240'),
'ProductID': 5, 'Name': 'Printer', 'price': 9000}]

This cursor object is an iterator that serves one document for every call of the next () method. Each document is a dictionary object of k-v pairs. The following code displays the name and GSTIN of all customers.

Example

#mongofind. py 
from pymongo import MongoClient 
client=MongoClient( ) 
db=client.newdb 
cust=db['customers'] 
docs=cust. find ( ) 
while True:               
 try:                          
         doc=docs.next()                          
         print (doc['Name'], doc['GSTIN'])              
         except Stoplteration:                           
         break 
 client.close ( )

Run the above script from the command prompt.

E : \python3 7 >python mongofind. py
Ravikumar 27AAJPL7103N1ZF
Patel 24ASDFG1234N1ZN
Nitin 27AABBC7895N1ZT
Nair 32MMAF8963N1ZK
Shah 24BADEF2002N1ZB
Khurana 07KABCS1002N1ZV
Irfan 05IIAAV5103N1ZA
Kiran 12PPSDF22431ZC
Divya 15ABCDE1101N1ZA
John 29AAEEC4258E1ZK

You can, of course, employ a regular ‘for’ loop to traverse the cursor object to obtain one document at a time.

Example

for doc in docs: 
      print (doc['Name'] , doc[1GSTIN'])

The logical operators of MongoDB (described earlier in this chapter) are used to apply filter criteria for the find ( ) method. As an example, products with price>10000 are fetched with the following statement:

Example

>>> products=db['products'] 
>>> docs=products .find 
({'price :{ 1$gt ' :10000} } ) 
>>> for 
doc in docs: print (doc.get('Name'), 
doc.get('price')) Laptop 25000 TV 50000

 

Data Persistence – Python – PyMongo Read More »

Data Persistence – Python and Excel

Python Data Persistence – Python and Excel

Interactive spreadsheet applications have been in use for quite a along. Over the years, Microsoft Excel has emerged as the market leader in this software category. Early versions of MS Excel used a proprietary data representation format. After 2003, Microsoft has adopted Office Open XML (OOXML) file format for its later versions of MS Excel.

OOXML is an ECMA standard file format. This has led to the development of programming interfaces for Excel in many programming environments including Python. Out of many Python packages, openpyxl is the most popular and can read/write Excel files with the .xlsx extension. Pandas is a very popular Python library. It has various tools for performing the analysis of data stored in different file formats. Data from Excel sheet, CSV file, or SQL table can be imported into Pandas data frame object for processing.

In this chapter, first, we shall learn to use openpyxl to programmatically perform various operations on an Excel file such as copying a range, define, and copy formulas, insert images, create charts, and so on.

In the second part of this chapter, we get acquainted with Pandas library – specifically, how to read/write data from/to an Excel worksheet.
Before proceeding further, here is a brief recap of important terms used in the Excel spreadsheet. An Excel document is called a workbook and is saved as a .xlsx file. A workbook may have multiple worksheets. Each worksheet is a grid of a large number of cells, each of which can store one piece of data – either value or formula. Each cell in the grid is identified by its row and column number. Columns are identified by alphabets, A, B, C,…., Z, AA, AB, and so on. Rows are numbered starting from 1. (figure 10.1)

Python Data Presistence - Python and Excel chapter 10 img 1

Excel With openpyxml

The openpyxl is an open-source package. Its installation is straightforward by using pip utility. It is recommended that you set a virtual environment first and then install openpyxl in it using the following command:

Example

E:\>cd excelenv 
E:\excelenv>scripts\activate
(excelenv) E:\excelenv>scripts\pip3 install openpyxl Collecting openpyxl
Using cached https://files.pythonhosted.org/packag- es/Sf/f8/a5d3a4ab669f99154f87ab531192dd84ac79aae62e- 
fab6G2bd2d82a72194/openpyxl-2.6.1.tar.gz Collecting jdcal (from openpyxl)
Using cached https://files.pythonhosted.org/packag- es/aO/38/dcf8353248Of25284f3ef13f8ed63e03c58a65c9d- 
3ba2a6a894ed94972 07/j deal-1.4-py2.py3-none-any.whl Collecting et_xmlfile (from openpyxl)
Using cached https://files.pythonhosted.org/ packages/22/28/a99c42aea746el8382ad9fb36f64c- 
lclf04216f41797f2fOf a567dall3 8 8/et_xmlfile -1.0.1.tar. gz
Installing collected packages: jdcal, et-xmlfile, openpyxl
Running setup.py install for et-xmlfile ... done Running setup.py install for openpyxl ... done 
Successfully installed et-xmlfile-1.0.1 jdcal-1.4 openpyxl-2.6.1

Read Cell Range to List

First of all, let us read back the data from A2:C7 in a Python list object by traversing the range with two nested loops. Each row is collected in a tuple and appended to a list.

Example

sheet1 = wb['PriceList'] 
pricelist= [ ] 
for row in range(1,7): prod= [ ]
for col in range(1,4):
val=sheetl.cell(column=col,
row=2+row).value
prod.append(val) 
pricelist.append(tuple(prod)) 
print (pricelist)

The result will be as shown below:

[(1, 'Laptop', 25000) , (2, 'TV' , 40000) , (3, 'Router', 2000) , (4, 'Printer', 9000)] 'Scanner', 5000) , (5, 'printer' , 9000)]

Merge and Center

To merge Al-Cl cells use the merge_cells ( ) method.

sheet1.merge_cells('A1:C1')

The openpyx1. styles module defines Alignment and Font classes. Apply ‘Center’ alignment to text in ‘AT

cell=sheetl[1A1']
cell.alignment=Alignment('horizontal center')

The Font object can be configured by defining attributes like name, size, color, and so on. Font constructor also accepts bold, italic, underline, and strike, as Boolean attributes (True/False).

cell.font=Font(name='Calibri',size=20,bold=True)

Insert Image

First, install the pillow library in your current Python environment. It is an open-source Python image library that provides support for opening, saving, and manipulating image files.

pip3 install pillow

Let us create a new worksheet in ‘example.xlsx’ in which we shall insert an image.

wb = load_workbook('example.xlsx') 
sheet2=wb.create_sheet(title="image")

Now import Image class from openpyxl. drawing. image module. Obtain the Image object from the image file to be inserted. Finally call add_image () method of the worksheet. This method needs image object and location, (figure 10.9)

Example

from openpyxl.drawing.image import Image
img = Image('openpyxl.jpg')
sheet2.add_image(img,'Al')
wb . save (filename= ' example . xlsx ' )

Define Formula

It is very easy to define a formula in a cell. Just assign string representation of cell formula as would appear in the formula bar of Excel. For example, if you want to set cell 8 to sum of cells between C3 to 7, assign it to ‘=SUM(C3:C7)’

Example

sheet1 ['B8'] ='SUM'
sheet1['C8']='SUM(C3:C7)'
sheet1['C9'] ='=AVERAGE(C3:C7)'

All above actions are collected in the following script. Run it from the command prompt and then open the workbook, (figure 10.4)

Example

#readworkbook.py
from openpyxl import load_workbook 
wb = load_workbook('test.xlsx') 
sheet1 = wb['PriceList']
#copy range to list 
pricelist= [ ] 
for row in range(1,7): 
prod= [ ]
for col in range(1,4):
val=sheetl.cell(column=col,
row=2+row).value
prod.append(val) 
pricelist.append(tuple(prod)) 
print (pricelist)
#merge and center
sheetl.merge_cells('A1:C1')#merge
cell=sheetl['A1']
cell.alignment=Alignment(horizontal'center1) #apply font
cell.font=Font(name='Calibri',size=20,bold=True)
#define formula
sheetl['b8']='SUM'
sheetl['C8']='=SUM(C3:C7)'
sheetl['C9']='=AVERAGE(C3:C7)'

Python Data Presistence - Define Formula chapter 10 img 1

Copy Formula

One of the important features of Excel software is the ability to copy a cell formula either with the relative or absolute address to other cells or ranges. In the above worksheet, we calculate the difference of each price and average and store it in Column D. Accordingly formula for D4 should be C4-C9. It is to be copied for range D5:D9

The openpyxml. formula module defines Translator class having translate_formula ( ) function that copies formula at original cell (D4) to the required range.

Example

from openpyxl.formula.translate import 
Translator#copy formula 
sheet1['D2']='DIFF' 
sheet1['D3']= ' =C$9-C3' 
sheet1['D4'] = Translator("=C$9-C3", origin="D3") . translate_formula("D4") 
for row in range(4,8): 
coor=sheetl.cell(column=4, row=row). 
coordinate#copy formula to range 
sheet1.cell(column=4, row=row). 
value=Translator("=C$9-C3", origin="D3"). \ 

translate formula(coor)

Using the translate_formula, we can also copy a range of cells to other location. Following snippet copies range A2:D2 which is the table’s heading row to A10:D10 cell range.

Example

sheet1['A10'] = '=A2' 
for col in range(1,4): 
coor=sheet1.cell(column=col,row=3).coordinate 
coor1=sheet1.cell(column=col, row=10).coordinate 
print (coor,coor1) 
sheet1.cell(column=col, row=10). 
value=Translator("=A2", origin="A10"). \ 

translate formula(coor1)

After copying the formula and range, (figure 10.5) the worksheet appears as follows:

Python Data Presistence - Copy Formula chapter 10 img 1

Data Persistence – Python and Excel Read More »

Python Data Persistence – class Keyword

Python Data Persistence – class Keyword

Apart from various built-in classes, the user can define a new class with customized attributes and methods. Just as a built-in class, each user-defined class is also a subclass of the Object class. The keyword ‘class’ starts the definition of a new class.

Example

>>> #user defined class
. . .
>>> class MyClass:
. . . pass
. . .
>>> type(MyClass)
<class 'type'>
>>> MyClass.__bases__
(<class 'object'>,)
>>> issubclass(MyClass, object)
True

A user-defined name is mentioned just beside the class keyword and is followed by a symbol to initiate an indented block containing its attributes and methods. In this case, the class body contains just one statement pass which is a no-operation keyword.
Above code clearly indicates that the user-defined class is subclassed from the object class. It means that any Python class, built-in or user-defined is either directly or indirectly inherited from the parent object class. To underline this relationship explicitly, the MyClass can very well be defined as follows:

Example

>>> class MyClass(object):
. . . pass
. . .
>>> MyClass.__bases__
(<class 'object'>,)

Now we can have objects of this class. The library function dict() returns an empty dictionary object. Similarly, MyClass( ) would return an empty object because our new class doesn’t have any attributes defined in it.

Example

>>> obj1=MyClass( )
>>> obj2=MyClass( )

It is veiy easy to add attributes to this newly declared object. To add ‘myname’ attribute to obj 1, give the following statement:

>>> obj1.myname='Ashok'

There is also a built-in setattr ( ) function for this purpose.

>>> setattr(obj1, 'myage',21)

The second argument for this function may be an existing attribute or a new one. The third argument is the value to be assigned. You can even assign a function to an attribute as shown below:

Example

>>> def about me(obj):
. . . print ( ' My name is { } and I am { } years old ' . format ( obj,myname,obj.myage ) )
. . .
>>> setattr(MyClass, 'about', aboutme)
>>> obj1.about( )
My name is Ashok and I am 21 years old

So you have been able to add new attributes to an individual instance or class. Even from outside the class! If you have honed your object-oriented programming skills by learning Java or C++, this might send shivers down your spine! On the face of it, this seems to be a violation of the very idea of class which is a template definition of an object’s attributes. Don’t worry; we will soon make a Python class work as per OOP guidelines.

Python Data Persistence – class Keyword Read More »