Everything About Python — Beginner To Advanced

Everything You Need To Know In One Article. This article aims to outline all of the key points of Python programming language.

My target is to keep the information short, relevant and focus on the most important topics which are absolutely required to be understood.

After reading this blog, you will be able to use any Python library or implement your own Python packages.

You are not expected to have any prior programming knowledge and it will be very quick to grasp all of the required concepts.

I will also highlight top discussion questions that people usually query regarding Python programming language.

Lets build the knowledge gradually and by the end of the article, you will have a thorough understanding of Python.
Please let me know whether you want me to post exercises and their solutions to help you practice Python.
Lets build the knowledge gradually and by the end of the article, you will have a thorough understanding of Python.### 1. Introducing Python

What Is Python?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Why Python?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Why Not Python?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

How Does Python Work?

This image illustrates how python runs on our machines:

The key here is the Interpreter that is responsible for translating high level Python language to low level machine language.

2. Variables — Object Types And Scope

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Python supports numbers, strings, sets, lists , tuples and dictionaries. These are the standard data types.

Declare And Assign Value To Variable

Assignment sets a value to a variable:

myFirstVariable = 1
mySecondVariable = 2
myFirstVariable = "Hello You"

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

*Note how I assigned an integer value of 1 and then a string value of “Hello You” to the myFirstVariable variable. *This is possible due to the fact that the data types are dynamically typed in python.

Numeric

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
value = 1 #integer
value = 1.2 #float with a floating point

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Strings

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
name = 'farhad'

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
a = 'me'
a[1]='y'
It will throw a Type Error

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Variables can have local or global scope.

Local Scope

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
def some_funcion():
  TestMode = False
print(TestMode) <- Breaks as the variable doesn't exist outside

Global Scope

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
TestMode = True
def some_function():
  global TestMode
  TestMode = False
some_function()
print(TestMode) <--Returns False

Removing the line “global TestMode” will only set the variable to False within the some_function() function.

Note: Although I will write more on the concept of modules later, but if you want to share a global variable across a number of modules then you can create a shared module file e.g. configuration.py and locate your variable there. Finally, import the shared module in your consumer modules.

Finding Variable Type

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
type('farhad')
--> Returns <type 'str'>

Comma In Integer Variables

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
9,8,7 is three numeric variables

3. Operations

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Numeric Operations

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
1//3  #returns 0.333...
1/3 #returns 0 as both of the operands are integers

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
2**3 = 2 * 2 * 2 = 8

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
7%2 = 1

String Operations

Concat Strings:

'A' + 'B' = 'AB'

Repeat String:

‘A’*3 will repeat A three times:  AAA

Slicing:

y = 'Abc'
y[:2] = ab
y[1:] = c
y[:-2] = a
y[-2:] = bc


Reversing:

x = 'abc'
x = x[::-1]

Negative Index:

If you want to start from the last character then use negative index.

y = 'abc'
print(y[:-1]) # will return c

Also used to remove any new line carriages/spaces.

Finding Index

name = 'farhad'
index = name.find('r')
#returns 2
name = 'farhad'
index = name.find('a', 2) # finds index of second a
#returns 4

For Regex, use:

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Casting

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Set Operations

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
a = {1,2,3}

Intersect Sets

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
a = {1,2,3}
b = {3,4,5}
c = a.intersection(b)

Difference In Sets

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
a = {1,2,3}
b = {3,4,5}
c = a.difference(b)

Union Of Collections

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
a = {1,2,3}
b = {3,4,5}
c = a.union(b)

Ternary Operator

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Syntax:

[If True] if [Expression] Else [If False]

For example:

Received = True if x = 'Yes' else False

4. Comments

Single Line Comments

#this is a single line comment

Multiple Line Comments

One can use:

```this is a multi
line
comment```

5. Expressions

Expressions can perform boolean operations such as:

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

6. Pickling

Converting an object into a string and dumping the string into a file is known as pickling. The reverse is known as unpickling.

7. Functions

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Define New Function

def my_new_function():
  print('this is my new function')

Calling Function

my_new_function()

Finding Length Of String

Call the len(x) function

len('hello')
prints 5

Arguments

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
def my_new_function(my_value):
  print('this is my new function with ' + my_value)

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

We can pass in optional arguments by providing a default value to an argument:

def my_new_function(my_value='hello'):
  print(my_value)
#Calling
my_new_function() => prints hello
my_new_function('test') => prints test

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

If your function can take in any number of arguments then add a * in front of the parameter name:

def myfunc(*arguments):
  return a
myfunc(a)
myfunc(a,b)
myfunc(a,b,c)

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

It allows you to pass varying number of keyword arguments to a function.

You can also pass in dictionary values as keyword arguments.

Return

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
def my_function(input):
  return input + 2

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
resultA, resultB = get_result()
get_result() can return ('a', 1) which is a tuple

Lambda

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
my_lambda = lambda x,y,z : x - 100 + y - z
my_lambda(100, 100, 100) # returns 0

Syntax:

variable = lambda arguments: expression

Lambda functions can be passed as arguments to other functions.

dir() and help()

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Photo by Noah Silliman on Unsplash

