Saturday, June 20, 2020

Date Time

Sometimes it becomes very important to use date and/or time in our code. In this session, let's try to see how we can get current date time and convert it into different formats. For this we need to import datetime module.
import datetime

current_date_time = datetime.datetime.now()

print(current_date_time)


2020-06-21 14:52:14.623809
Let's now try to get today's date or say only current day/month/year.
import datetime

today = datetime.date.today()

print("Today's Date:", today)
print("Today's day:", today.day)
print("Today's month:", today.month)
print("Today's year:", today.year)

Today's Date: 2020-06-21
Today's day: 21
Today's month: 6
Today's year: 2020
Now, let's try to convert it into different format.
import datetime

today = datetime.date.today()

print("Date in dd/mm/yyyy: ", today.strftime("%d/%m/%Y"))
print("Date in mmm dd, yyyy: ", today.strftime("%b %d, %Y"))

Date in dd/mm/yyyy:  21/06/2020
Date in mmm dd, yyyy:  Jun 21, 2020
Similarly, we can get and format time
import datetime

current_date_time = datetime.datetime.now()

print('Current Time (0-24):', current_date_time.strftime("%H:%M:%S"))
print('Current Time (0-12):', current_date_time.strftime("%I:%M:%S %p"))
print('Current Hour (0-24):', current_date_time.strftime("%H"))
print('Current Hour (0-12):', current_date_time.strftime("%I %p"))
print('Current Minute:', current_date_time.strftime("%M"))
print('Current Second:', current_date_time.strftime("%S"))


Current Time (0-24): 02:54:25
Current Time (0-12): 02:54:25 PM
Current Hour (0-24): 14
Current Hour (0-12): 14 PM
Current Minute: 54
Current Second: 25
Here are some formats that can be used
Format Code Description Example
%Y YYYY 2020
%y yy 20
%m mm 06
%b mmm Jun
%B mmmm June
%d dd 21
%H 0-24 14
%i 0-12 02
%p AM/PM PM
%M Minute 55
%S Seconds 32

Thursday, February 13, 2020

Sets

Set is a collection of distinct elements same as we have in mathematics.

How can set help us?
If we have a list with duplicate items and we need a list with a unique item then convert the list to a set. We can also find union, intersection or difference between two sets.

Find unique elements
# Get the unique list
num_list = [1, 2, 3, 4, 2, 2, 3, 1]
num_list = set(num_list)
print(num_list)


{1, 2, 3, 4}

Add an element in the set
# Add a list of elements
new_items = [5, 6, 7, 8]
num_list.update(new_items)
print(num_list)


{1, 2, 3, 4, 5}

Add a list of elements
# Add a list of elements
new_items = [5, 6, 7, 8]
num_list.update(new_items)
print(num_list)


{1, 2, 3, 4, 5, 6, 7, 8}

Now, let's perform some basic operation on two sets such as union, intersection and difference of these sets.

Union
# union of two sets
set_a = {1, 2, 3, 4}
set_b = {2, 4, 6, 8}
print(set_a.union(set_b))


{1, 2, 3, 4, 6, 8}

Intersection
# intersection of two sets
set_a = {1, 2, 3, 4}
set_b = {2, 4, 6, 8}
print(set_a.intersection(set_b))


{2, 4}

Difference
# difference of two sets
set_a = {1, 2, 3, 4}
set_b = {2, 4, 6, 8}
print(set_a - set_b)


{1, 3}

Let's try to find out if a set is subset or superset of a given set.
Subset
# subset
set_1 = {1, 3, 5}
set_2 = {1, 2, 3, 4, 5, 6}
set_3 = {1, 3, 5, 7}
print('set_1 is subset of set_2:', set_1.issubset(set_2))
print('set_3 is subset of set_2:', set_3.issubset(set_2))


set_1 is subset of set_2: True
set_3 is subset of set_2: False

