Python Basics

Python is an interpreted language, which means your code is directly executed instead of converting it into a machine language unlike a complied language. I am using Anaconda Spyder for all my outputs below

Lets now focus on bare minimum some basics

1) Variable declaration
2) Arrays/Lists 
3) Map/Dictionary
4) Set

Variable declaration:

Variables do not need any data type declaration in Python. But you will need to initialize it before use.
Here is a small example -> 













Output -> 







Unlike other compiled languages, python does not need any data type declaration.

Arrays/Lists:

Please refer to the previous sections if you need to know what an array or List is.
We will discuss about the actual implementation details of arrays and lists here

In arrays all we need to know is how to set and retrieve a value here is how to do it



















Output -> 
















Now coming to Lists, there is hardly any difference between arrays and list in python as far as implementation is concerned

Here are the list of operations on a list.
1) Insert an element at the end of the list
2) Insert an element at the beginning of the list
3) Remove an element from the list
4) Get an element from the list - This similar to accessing an element in an array
5) Get length of the list































Output->




















Stack:

All we need for a stack is just push and pop. This can be accomplished using a list in Python
Push -> .append(): adds an element to the last 
Pop -> .pop() returns the last element from the list
Peek -> [-1] index of -1 will return the last element of the stack


















Output ->













Queue:

For a queue all we need is to enqueue and dequeue, append can push to the end of the array. pop(0,0) can remove an element from index 0. This is good enough for a queue.




















Output ->













Dictionary:

A map or dictionary needs to store a key value pairs, we will learn about initialization, adding, removing and iterating a dictionary 

1) Initializing a dictionary
2) Adding to a dictionary is as simple as adding to a list
3) Removing from a dictionary
4) Iterating a dictionary























Output ->
























Set:

A set is a unique collection of elements. Below is what we need for a basic set in python

1) Initializing a Set
2) Adding new element
3) Removing an element
4) Iterating a Set























Output ->






















Making a 2D array:

This is a very basic requirement in any language, there are two ways of doing this. But I will only explain one way which is most useful in my opinion.


Code:


Output:






No comments:

Post a Comment