Python Programming Notes

Below I have listed some basic concepts that will help you to get started with programming in Python.

This is in no way extensive and familiarity with a programming language will be helpful. I will recommend you to visit the official Python documentation which will come in handy.

Comments can be added using #

Basic data types include Numbers, String etc.

Strings are immutable in Python. Which means you cannot modify a string.

Format method can be used to print values

x1 = ‘Jack’
print(‘{0} is new to the city’.format(x1))

Python does not allow special characters such as @, $, and % within identifiers. Python is a case-sensitive programming language.

Python is strongly object-oriented in the sense that everything is an object including numbers, strings and functions.

How to indent

Python does not use braces. Use four spaces for indentation.
if(a == 5):
print(‘Indentation works’)


Functions in Python let you create reusable piece of code. A function can have a block of code which performs a specific task. Like finding a prime number. Let’s see a simple function which returns sum of two numbers.

def getSum(a,b):
return a+b

VarArgs parameters
Sometimes you may want to define a function that can take any number of parameters, i.e. variable number of arguments, this can be achieved by using the stars.

def getSum(*numbers):
    sum = 0
    for x in numbers:
        sum = sum+x
    return sum


What * does is to collect all values in a tuple. With ** we get the values in a dictionary.

This allows us to provide documentation for a function. A string on the first logical line of a function is the docstring for that function.

def getSum(*numbers):
“””This function return sum of the numbers.
return sum

If we want to group a set of functions together to make a library. We can create a module by putting functions in a .py file or we can also write code in a language like C. Upon compilation it can be used by the python interpreter.

To use a module we need to import it. To import a function from a module we can use
from math import sqrt
from test_module import getDiff

Modules can be clubbed together in Packages.

Data Structures in Python
Data Structures are used to efficiently organise, manage and access data.

Python provides four types of data structures : List, Tuple, Dictionary and Set.

List Data Structure
Elements in a List can be of different data types. They are ordered. A List is mutable meaning it can be modified.

myList = [2,3,'a']

To iterate through a List we can use

for i in range(len(numbers)):
    numbers[i] = numbers[i] * 2

List Operations
The + operator concatenates lists
The * operator makes it repeat
The slice operator also works on lists: print(myList[2:3])
We can use append() to add value to the end of the list.
sort() is available to sort values in list
To add all elements of the list we can use sum(myList)

Map, Reduce and Filter
Map is when we have a function that maps each element of a list with another. Like map each character to its upper case.
Reduce is like function that sums all values of the list and returns just one element.
Filter is like getting a subset based on some condition.

Delete from a List
pop can be used like d = myList.pop(3) It returns the element that was removed.
We can also use del with slice index. Like : del mylist[1:2]

Dictionaries Data Structure
A key-value pair. When we print the dictionary the order of items may be different.
To traverse the keys in sorted order, you can use the built-in function sorted.
We cannot use a list as a key since it’s mutable. We may not get the same hash value.
In a key-value pair the hash value is computed using the key.

dict = {1:”Ram”, 2:”Mike”}

Tuples are like Lists but are immutable.

t = (32, 34, 23)

Another way to create a Tuple is using t = tuple()
If we try to do this now t[0] = ‘g’; we get TypeError: ‘tuple’ object does not support item assignment

Set Data Structure
Python also supports Sets. Sets can be used to hold unique elements. But the elements in a set need not be ordered.

a = {2,3,4}