Superset
# superset and subset
set_1 = {1, 3, 5}
set_2 = {1, 2, 3, 4, 5, 6}
set_3 = {1, 3, 5, 7}
print('set_3 is superset of set_2:', set_3.issuperset(set_2))


set_3 is superset of set_2: False

Sunday, February 2, 2020

Data Analysis for new store location


ABC Bookstore

1. Introduction
As Nelson Mandela says "Education is the most powerful weapon which you can use to change the world". Education not only makes a person knowledgeable but also helps them grow and grab better opportunities in their life to be successful. It also makes an individual more responsible, take the right decisions and develop them personally and socially. It also helps one to do their daily activities in the best possible ways. It helps in acquiring knowledge and new skills that will impact their personal development and for their country. To study, one needs to have access to good resources of books and a book store. 

2. Business Problem
ABC company is located in New York, is a known and independent book store focused on staff curated book selections. Stores are generally located near a school mainly helping students with academic books and stationaries. The bookstore will offer a wide selection of books including children’s literature, modern fiction, true crime, cookbooks, foreign language titles and art books. The cosy bookstore will also offer a book club, author events and children’s story hour and gift-wrapping section. ABC company is looking to help students and community by opening a new branch in Sydney, Australia.
To open a bookstore, a few things that will be considered while finalizing the location. Below are the important things that need to be taken into consideration:
1.     The bookstore should be located within a 2 km radius of a school.
2.     The school rating should be 7 or above out of 10.
3.     There should not be more than 1 bookstore within a 2 km radius of the school.
4.     There should be a bank/atm in the vicinity.

3. Data Location Gathering:
In Sydney, there are a lot of schools but very limited bookstore available. In order to finalize an optimum location, data from Foursquare.com will be gathered. It will also help us grab the rating for different schools along with the number of bookstores or bank available in its approximation areas. Foursquare has rich information available and above all, all of this information can be extracted by using their API service. This API service will gather a list of all the schools available in Sydney along with their rating. Once all the schools' location (latitude and longitude) and filtered with rating 7 or above, we will look for other criteria such as the number of other bookstores and bank/atm availability.
In order to generate the best location, we would need to find
·       schools in Sydney
·       schools average rating
·       number of existing book stores around the school
·       other facilities such as bank/atm
We will look for schools within a radius of 30 Km from Sydney. For the existing bookstore or bank/atm we will search within 2 km of radius from school.

4. Data Acquisition and cleaning

4.1  Data Sources

Retrieving the data is the most important aspect of any data analysis. For our analysis, we used Foursquare API to pull a list of all the schools and list of all book stores, banks and ATM’s near each school that was required for the data analysis. There are various sources that provide the data but either they do not have any API service to use or have a very limited number of calls we can make through their website for free. Foursquare helped us by letting us scrap most of the data in the first couple of days.

4.2  Data Cleaning

Data scrapped from various sources were combined, cleaned and stored in various dataframe for analysis. There were several problems with the data available.
Firstly, there were missing data that we couldn’t use, hence needs to be dropped.
Secondly, the datatypes were converted to the proper format
Thirdly, some of them were having null values that were replaced will 0.

4.3  Data Selection

While considering the data, we need to make sure we use the data from the correct location. For that, we used a radius of 30 km of Sydney location. It retrieved 100 schools. Later, we also need to retrieve their average rating. And finally, we need to pull a list of all book stores, banks, atm within 2 km radius of each school.

5. Exploratory Data Analysis

Once we retrieved and data was pre-processed, we plotted all our schools, book stores, banks and atm’s on the map to visualize it further (Figure 1).


Fig 1: Location of all 100 schools, ~800 book stores and ~400 Banks/ATM’s in Sydney

This image has around 800 book stores, 400 banks/atm and 100 schools.

There were few places which have a staggering number of book stores within 2 km of these schools. Some of the schools have 35 book stores around them. This was again plotted to see where these were located. There were 8 schools around which more than 30 book stores were present within its 2 km radius (Figure 2).



