Top 10 Python Books on Amazon.Com

Here we have listed top 10 python programming books on Amazon.

Books are selected based on user rating (count and star). Books are listed in no particular order.

– Automate the Boring Stuff with Python

– Python Tricks: A Buffet of Awesome Python Features

– Python Programming: An Introduction to Computer Science, 3rd Ed

– Python Programming for the Absolute Beginner, 3rd Edition

– Python Programming: An Introduction to Computer Science

– Learn Python the Hard Way

– Learning Python, 5th Edition by Mark Lutz

– Python Crash Course: A Hands-On, Project-Based Introduction to Programming

– A Smarter Way to Learn Python: Learn it faster. Remember it longer.

– Invent Your Own Computer Games with Python

Please suggest if any other book should be in list.

 

Feature image source: https://goldenharebooks.com/

Python Tip 1: Accessing localhost Django webserver over the Internet

We use Django development webserver to test our application on localhost.

We can start the development webserver with below command:

Now one problem we face with this approach is that we can access/browse the project over the browser only in the local network to which our system is connected.

What if you want to show your project to someone at another location, in another city over the Internet?

How would you generate a public URL for your localhost webserver?

 

For such situations, there is one software available. ngrok.

How to use it:

– Download the ngrok from its download page .

ngrok download

–  Follow the documentation to unzip the downloaded content and to add the authentication token.

– Go to your project directory and activate your virtual environment.

–  Now start your Django development server at port lets say 8080.

– You can access your project on localhost:8080. We will generate a public URL for this localhost URL by creating a tunnel using ngrok.

– Go to the directory where you unzipped ngrok and run below command.

– You will see something like this:

ngrok tunnel

– Now you can access your project over the public address http://9f134cf4.ngrok.io  .

–  You can access the web interface on http://127.0.0.1:4040 to see which URL got how many hits.

Public URL for localhost Django project

 

You can host your Django App for free on PythonAnyWhere server.

5 common mistakes made by beginner python programmers

During the initial days as python programmer, everyone of us face some or other type of weird bug in our code which, after spending multiple painful hours on Stackoverflow, turns out to be not a bug but python feature. That’s how things works in python. So below are the 5 most common mistakes most of the beginner python programmers make. Lets know a bit about them so that we can save few hours of asking questions on Facebook pages and groups.

 

1. Creating a copy of dictionary or lists.

Whenever you need to make a copy of a dictionary or list, do not simply use the assignment operator.

wrong way:

Now if you edit/update the dict_b , dict_a  will also be updated because by using assignment operator, you are trying to say that dict_b  will point to the same object to which dict_a  is pointing.

Right way:

Use the copy()  or deepcopy()  method.

See the difference between copy and deepcopy.

2. Dictionary keys.

Lets say we put the below values in a dictionary.

If we try to print the dictionary, what will be the output. Let’s see.

what just happened? where is the key  True .

Remember Boolean class is the subclass of Integer. Integer equivalent of True is 1 and that of False is 0. Hence value of key 1 is overwritten.

 

3. Updating lists or dictionaries.

Lets say you want to append an item to the list.

Try to update a dictionary.

Ok, lets try to sort a list.

Why nothing is being printed? What are we doing wrong?

Most the sequence object methods like sort, update, append, add etc works in place to increase performance by avoiding to create a separate copy un-necessarily.

Do not try to assign the output of such methods to variable.

Right way:

 

4.  Interned Strings.

In some cases, python try to reuse the existing immutable objects. String interning is one such case.

Here we tried to create two different string objects. But when checked if both the objects are same, it returned True. This is because python didn’t created another object b  but pointed the b to the first value “gmail”.

All strings of length 1 are interned. String having anything except ASCII characters, digits and underscore in them are not interned. Let’s see.

Also remember ==  is different than is  operator. ==  checks if values are equal or not while is  checks if both variable are pointing to same object.

So keep the above point in mind while using immutable strings or ==  or is  operator.

 

5. Default arguments are evaluated only once.

Consider below example.

What do you think will be the output of above two print statements.

Lets try to run it.

Why the output is [1,2]  in second case. Shouldn’t it be just [2] .

So the catch here is, default arguments of a function are evaluated on once. On first call i.e  func(1) , list lst  is evaluated and is found empty hence 1 is appended to it. But on second call, list is already having one element hence output is [1,2]

 

Bonus: Don’t mix spaces and tabs. Just don’t. You will cry.

Please comment if you find something is not correct.

Few useful python scripts.

 

Difference between list and tuples in Python and more such comparisons

In this article we will see key differences between commonly used terms in python. For example difference between tuple and list, difference between range and xrange and so on.

List and Tuples:

List:
– are mutable i.e. we can add, extend or update a list.
– generally are homogeneous data structures.


Tuples:
– are immutable i.e. we can not update a tuple.
– generally are heterogeneous data structure.
– is a sequence where position have semantic value.
– are more like records, collection of fixed number of fields.

Continue reading “Difference between list and tuples in Python and more such comparisons”

List of USA states in Python-Django format

In one of previous article we released the list of Indian states in different formats (CSV, List, Set, Model Choice, HTMLSelect) to be used in python-django project. We received few requests to release the USA states in same format. So here we are, presenting to you the states of USA in different Python-Django format.

Continue reading “List of USA states in Python-Django format”

List of Indian States in Python Format

So I was working on a project where I needed list of Indian States in Python format. I looked around on the web but the data was not available in required format. So I decided to create the list of Indian states in different formats which can be used directly in Python-Django project.

You can simply copy paste the data in any of the below format.

Continue reading “List of Indian States in Python Format”

How to send bulk emails for free using Mailgun and Python Django

Consider these scenario in your newly developed Django Application:

  • You need to send promotional emails to all the registered users.
  • You need to send email confirmation link or password reset link.
  • You need to send weekly newsletter to you readers.

What do you choose? Free Gmail Account to send email using python code? That is a nice idea provided you want to look unprofessional and you need not to send more than 500 emails per day.

But if you want professional look in your emails [from address something like admin@yourwebsite.com  instead of something@gmail.com  and do not want the limit on per day email count, then this article is for you.

Continue reading “How to send bulk emails for free using Mailgun and Python Django”

How to schedule a cron on pythonanywhere server

If you have hosted your Django app on pythonanywhere.com server and you want to schedule a cron job on the server to execute some task periodically ( daily or hourly) then this article is for you.

cron jobs are executed after a fixed interval of time, repetitively until stopped explicitly. This may come handy if you want to send bulk mails every hour or you may want to take a backup of your database or basically anything you want to perform periodically.

Continue reading “How to schedule a cron on pythonanywhere server”

How to start logging errors in Django app

In development or testing environment, you may set DEBUG=True  in your settings.py  file and whenever any error is thrown, you see it in the browser.

Setting DEBUG=True  in production environment is not recommended. Debug should be set to False on live servers so that stack trace is not visible in case of any error. Hackers or malicious users may exploit this and may attack you web application.

So how do you debug you application on live servers? Answer is Logging the errors in log files.

Lets  see how to start logging errors in django?

Continue reading “How to start logging errors in Django app”