Tuesday, January 28, 2020

Today's python learning: Cascaded Assignments, Simultaneous assignment, Multiline statement, Multiline strings, split() method and eval method

Today I learned:

Cascaded Assignments:

We can assign the same value to multiple variables at the same time. For example, if we write

x=y=z=10

there will be 3 separate variables each of them holding 10 in their content. We can see from there content. This variable type and content explorer can be viewed from the Spyder IDE, which is cool and intuitive.

Simultaneous assignment:

We can also do simultaneous assignments in Python which is not commonly available to other languages. To do that we have to use a comma between expressions and an equal (assignment) operator at the right side. The following variable swapping problem is a perfect example of a simultaneous assignment.

We can see from the output that it works perfectly.

Multiline Statement:

This can be constructed using either escaping a newline character or using the parentheses.

Multiline String:

The multiline string can be done using enclosing strings into either a set of triple-double quotation marks or single quotation marks.

Follow the following examples for clarification.

Split method:

This is a very important method. It returns a list of strings after breaking the given string by the specified separator It accepts two parameters. Separator and the Max split. The following is an example of the split method, where the default separator is space.


eval() function:

This is a powerful tool as it evaluates the strings into python expressions. So we need to be careful about this tool. It will be an amazingly handy tool for building a calculator app or some graphing tools. The example can be found below.







Monday, January 27, 2020

Python Practice problem 4: Check for palindrome

Palindromes are the words that do not get changed when you put it in reverse order. So, "Mom", "Madam" are examples of the palindrome. This is a very simple problem where we use reversing a list using negative list slicing.


Sunday, January 26, 2020

Sunday Morning bird searching with my new Nikon D7500

    I woke up at 9 am and had some cereal. Then, I took out my Nikon D7500 (Recently I have sold my old Nikon D5300+18-55 kit lens in the Facebook marketplace and bought this one ) and attached the 70-300mm telephoto lens on it. The objective was to go out and search for some birds in this area. It is winter here and the surroundings are mostly covered with snow with no leaves in the trees. It is pretty hard to find any birds and animals outside except some squirrels here and there occasionally. However, today's temperature was not negative. It was averaging at 1-2 degrees celsius. So, I thought that it will be fun walking in Elmwood Park.


    This park is my recent favorite place because of its location near to the Big Sioux River. A lot of birds come and rest there in the afternoon. For some reason, the water is rarely frozen there. However, you won't find any bird at the river in the daytime. So I wanted to just walk by the rive and there is a nice road beside the river which in my opinion, one of the best places for my relaxation in Sioux falls. After parking my car, I walked to the riverside road and walked for some time. It was sunny whole day. You can walk inside the park for some time and then get to the riverside road.

    While walking through the woods, I heard chirpings of the bird but, interestingly, I was not able to see any bird in the woods. Later, I discovered that the birds were too small to skip if you don't look carefully. To add more complexity the trees a the park were  huge in height, so it posses a tough challenge to locate those tiny birds. I felt that my 70-300 is not enough for me (I am thinking of the 200-500mm for a long time and I might get one soon). After spending about one hour I got to manage some shots. After coming back to home I processed some of the images. Oh one thing, I started shooting RAW and it made a night and day difference in the photo I take. I am very surprised by the image quality of this awesome camera (next target is to get a full-frame; Nikon D780 or Z6 might be the best option for me ...LoL). However, I am attaching some pictures from the Elmwood Park.
DSC_2857 DSC_2873-2 DSC_2924 DSC_2973




Thursday, January 23, 2020

Python Practice problem 3: Determining the overlaps of two lists.

Problem:

This is the practice problem 5 from https://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html. The problem is -



1. We need to make two lists, the items of the lists might be unique or have common items within them.


2. We need to write a program that returns a list containing only the elements that are common between the lists while not considering the duplicates.


For example, we are given two lists- list_A and list_B as the following


list_A=[1,3,5,6,7,8,9,10,13,15,20]


list_B=[2,4,6,8,9,10,12,14]


Our program should be able to find duplicates.


Solving Part:

The problem is pretty straight forward, but our objective is to explore different ways and learn python through problem-solving.

While solving problems, one problem I often fetch is indexing error which is caused by putting variable() instead of variable[]. The problem arises because I came from a Matlab background where the function calling and the variable indexing uses the same "()" which is confusing actually.


Anyway, let's go to coding. The first quick draft is shown at the following which is not taking care of duplicate values. The first thing comes to mind is the use of Brute Force. which is -