Fig 2: Location of 8 schools having 30+ book store existing around each school in Sydney

Finally, after sorting and filtering our data, we were able to gather our prime candidates with least number of book stores in its vicinity and have at least 1 bank/atm with its 2 km radius. Figure 3 shows the locations of these schools.


Fig 3: Location of 7 schools having 1 book store and at least 1 bank/atm around each school in Sydney


6. Results and Discussions
Our results show that although we had 100 schools with around 800 books store and approximately 400 atm’s/banks within a radius of 2 km from the schools, there are few schools where a number of books store was minimal. There were few schools around which a staggering number of books stores existed (35 book stores). Most of these schools were located in the Central/Financial district of Sydney. Our potential location candidate for book stores was in the suburbs of Sydney.
With narrowing down all the available schools and as per the requirement, there were 7 schools that have at least 1 atm/bank and at max 1 book store within its vicinity. These schools are located in:
1.     one school in the upper north suburb of Sydney
2.     two schools in the western suburb of Sydney
3.     two schools in the south-west suburb of Sydney and
4.     two schools in the eastern suburb of Sydney

7. Conclusions

Purpose of this project was to identify an optimal location for opening a book store with the least number of bookstores around a school and have at least one bank or atm near it. Identifying these schools, existing bank stores and banks or ATM's according to with the help for Foursquare API's has helped achieve our requirement by the exploration of these data points and can be shared with the stakeholders.

Based on the results, a stakeholder can take an appropriate decision based on other specific characteristics such as real estate price and availability, number of people living in that neighbourhood, accessibility to parking, public transportation in each recommended zone. They can also take into considerations like accessibility to highway or types of lease available for the shop.  



Wednesday, January 15, 2020

File Handling

Welcome Friends!

Being a programmer, it is important that we should be able to read and write data to a file. In today's blog, we will discuss how we can read and write to a file. 

There are various modes to open a file. For example:

  • w - to create a new file and write. If the file exists, it will overwrite it.
  • a - to append at the end of the file.
  • r - to read the file

Let's first create a new file
# Open a txt file with name as 'sample'
text_file = open('sample.txt', 'w')

for i in range(1, 10): 
    # Write to a file  
    text_file.writelines("Hello there! We are at line number {}\n".format(i))

# close the file
text_file.close()

To append the file, change the mode from "w"(write) to "a". 
# Open a txt file with name as 'sample'
text_file = open('sample.txt', 'a')

for i in range(1, 10): 
    # Write to a file  
    text_file.writelines("Hello there! We are at line number {}\n".format(i))

# close the file
text_file.close()

To read the file line by line, use the mode as "r".
# Open a txt file with name as 'sample'
text_file = open('sample.txt', 'r')

for each_line in text_file: 
    # Read to a file  
    print(each_line)

# close the file
text_file.close()


I hope you have enjoyed this. Stay tuned for more update.


Saturday, January 11, 2020

Exception Handling

Welcome Friends!

Today we are going to discuss Exception Handling. Well, sometimes our python or user don't respond to the code it is designed and result in errors or exceptions. Let's take an example to understand it better. Suppose our program expects 2 numbers and it performs addition on these numbers and gives the result. Now, if a user enters a number and a string, python will not be able to convert string to a number and will throw an error. To capture these and display a meaningful message on screen we use try and except in python.
num_1 = int(input("Enter 1st number: "))
num_2 = int(input("Enter 2nd number: "))
total = num_1 + num_2
print("Sum of two numbers are {}".format(total))

Now, if we run this we will get an error if we provide a string as an input.
Enter 1st number: 78
Enter 2nd number: u89
Traceback (most recent call last): 
    File "D:/Python_Data/new2python.py", line 3, in <module>
      num_2 = int(input("Enter 2nd number: "))
    ValueError: invalid literal for int() with base 10: 'u89'

