Python Data Structure and Containers

Python has several in built data containers to facilitate efficient data storage and retrieval. Some key ones are-

  • List
  • Tuple
  • Dictionary

Let’s look at the above types one by one

List- Lists are mutable (can be edited) and iterable data containers with homogeneous or heterogeneous data. This is one of the most commonly used data structure in Python. A list is denoted by square brackets – “ [ ]

Let’s look at some examples of lists operations-

list3

list4.png

list5

list6

Next, let’s do slicing and dicing of the list. This follows the same zero based indexing as strings

list7

list8.png

list9.png

Tuple- Tuples operations are significantly faster than list, however tuples are immutable. Tuples are best suited for write once and read many times jobs such as big data operations. Similar to list, a tuple can store heterogeneous data.

They are defined by ” ( ) “.  Let’s look at some examples of tuples operations-

tuples

Dictionary- Similar to tuples operations, dictionary operations are significantly faster than that of lists. A dictionary is made of “Key-Value” combinations. Values are generally retrieved by providing the keys.

Dictionaries are defined by ” { } “.  Let’s look at some examples of dictionary operations-

dictionary1.png

dictionary2

You can find much more information on the above objects in Python Official Documentation.

Cheers!

Conditional and Loops in Python

Conditional and loop statements are great tools for executing codes when a certain condition is met or till the point until certain condition(s) remain true. There are may types of conditionals and loops in Python. Some key ones are-‘ if’ statement,’ for’ statement, ‘while’ statement. Here are few examples.

conditiional1

conditiional2

conditiional3

Cheers!

 

String Manipulation and Python Indexing

In Python strings are created by specifying text either in single quote or double quote. Furthermore, Python Index begins from 0 while going from left to right and -1 while going from right to left. We can use indexing in many different ways. Some examples are shown below. In the below example, we are creating two strings and doing slicing an dicing and other string manipulation

string1

string2.PNG

string3

Cheers!

Exception Handling in Python

Exception handling is a graceful way to manage execution of a program and display user friendly information when errors occur during execution of the programs.

Let’s look at the below example to get a better understanding. User needs to provide a number to the below program and reverse of the number will be returned.

If the user provides a numerical input the “try block” will be executed and the “finally block” will be executed.

execption_handling1

However, if the user provides a non numeric input, an exception error “ValueError” will happen and code will execute the first “except” block and “finally block”.

execption_handling2.PNG

Similarly if the user provides ‘zero’ as the input, an exception error “ZeroDivisionError” will happen and code will execute the second “except” block and “finally block”.

execption_handling3.PNG

Cheers!

 

Python Functions

Functions are used to make certain portions of codes reusable and are a great tool to bring efficiency in the codes. In Python, we define a function using keyword “def” followed by the name of the function, the parameters and the indented block which will execute the function to produce a certain output. Let’s look at few examples.

The first function is called “hello” and it takes no parameter and will print “This is my first python function and I’m so excited”, whenever this function is called [ hello ()].

Defining a simple function called “Hello” to print the below message

Slide1

Functions may be given parameters and default values, in case the user is not providing or overwriting the default values, the function will use these values to process the codes.

Defining a function called “sqr” to square a number. The default value given is 2

Slide2

Defining a function to add three numbers with default values. Can you figure out what the below Python function is doing? What are the default values?

Slide3Anonymous or Lambda Functions are short functions which are generally written in a single line. For example, the below set of codes show you how we can use Lambda function to cube a number, instead of defining a function.

Slide4

Moreover, the Lambda functions can be assigned to a variable as shown in the above code, where we are assigning it to the ‘cube’ variable.

Python also has a host of in built functions – Inbuilt Functions

Thanks for reading!

Types of Variable in Python and Typecasting

Python has many types of variables. Some key ones are-

  • Integer (Whole number)
  • Float (Decimal points)
  • String (Categorical)
  • Date time

It’s easy to convert one type of variable into another type, whenever possible. It’s called “Typecasting”.  Moreover, we don’t have to specify the type specifically in Python.

Python assigns the right type depending on the value we assign. Here are some examples-

Typesofvariables

Typesofvariables1

Typesofvariables2

Typesofvariables3

Cheers!

 

Python Variable Naming Convention

Here are few things that we have to remember when naming variables and objects in Python.

  • Python is a case sensitive language. ‘x’ and ‘X’ will be treated as different variables.
  • Python variables can have letter, numbers and underscores only
  • The variable names to begin with a letter or underscore only
  • Single line comments are denoted with ” # ‘ and multiple line comments are with triple quotes ”’ ”’

Few examples-

Python_Variable_Naming.PNG

Cheers!

Comparison or Logical Operations in Python

Various logical, boolean and comparison operations can be conducted in Python using simple codes such as shown below

For example,  “1 == 2” is testing or whether 1 is equal to 2. The answer is obviously “False”. Similarly “x!=2 and y !=0” is testing whether both conditions are met, i.e. x is not equal to 2 and y is not equal to 0, which turns out be “False” as well in the below example as x is equal to 2.

Relational_Operations

Cheers!