Calculating the total cost of a meal including a tip - using Python
Let’s create a restaurant bill
- Cost of meal: $44.50
- Restaurant tax: 6.75%
- Tip: 15%
We will have apply the tip to the overall cost of the meal (including tax).
So let’s start by defining the variable “meal” and assigning it the value “44.50”
Now let’s create a variable for the tax percentage.
The tax on the receipt is 6.75%. Let’s divide 6.75 by 100 in order to get the decimal form of the percentage.
We assume we got good service, so we would like to leave a 15% tip on top of the cost of the meal, including tax.
Before we compute the tip for the total bill, let’s set a variable for the tip. Again, we need to get the decimal form of the tip, so we divide 15.0 by 100.
We’ve now got three variables we need to perform our calculation on, and we know some arithmetic operators that can help us out.
On Line 7, let’s reassign
meal to the value of itself + itself * tax to simplify it a bit
Now that the new variable of “
meal" has the cost of the food plus tax, let’s introduce on Line 8 a new variable, “
total" equal to the new “
meal" + “
So lets assign the variable “
total" to the sum of “
meal" + “
tip" on Line 8.
Also let’s define the final print value of the total with exactly two numbers after the decimal.
Compile this script and submit it…and you get your total bill.
That’s how to write a script on Python to calculate your restaurant bill
Why keywords and keyword density don’t matter that much in driving SEO
Sometime back keywords were the holy grail of doing SEO along with the right kind of meta nomenclature.
Get the search query - align all kinds of keywords that are related to it, sprinkle those in copious amounts in your copy , tag the life out of it , and boom….Google would index it and throw it up in search results.
Experts went even one step further and defined the number of times a keyword could be repeated in every 100 words of an article to really make the copy stand out in search queries.
Those who couldn’t do anything, they just went and bid and paid a lot of money and bought the whole set of keywords and created a lot of ads.
But then it became a bit complicated…especially around a simple matter. Word and terminology context associated to the search query.
Let’s use as an example - the word “Avatar”.
Now there is that James Cameron movie called Avatar and then sometimes these days we create Avatar’s of our own self in online portals, and then again in vernacular language it can mean something totally different akin to gods and goddesses.
Question is which Avatar are you searching for and how does the Search Engine contextualise that search query to give the right and most relevant answer?
How do you ensure as creator of the movie Avatar, that your films website comes up tops?
Now some years back your website metadata and HTML structure would read something like this below:
<div> <h1>Avatar</h1> <span>Director: James Cameron (born August 16, 1954)</span> <span>Science fiction</span> <a href="../movies/avatar-theatrical-trailer.html">Trailer</a> </div>
The phrases like Science Fiction etc being the related keywords you want indexed.
Now let's decide the section of the page which actually talks about one item; that is
defining the movie Avatar and lets define it using the "element" itemscope
Then it looks something like this:
<div itemscope> <h1>Avatar</h1> <span>Director: James Cameron (born August 16, 1954) </span> <span>Science fiction</span> <a href="../movies/avatar-theatrical-trailer.html">Trailer</a> </div>
By adding Itemscope we have now specified that the HTML contained in the
<div>...</div>block is about a particular item
But it’s not all that helpful to specify that there is an item being discussed without
specifying what kind of an item it is. So we can now specify the type of item using the
itemtypeattribute immediately after the
<div itemscope itemtype="http://schema.org/Movie"> <h1>Avatar</h1> <span>Director: James Cameron (born August 16, 1954)</span> <span>Science fiction</span> <a href="../movies/avatar-theatrical-trailer.html">Trailer</a> </div>
This specifies that the item contained in the <div> is in fact a Movie, as defined in the
schema.org type hierarchy.
Item types are provided as URLs, in this case
Now what additional information can we give search engines about the movie Avatar? Movies have interesting properties such as actors, director, ratings. To label properties of an item, we can use the
For example, to identify the director of a movie, lets add
itemprop="director" to the element enclosing the director’s name. Let’s also for fun define the genre.
(There’s a full list of all the properties you can associate with a movie at http://schema.org/Movie.)
<div itemscope itemtype ="http://schema.org/Movie"> <h1 itemprop="name">Avatar</h1> <span>Director: <span itemprop="director">James Cameron</span> (born August 16, 1954)</span> <span itemprop="genre">Science fiction</span> <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a> </div>
Note that we have added additional
<span>...</span> tags to attach the
itemprop attributes to the appropriate text on the page.
<span> tags don’t change the way pages are rendered by a web browser, so they are a convenient HTML element to use with
Search engines can now understand not just that http://www.avatarmovie.com is a URL, but also that it’s just the URL for the trailer for the science-fiction movie Avatar, which was directed by James Cameron. ( much more context right?- Much more defined search response?)
Sometimes the value of an item property can itself be another item with its own set of properties. For example, we can specify that the director of the movie is an item of type Person and the Person has the properties
birthDate. To specify that the value of a property is another item, you begin a new
itemscope immediately after the corresponding
<div itemscope itemtype ="http://schema.org/Movie"> <h1 itemprop="name"&g;Avatar</h1> <div itemprop="director" itemscope itemtype="http://schema.org/Person"> Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954)</span> </div> <span itemprop="genre">Science fiction</span> <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a> </div>
This in short is the value of Schema’s and why they have become so important in Google’s Knowledge Graph and why it has changed the relevancy of keywords in driving SEO. For Google these days context matters more than keywords.
Here are a few notes to keep in mind when adding schema.org markup to your web pages.
More is better, except for hidden text. In general, the more content you mark up, the better. However, as a general rule, you should mark up only the content that is visible to people who visit the web page and not content in hidden div’s or other hidden page elements.
- Expected types vs text. When browsing the schema.org types, you will notice that many properties have “expected types”. This means that the value of the property can itself be an embedded item (see section 1d: embedded items). But this is not a requirement—it’s fine to include just regular text or a URL. In addition, whenever an expected type is specified, it is also fine to embed an item that is a child type of the expected type. For example, if the expected type is Place, it’s also OK to embed a LocalBusiness
Using the url property. Some web pages are about a specific item. For example, you may have a web page about a single person, which you could mark up using the Person item type. Other pages have a collection of items described on them. For example, your company site could have a page listing employees, with a link to a profile page for each person. For pages like this with a collection of items, you should mark up each item separately (in this case as a series of Persons) and add the url property to the link to the corresponding page for each item
So next time when your search agency comes with their list of keywords and meta tags, get your web developer and content writer as well. Sit together. Define the context and markup your page HTML with schemas. Painstakingly and with time. Use those keywords in the base HMTL program itself. Just writing more inane content with a mix of those words is not going to improve your ranking.
Not anytime soon.
Then start doing SEO.
PS: Oh and yes use a code compiler to test the code you write post markup before publishing.
Writing a simple calculator program on Python
This is me just writing to remember basic stuff am learning. Writing things I read helps me remember them better.
So now let’s start by explaining what we expect a simple calculator program to do in simple understandable english
I first read this, then tried it on DOS, typed this on IDLE and then just pasted it from there.
START PROGRAM print opening message
while we let the program run, do this: #Print what options you have print Option 1 - add print Option 2 - subtract print Option 3 - multiply print Option 4 - divide print Option 5 - quit program ask for which option is it you want
if it is option 1: ask for first number ask for second number add them together print the result on screen
if it is option 2: ask for first number ask for second number subtract one from the other print the result on screen
if it is option 3: ask for first number ask for second number multiply! print the result on screen
if it is option 4: ask for first number ask for second number divide one by the other print the result on screen
if it is option 5: tell the loop to stop looping
Print onscreen a goodbye message END PROGRAM
Pretty simple, where my simple calculator can only do 4 things
Since anything beyond that right now is too much for my brain to capture and process
Now let's try writing it in a way that Python understands by defining functions and parameters
# calculator program # NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER # Here we will define our functions # this prints the main menu, and prompts for a choice def menu(): #print what options you have print "Welcome to calculator.py" print "your options are:" print " " print "1) Addition" print "2) Subtraction" print "3) Multiplication" print "4) Division" print "5) Quit calculator.py" print " " return input ("Choose your option: ") # this adds two numbers given def add(a,b): print a, "+", b, "=", a + b # this subtracts two numbers given def sub(a,b): print b, "-", a, "=", b - a # this multiplies two numbers given def mul(a,b): print a, "*", b, "=", a * b # this divides two numbers given def div(a,b): print a, "/", b, "=", a / b # NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN loop = 1 choice = 0 while loop == 1: choice = menu() if choice == 1: add(input("Add this: "),input("to this: ")) elif choice == 2: sub(input("Subtract this: "),input("from this: ")) elif choice == 3: mul(input("Multiply this: "),input("by this: ")) elif choice == 4: div(input("Divide this: "),input("by this: ")) elif choice == 5: loop = 0 print "Thankyou for using calculator.py!" # NOW THE PROGRAM REALLY FINISHES
That's it really.
Only 34 lines of code to make a calculator add/subtract/divide and multiply
And now I need to rest my overworked brain.
Thank you supremely intelligent denizens of the internet to make all this free.
Experimenting with a Billy Joel Cover of Piano Man.
This is just a cover version attempt. No part of this song has been originally written, composed or arranged by me. The original rights belong to Billy Joel and Columbia Records and it was first published by Billy Joel and Columbia Records in 1973/74.
However all music arrangements on this cover version have been done by me.
Instruments used: Korg Workstation
Harmonica: Hohner Blues Harp
Mixed using LogicPro
In the quest of simpler blogging platforms and on a tangent - gitHub pages
Those who are prolific bloggers sooner or later come to this question. “Which is the best, most easiest way for me to write and publish online”?
Just concentrate on content without the frills and fancies and write on the go without having to bother about software updates, comments, database updates, server outage, security hack, spamming, html code and those thousand things which after a point of time become irritatingly time consuming and add very little value to the actual content you are shipping.
Most developer turned bloggers, have an affinity for blogging platforms which are based on the nuances of Markdown, originally created by John Gruber back in 2004.
So what is Markdown?
Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).
Thus, “Markdown” is two things: (1) a plain text formatting syntax; and (2) a software tool, written in Perl, that converts the plain text formatting to HTML. See the Syntax page for details pertaining to Markdown’s formatting syntax. You can try it out, right now, using the online Dingus.
They prefer markdown because of its ease. And there there are few bloggers out there in the world who have more relevant traffic than John Gruber who himself uses markdown and who earns much much more in a week by blogging than most bloggers do in a lifetime.
Jekyll is one such platform which can convert your plain text into static websites and blogs. This is not the only platform that does this, but am writing about it, since I have been tinkering with it for a while.
- No databases
- No comment moderation
- No updates
- It uses Markdown, Textile or Liquid as per what you are comfortable with
- Ready for deployment static sites
- Allows you to have categories, pages, permalinks, custom layouts etc
Jekyll follows Liquid templating language to process templates. You can check it out here.
And last but not the least, if you are a developer and want to share code on your blog, then markdown sites are way better. So much so that gitHub which launched gitHub pages sometime back to allow developers to create project websites, uses Jekyll to run gitHub pages.
And it’s way easy to install and get started.
So give it a shot - and create a blog using beautiful Markdown syntax where you just have to bother about what you write. It;s a great experience to try it once at least.
PS: If you are however, the kind of blogger who likes comments then stick to Wordpress.
EasyPost - an API to add shipping to your product site
You started on a hunch at 3 am in the morning and created those first tee-shirts
You got them printed.
You even created a site to showcase the designs.
Then you plugged in an easy e-commerce platform like WooCommerce for payments.
And now you have got customers in Iceland.
Question is: How do you ship from your site easily ?
If you are a bootstrapping e-commerce site, trying to sell your awesome self made t-shirts or posters or mugs, by integrating shipping to your site can be at times a multi month negotiation and integration process.
From tracking to standards to tech integration.
EasyPost makes it simple.
EasyPost is a flexible, modern RESTful API that makes it easy to add shipping to your app. It connects to all the major carriers easily without the hassle and with client libraries for Java, Node, PHP, Python, Ruby, and .NET, it allows you to integrate with whatever development set up you have.
Carriers include, USPS, FedEx, UPS, and DHL.
It allows you to enable your customers to purchase from your site in real time shipping with one call to the API. You get great commercial USPS rates out of the gate and can use your own negotiated rates with other carriers as well.
It even has a Customs API, for international shipping thereby making it easier for you to never fill up those endless custom forms manually.
So if you are a developer looking for an easy shipping app to add to the e-commerce website you are building or a bootstrapping entrepreneur of an e-commerce venture you can get started with EasyPost here.
Neat! Simple. Plug and play.
The beautiful world of API’s.
Image Courtesy - EasyPost.com
An app for learning Python
Well, maybe not quite learning it end to end. But quite good to test whether you know what you need to know.
Quiz & Learn Python is an app for testing and improving your knowledge of Python concepts! Questions range from basics of programming to highly Python specific, possibly unexpected ways to write your code.
It’s in the realm of the modern day tutorial meets gamification kind of union- making learning a little bit more fun, with scores and points to get that competitive streak inside you all worked up.
And yes, it even has lifelines. ( Just like the quiz shows you watch on TV!!)
The coolest feature of this app, however, is the debug lifeline which allows you to step through the code line by line, backward and forward, and see all function calls and variable value changes.
Photo- courtesy : Mobileicecube.com
Creating a self hosted app platform for agile dev teams
Building applications is a tiresome, as well as a time consuming process. Mostly because while you are in the dev/demo mode, you need to keep producing newer features, tweak existing features until it all falls into place.
However, in most cases, you have to create a demo environment to show that new feature, which means working on the server, updating files and so on. The problem is complicated by multiple developers working on different parts of the same application - some working on new code, some working on debugging, some creating newer features and so on.
Then comes the feedback loop - and before you know you are back changing lines of code again - updating graphics again - updating team members of changes that need to be driven.
With Kite, you can create a self hosted app environment. It helps build an on-demand development environment ready to build and show off new features on the go - that too on your own servers.
It also allows your databases and entire dev environment to be instantly accessible as well to all members of the team - a critical benefit for dev teams working together on the same application but in different geographies. No more hassles of server access and security given that you can literally clone anything you have coded on Kite. So for dev teams who are working in agile environments it can be a particularly neat addition.
This is not the only tool which can do this. But it’s pretty cool if you want to give it a spin.
Here’s a nice demo video for you to check it out.
Writing content ? Here’s some Orwellian advice which kinda nails it
George Orwell - in his essay titled Politics and the English Language
" A scrupulous writer, in every sentence that he writes, will ask himself at least four questions, thus:
What am I trying to say?
What words will express it?
What image or idiom will make it clearer?
Is this image fresh enough to have an effect?
And he will probably ask himself two more: Could I put it more shortly?
Have I said anything that is avoidably ugly?
But you are not obliged to go to all this trouble.
You can shirk it by simply throwing your mind open and letting the ready-made phrases come crowding in. They will construct your sentences for you — even think your thoughts for you, to a certain extent — and at need they will perform the important service of partially concealing your meaning even from yourself. It is at this point that the special connection between politics and the debasement of language becomes clear.”
No one can teach anyone to become a writer. There are no “ten tips” to becoming a writer. There are no tuition classes and expert tips to becoming a writer.
The 1000 articles that you read on this subject online, are click-baits driven by slideshows, racking up page views and ad rates.
Instead try this.
Read. Read a lot. Read different authors across different genres.
Then try emulating some of them. Copy their style if you feel like. It’s ok. It’s just a learning process.
Do it for a few years and you may just end up finding a voice. Your voice.
And then let it flow.
The rest doesn’t matter.
You will know you are good, when people who DONT know you take considerable pains to tell you that you are. They will even pay to read what you have written.
Until then you are not good. You are just trying, just like most of us.
PS: While you are at it, also try reading a bit of Orwell, Wodehouse, Dickens and some of the other victorian oldies. They are gold. Even now.