No one would like to see this kind of messages. Let's display some meaningful message by capturing this kind of scenario.
try:
    num_1 = int(input("Enter 1st number: "))
    num_2 = int(input("Enter 2nd number: "))
    total = num_1 + num_2 
    print("Sum of two numbers are {}".format(total))
except: 
    print('Invalid number entered. Please try again')

And if we run this and make a mistake, we will see a message that the user has entered an invalid number and can retry.
Enter 1st number: 44
Enter 2nd number: r55
Invalid number entered. Please try again

Well, this can be put inside a while loop so that it will keep repeating until the user provides a valid input.

Let's take another scenario, our program will ask the user to provide two numbers and the program will divide these numbers and displays results. What if the user enters 0 (zero), in this case, we need to display a different error message. Let's try to fix it.
try:
    num_1 = int(input("Enter 1st number: "))
    num_2 = int(input("Enter 2nd number: "))
    total = num_1 / num_2 
    print("Division of two numbers are {}".format(total))
except ValueError: 
    print('Invalid number entered. Please try again')
except ZeroDivisionError: 
    print('Cannot divide by zero. Please try again')
except: 
    print('Something really went wrong')

If we run the above code, we will be able to capture the specific error message.
Enter 1st number: 43
Enter 2nd number: 0
Cannot divide by zero. Please try again

By now you must be wondering, how can we know the exception values we need to use? It's simple, just run your code without try and except and once you get the error message, it will let you know the type of message you can use to capture these kinds of scenarios.

When the code goes in production, there can be unknown errors and in that case, we don't want to see a generic error message rather we would also like to capture the exception. To capture the exception and display, let's try this one out.
try:
    num_1 = int(input("Enter 1st number: "))
    num_2 = int(input("Enter 2nd number: "))
    total = num_1 / num_2 
    print("Division of two numbers are {}".format(total))
except ValueError: 
    print('Invalid number entered. Please try again')
except Exception as e: 
    print('Something really went wrong.\nError message: {}'.format(e))

Now we know how to handle and capture the exceptions, there is one last piece remaining in this section. The final block is known as finally. so it goes like, 
   try: 
      ... 
   except: 
      ... 
   finally: 
      ... 

Like the name suggests, it will be executed irrespective if there is an error or not. In our above example, after the user has provided the numbers and after displaying, we would like to display a thank you note for taking their time to test. Let me show you how we can write it up.


try:
    num_1 = int(input("Enter 1st number: "))
    num_2 = int(input("Enter 2nd number: "))
    total = num_1 / num_2 
    print("Division of two numbers are {}".format(total))
except ValueError: 
    print('Invalid number entered.')
except ZeroDivisionError: 
    print('Cannot divide by zero.')
except Exception as e: 
    print('Something really went wrong.\nError message: {}'.format(e))
finally: 
    print('Thank you for your time. Have a good day!')

I hope you have enjoyed this. In our next post, we will discuss how to open and write to a file. Stay tuned. 

Monday, January 6, 2020

Functions

Welcome Friends!

Today we are going to discuss Functions. Functions are a set of instructions or a program that performs a specific task. Then one may ask, how is it different from what we were doing until now? Well, a function is used to segregate the tasks and avoid duplication of activity. Let's take a simple example to illustrate its use.

Assume, if I want to calculate how much money we can save at the end of 2 years. The program will ask the user for the principal amount and rate of interest and program will calculate simple interest and will display the amount and then will again ask the user again for the amount invested for the second year and will display the amount saved by end of 2nd year. A typical approach will be, 
Get the rate of interest from the user for 1st Year
Calculate Simple Interest
Calculate the Amount
Display the amount and Interest
Get the principal from the user for 2nd Year
Get the rate of interest from the user for 2nd Year
Calculate Simple Interest
Calculate the Amount
Display the amount and Interest

Let's see how a function can help us here.
Call function 1 to get user principal and rate of interest
call function 2 to calculate and display simple interest and Amount
Call function 1 to get user principal and rate of interest
call function 2 to calculate and display simple interest and Amount