8. Modules

What is a module?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

PYTHONPATH

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

How To Import Modules?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
import MyFirstPythonFile

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
import my_module

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
print(my_module.my_object)

Note: If you do not want the interpreter to execute the module when it is loaded then you can check whether the name == ‘main

2. From

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
from my_module import my_object

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
print(my_object)

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
from my_module import *

Note: Modules are only imported on the first import.

If you want to use C module then you can use PyImport_ImportModule

Use import over from if we want to use the same name defined in two different modules.

9. Packages

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
from packageroot.packagefolder.mod import my_object

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
from data_service.database_data_service.microsoft_sql.mod

Note: Ensure each directory within your package import contains a file init.py.

Feel free to leave the files blank. As init.py files are imported before the modules are imported, you can add custom logic such as start service status checks or to open database connections etc.

PIP

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
pip install package_name

10. Conditions

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
if a = b:
  print 'a is b'
elif a < b:
  print 'a is less than b'
elif a > b:
  print 'a  is greater than b'
else:
  print 'a is different'

Note how colons and indentations are used to express the conditional logic.

Checking Types

if not isinstance(input, int):
  print 'Expected int'
  return None

You can also add conditional logic in the else part. This is known as nested condition.

#let's write conditions within else
else:
 if a = 2:
    print 'within if of else'
 else:
     print 'within else of else'

11. Loops

While

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
while (input < 0):
 do_something(input)
 input = input-1

For

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
for  i in range(0,10)

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
for letter in 'hello'
  print letter

One-Liner For

Syntax:

[Variable] AggregateFunction([Value] for [item] in [collection])

Yielding

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Combine For with If

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
name = 'onename'
anothername = 'onenameonename'
for character in name:
  if character in anothername
     print character

Break

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
for i in range(0,10):
 if (i==5):
   break

while True:
  x = get_value()
  if (x==1):
     break

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
def fib(input):
 if (input <=1):
   print(str(input))
 else:
   first = 0
   second = 1
   count = 0
   for count in range(input):
     result = first + second
     print(first)
     first = second
     second = result
     count += 1
     
fib(7)

12. Recursion

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Let’s implement a factorial recursive function:

Rules:

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Steps:

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
def factorial(n):
  if n==0:
    return 1
  else:
    return n * factorial(n-1)

Another Example: Let’s write Fibonacci recursive function:

Rules:

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

0, 1, 1, 2, 3, 5, 8…

Steps:

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
def fibonacci(n):
 if (n<=1):
   return n
 else:
   return fibonacci(n-1)+fibonacci(n-2)
print(fibonacci(6))

It is important to have an exit check otherwise the function will end up in infinite loop.

13. Frames And Call Stack

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

You can use traceback to find the list of functions if an error is encountered.

14. Collections

Lists

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
my_list = ['A', 'B']

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
my_list.append('C') #adds at the end
my_list[1] = 'D' #update
my_list.pop(1) # removes
or
del my_list[1:2] # removes
my_list.extend(another_list) # adds second list at end

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
my_list.sort()

Tuples

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
my_tuple = tuple()
or
my_tuple = 'f','m'
or
my_tuple = ('f', 'm')

Note: If a tuple contains a list of items then we can modify the list. Also if you assign a value to an object and you store the object in a list and then change the object then the object within the list will get updated.

Dictionaries

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
my_dictionary = dict()
my_dictionary['my_key'] = 1
my_dictionary['another_key'] = 2

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
my_dictionary = {'my_key':1, 'another_key':2}

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
for key in dictionary:
  print key, dictionary[key]

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
dictionary.items() # returns items
#checking if a key exists in a dictionary
if ('some key' in dictionary):
  #do something

Note: If you want to perform vectorised/matrix operations on a list then use NumPy Python package

15. Compilation And Linking

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Compilation:

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Linking:

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

16. Iterators

Iterators

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Filter

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Map

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Reduce

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Zip

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
name = 'farhad'
suffix = [1,2,3,4,5,6]
zip(name, suffix)
--> returns (f,1),(a,2),(r,3),(h,4),(a,5),(d,6)

17. Object Oriented Design — Classes

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
class MyClass:
  def MyClassFunction(self):  #self = reference to the object
    return 5
#Create new instance of MyClass and then call the function
m = MyClass()
returned_value = m.MyClassFunction()

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Note: If a tuple (immutable collection) contains a list (mutable collection) of items then we can modify the list. Also if you assign a value to an object and you store the object in a list and then change the object then the object within the list will get updated.

init

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
class MyClass:
   def __init__(self, first_property):
       self.first_property = first_property
   def MyClassFunction(self):
      return self.first_property

#Create an instance
m = MyClass(123)
r = m.MyClassFunction()
r will be 123

Note: self parameter will contain the reference of the object, also referred to as “this” in other programming languages such as C#

str

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
m = MyClass(123)
print m #Calls __str__

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

cmp

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Overloading

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Shallow Vs Deep Copy Of Objects

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
import copy
m = MyClass(123)
mm = copy.copy(m)

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
import copy
m = MyClass(123)
mm = copy.deepcopy(m)

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