1. Take any of the two lists and iterate through it

2. While iterating through the items, check for the existence in the second list using "in" keyword.

The code is shown below-




You can obviously see the problem here. Look at the program output. [6, 8, 9, 10, 10]. We have duplicates in the result. It is expected. The last piece of code I  want to add to this program is, well I know it's not the best way to do this, is using a set operation on the generated list to remove the duplicate items i.e., to keep the unique items only in the list.


So the final version is here.


Learnings:


Few things are important to note from here


"in" operator:

The "in" operator in python is for checking membership of value against a sequence. For example,
If we declare a list and then if we want to check for the existence of an item in that list we can use this keyword "in". I am writing these expressions for clarification

There are two operators which are called membership operators. 

a. in operator
b. not in operator
About 'not in' operator, It basically does the opposite of  "in" operator. i.e., the expression containing it evaluates to true if it does not find the variable in the sequence.

We need to learn about Identity operators too. There are two identity operators like the membership operator.


1. 'is' operator: It checks if the object on the left side of  'is' is pointing to the same object on the right side. Personally, at first, I was confused about this operator.  But, when I think of the variables as a just human-readable version of memory tags,  it becomes a little bit clear. The following example and the output is a good way to understand the behavior of the 'is' operator. Here we took three variables p,q, and r. Then we initialized p and q with the same content. In the third line of code, we make p and r equal. As expected, although the content of the p and q are the same, they are pointing to the different locations in the memory. The code snippets will clarify the rest-



2.' is not' operator: just the opposite of the 'is' operator. 






Monday, January 20, 2020

Python Practice Problem 2: Print the Fibonacci series up to a input point

Let us go to practice problem 14 ( https://www.practicepython.org/exercise/2014/04/30/13-fibonacci.html) and solve the problem of the Fibonacci series. If you want more information on this special type of numbers you can google the term or you can go to https://en.wikipedia.org/wiki/Fibonacci_number, they have some practical examples also.
I am putting my solution here .

Tuesday, January 14, 2020

Python Practice Problem 1: Finding the List of divisors of an integer number

Well, let us do exercise number 4, which is finding the divisors of an integer number. There are many ways to find a solution. Let's outline the objective and possible solution.

Objective:
1. User will input an integer
2. Find and Print the divisors
3. Also, Count the number of divisors

My First solution is given below.

This code can be modified a little bit to include the numbers in the list instead of printing the numbers. Now the modified code appends the divisors to a list. The purpose of this code is to understand the behavior of the empty list and learn how to append the code. The following code is the modified version of the previous one.
If we want to use list comprehension we can be more compact. But, personally, I am not a big fan of list comprehension. My personal opinion against is, sometimes it gets more complex to decode a list comprehension code which goes against the simplicity of Python. However, the following code is the list comprehension version. Again, from an efficiency standpoint, if we look carefully, the code is wasting half of its iterations by finding nothing. Let me explain with an example. Say, we are finding the divisors of 24. The divisors of 24 are [1, 2, 3, 4, 6, 8, 12, 24]. The way this divisor finder program is now working is checking with every number starting from 1, incrementing at one step, checking again and running the same process up to all the way to the input integer.

We already know that the number itself (24 in this case) and 1 is always will be in its divisor list.  And there is no possibility of getting any divisor when the program starts to check beyond half of the number (i.e., after 12 there will be no divisors). So, we can make use of this point by checking only up to half of the numbers. The code is below- Although I did not do any quantitative comparison of efficiency, it should be twice faster than the previous one. Again, other optimizations can be done (e.g., for even inputs the odd numbers cant be a divisor, some error handling for floating-point inputs) but they are not the purpose of this topic.

Pyhton exercises from Michele Pratusevich's website

While looking for python problems (because I do believe the best way to learn is by solving problems) to nurture my python skill I found this website https://www.practicepython.org by Michele Pratusevich. At first look, I liked the website and also sad by the fact that it is not being updated anymore. I will try to practice some problems from there. Will post those problems and my solution here.

Monday, January 6, 2020

Bought a domain through Google Domain

I am not sure why I bought the domain, but it is kind of cool that you have an online existence that has your preferred name and dot.com in it. Again, most importantly, I could use my age-old and favorite nahidxy id to the URL. I still remember opening of my first email id which was a nahidxy@yahoo.com, when I was at grade 8 (probably back in 2004). The xy part of the name was chosen intentionally to keep the id short and unique instead of using some nahid1234... or something like this.
You can also get yours via https://domains.google/