function 1:
    Get the principal from user
    Get the rate of interest from the user

function 2:
    Calculate interest
    Calculate the Amount
    Display Amount and Interest
Syntax:
The function is defined using the keyword def, and it will return the value with the return keyword
   def function_name(arguments):
       do something here ...
       return value

Let's now write our program using the function.
def get_details_from_user():
    principal_amount = float(input("Enter the principal amount: "))
    rate = float(input("Enter the rate of interest: ")) 
    return principal_amount, rate
    
def calculate_and_display_simple_interest(principal_amount, rate):
    interest = principal_amount * rate * 1/100  
    amount = interest + principal_amount 
    print("Your interest on $" + str(principal_amount) + " is " + str(interest)) 
    print("Your amount at the end of year is: " + str(amount) + "\n")
    
def main(): 
    # get user details for 1st year from user  
    principal_amount, rate = get_details_from_user() 
    
    # Calculate and display the amount and simple interest  
    calculate_and_display_simple_interest(principal_amount, rate) 
    
    # get user details for 2nd year from user  
    principal_amount, rate = get_details_from_user() 
    
    # Calculate and display the amount and simple interest  
    calculate_and_display_simple_interest(principal_amount, rate)

main()

I hope you have enjoyed this. In our next post, we will discuss exception handling. Stay tuned. 

Sunday, January 5, 2020

Formatting

Welcome Friends!

Let's see more in the formatting of strings today.

There are various ways in which we can format the text being displayed using the print statement.
For example, if we want to print the price of flowers as $3.50.
item_name = "flower"
item_price = 3.5
item_name = "flower"
item_price = 3.5
print("1. Price for", item_name, "is $", item_price)
print("2. Price for " + item_name + " is $" + str(item_price))
print("3. Price for {} is ${}".format(item_name , item_price))
print(f"4. Price for {item_name} is ${item_price}")

If we run this, we will see our output as:
1. Price for flower is $ 3.5
2. Price for flower is $3.5
3. Price for flower is $3.5
4. Price for flower is $3.5

You can use whichever option you like or can remember it. The item price doesn't look quite right. We now need to round this to 2 decimal places. Let's try it out.
item_name = "flower"
item_price = 3.5
print("1. Price for", item_name, "is $", "{:.2f}".format(item_price))
print("2. Price for " + item_name + " is $" + "{:.2f}".format(item_price))
print("3. Price for {} is ${:.2f}".format(item_name, item_price))
print(f"4. Price for {item_name} is ${item_price:.2f}")

And the result for this will be:
1. Price for flower is $ 3.50
2. Price for flower is $3.50
3. Price for flower is $3.50
4. Price for flower is $3.50

Let's take another example of formatting. In this case, I will use option 3 but you can use any of the options and it will work the same way.
name = "Learning Python"
date = "05-Jan-2020"
item_name_1 = "Flowers"
item_name_2 = "Cake"
item_price_1 = 33.50
item_price_2 = 9.99
tax = .05 * (item_price_1 + item_price_2)
total = tax + item_price_1 + item_price_2
print("{:>30}: {:<25}".format("Name", name))
print("{:>30}: {:<25}".format("Purchase Date", date))
print("{:>30}: ${:>6.2f}".format(item_name_1, item_price_1))
print("{:>30}: ${:>6.2f}".format(item_name_2, item_price_2))
print("{:>30}: ${:>6.2f}".format("Tax (5%)", tax))
print("{:>30}: ${:>6.2f}".format("Total Price(including 5% tax)", total))

Let's see its output.
                         Name: Learning Python
                Purchase Date: 05-Jan-2020
                      Flowers: $ 33.50
                         Cake: $  9.99
                     Tax (5%): $  2.17
Total Price(including 5% tax): $ 45.66

I hope you have enjoyed this. In our next post, we will discuss functions. Stay tuned.