18. Object Oriented Design — Inheritance

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
class ParentClass:
 def my_function(self):
   print 'I am here'

class SubClass1(ParentClass):
class SubClass2(ParentClass):

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Note: Python supports multiple inheritance unlike C#

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Multi-Inheritance:

class A(B,C):  #A implments B and C


  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
super(A, self).function_name()

19. Garbage Collection — Memory Management

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

As multiple objects can share memory references, python employs two mechanisms:

  1. Reference counting: Count the number of items an object is referenced, deallocate an object if its count is 0.
  2. The second mechanism takes care of circular references, also known as cyclic references, by only de-allocating the objects where the allocation — deallocation number is greater than threshold.
  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
import gc
collected_objects = gc.collect()

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

20. I/O

From Keyboard

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
user_says = raw_input()
print(user_says)

Files

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Open files

with open(file path, 'r') as my_file:
  for line in my_file
#File is closed due to with/as

Note: readline() can also be executed to read a line of a file.

To open two files

with open(file path) as my_file, open(another path) as second_file:
  for (line number, (line1, line2)) in enumerate(zip(my_file, second_file):

Writing files

with open(file path, 'w') as my_file:
  my_file.write('test')

Note: Use os and shutil modules for files.

Note: rw — read-write mode and a — append mode.

SQL

Open a connection

import MySQLdb
database = MySQLdb.connect(“host”=”server”, “database-user”=”my username”, “password”=”my password”, “database-name”=”my database”)
cursor = database.cursor()

Execute a SQL statement

cursor.fetch("Select * From MyTable")
database.close()

Web Services

To query a rest service

import requests
url = 'http://myblog.com'
response = requests.get(url).text

To Serialise and Deserialise JSON

Deserialise:

import json
my_json = {"A":"1","B":"2"}
json_object = json.loads(my_json)
value_of_B = json_object["B"]

Serialise:

import json
a = "1"
json_a = json.dumps(a)

21. Error Handling

Raise Exceptions

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
try:
  raise TypError
except:
  print('exception')

Catching Exceptions

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
try:
   do_something()
except:
   print('exception')

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
try:
   do_something()
except TypeError:
   print('exception')

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
try:
   do_something()
except TypeError:
   print('exception')
finally:
   close_connections()

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Try/Except/Else

try:
  do something
except IfTryRaisedException1:
  do something else
except (IfTryRaisedException2, IfTryRaisedException3)
  if exception 2 or 3 is raised then do something
else:
  no exceptions were raised

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
try:
  do something
except Exception1 as my_exception:
  do something about my_exception

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.
assert <bool>, 'error to throw'

Note: Python supports inheritance in exceptions

You can create your own exception class by:

class MyException(Exception): pass

22. Multi-Threading And GIL

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Note: GIL adds overheads to the execution. Therefore, be sure that you want to run multiple threads.

23. Decorators

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

24. Unit Testing In Python

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

1.Assume your function simply decrements an input by 1

def my_function(input):
  return input - 1

  1. You can unit test it by:
import unittest
class TestClass(unittest.TestCase):
 def my_test(self):
    self.assertEqual(my_function(1), 0)) #checking 1 becomes 0

We can also use doctest to test code written in docstrings.

25. Top Python Discussion Questions

Why Should I Use Python?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

How To Make Python Run Fast?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Which IDEs Do People Use?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

What Are The Top Python Frameworks And Packages?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

PyUnit (unit testing), PyDoc (documentation), SciPy (algebera and numerical), Pandas (data management), Sci-Kit learn (ML and data science), Tensorflow (AI), Numpy (array and numerical), BeautifulSoap (web pages scrapping), Flask (microframework), Pyramid (enterprise applications), Django (UI MVVM), urllib (web pages scraping), Tkinter (GUI), mock (mocking library), PyChecker(bug detector), Pylint (module code analysis)

How To Host Python Packages?

  1. Reference counting: Count the number of items an object is referenced, deallocate an object if its count is 0.
  2. The second mechanism takes care of circular references, also known as cyclic references, by only de-allocating the objects where the allocation — deallocation number is greater than threshold.
#(#!/my account/local/bin/python)

  1. You can use command line tool and execute it

  2. Use PyPRI or PyPI server

Can Python and R be combined?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Is there a way to catch errors before running Python?

  • Interpreted high-level object-oriented dynamically-typed scriptinglanguage.
  • Python **interpreter **reads one line of code at a time, interprets it into low level machine language (byte code) and then executes it.
  • As a result, run time errors are usually encountered.

Photo by Curtis MacNewton on Unsplash

Summary

This article outlined the most important 25 concepts of Python in a short, relevant and focused manner. I genuinely hope it has helped someone get better understanding of Python.

I believe I have concentrated on the must know topics which are absolutely required to be understood.This knowledge is sufficient to write your own python packages in the future or using existing Python packages.

Rest, just practice as much as possible and you can implement your own library in Python because this article contains all the knowledge you need.

If you want me to post Python exercises and solutions then please let me know.

Hope it helps.

Thanks for reading ❤

If you liked this post, share it with all of your programming buddies!

#python

Everything About Python — Beginner To Advanced
52.95 GEEK