Useful tools for Python

Python Tutor

Free online Python code visualization, Python learning tool.

Python Tutor
Python Tutor

http://pythontutor.com/

Anaconda

Easy package management

Packed with many useful Python tools

Anaconda
Anaconda
Anaconda - Software
Anaconda – Software

https://www.anaconda.com/distribution/#download-section

Jupyter Notebook

Jupyter notebook is like a magic notebook for Python. It can be used to share Notes, algebra, data analytics, code etc easily.

Jupyter Notebook
Jupyter Notebook

https://jupyter.org/install

(We can use Anaconda to install it easily)

IPython

IPython is a interactive shell for Python.

Supports Automatic indenting, bash shell commands, many built-in functions etc.

https://ipython.org/

Skulpt

Skulpt is a online Python environment built via javascript. Use with CodeMirror, we can do basic Python programming.

Skulpt
Skulpt

http://skulpt.org/


Simple ways to improve/optimize Python performance

1 Use built-in function when available

Use built-in function when available
Use built-in function when available

2 Use List comprehensions to Filter a list

Loop
Loop
Filter
Filter
List comprehensions
List comprehensions

3 Use “try” accordingly

# Slow
class Fruit(object):
    name='Apple'
    
fruit=Fruit()
    if hasattr(fruit,'name'):
        fruit.name
 
 
# Quicker
class Fruit(object):
    name='Apple'
    
fruit=Fruit()
    try:
        fruit.name
    except AttributeError:
        pass

4 Use “in” to check list rather than if (Use built-in function)

in vs for loop and if
in vs for loop and if

5 Deduplication

For loop and if vs set
For loop and if vs set

6 Sort

sorted
sorted
.sort()
.sort()

7 Put iterative loop into function

Iterative loop vs iterative loop in function
Iterative loop vs iterative loop in function

8 Check boolean

if var:
 
# Is faster than
 
if var is True:
 
# Is faster than
 
if var==True:

9 Check is the list is empty

if not list:
 
# Is faster than 
 
if list==[]:
 
# Is faster than
 
if len(list)==0:

10 Object vs list

list() and dict() are slower than [] directly returning a list.


How to: Install Anaconda/Anaconda3 in Linux/Debian/Ubuntu/Kali Linux

(For how to launch Anaconda Navigator or create Anaconda Navigator shortcut: How to: Create shortcut icon for Anaconda/Anaconda3 Navigator/Launch Anaconda Navigator in Linux/Debian/Ubuntu/Kali Linux)

1 Download Anaconda3 from official website: https://www.anaconda.com/distribution/

https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh

2 Make it executable

2.1 We can right click on the file, then click on “Properties” -> “Permissions” -> Check “Allow this file to run as a program”

Assign execute permission
Assign execute permission

2.2 We can also use “chomd” command to assign execute permission

Related to chmod command: Unix/Linux (Ubuntu, Debian, Kali Linux etc.) Privilege Management

3 Launch the “Anaconda3-2019.10-Linux-x86_64.sh” to install Anaconda

Anaconda3-2019.10-Linux-x86_64.sh
Anaconda3-2019.10-Linux-x86_64.sh

3.1 Accept the license terms and install to user home directory

Accept the license terms and install to user home directory
Accept the license terms and install to user home directory

3.2 Let the installer to initialize Anaconda3 by running conda init by typing “yes” then hit Enter key

Type Yes, then hit Enter key
Type Yes, then hit Enter key

4 Now it’s installed if there is no error

(Installation on Windows and macOS is very simple, just download the correct binary file (.exe) form official website, execute, follow the steps to install, the shortcut will be create automatically on desktop and in start menu, all installation packages can be found here: https://www.anaconda.com/distribution/)


How to: Convert string/text to ASCII in python & How to: Convert ASCII to string/text in python

Method 1

#String we want to convert to ASCII
my_string = "AAAasdasfd, a[sdfi       94q,p asf "
 
#Convert to ASCII
results_in_ASCII = list(my_string.encode())
 
#Convert back to string
results_in_string = bytes(results_in_ASCII).decode()
 
#Print ASCII
print(results_in_ASCII)
 
print()
 
#Print String
print(results_in_string)
Method 1
Method 1

Method 2

#String we want to convert to ASCII
my_string = "Hello, world!"
 
#Convert to ASCII
results_in_ASCII = [ord(x) for x in my_string]
 
#Convert back to string
results_in_string = ''.join(chr(i) for i in results_in_ASCII)
 
#Print ASCII
print(results_in_ASCII)
 
print()
 
#Print String
print(results_in_string)
Method 2
Method 2

Resource

How do I convert a list of ascii values to a string in python?


How to: upgrade/update Anaconda

Keywords: Python, anaconda, conda

Upgrade Anaconda & all packages to latest version:

1 Find and launch Anaconda Prompt

Anaconda from Windows start menu
Anaconda from Windows start menu

2 Using following command to update all packages including Anaconda itself

conda update --all
Anaconda Prompt -> conda update --all
Anaconda Prompt -> conda update –all
Anaconda Prompt -> conda update --all -> finished
Anaconda Prompt -> conda update –all -> finished

(Note: This way, it will drop all the version constraints from the history and tries to make everything as new as it can)

Bonus:

We can also use “conda update anaconda=VersionNumber” to update Anaconda to specified version

e.g.

conda update anaconda=1.9.7