Monday, December 30, 2019

Nested Loops

Welcome Friends!

In today's blog, we will discuss nested loops. So, what is a nested loop? A nested loop is a loop within a loop. To explain this, let's take an example of a basic loop and then a nested loop, assuming we want to print a table of 5.

# Print a table from 2 to 7
for i in range(1, 11):
    result = 5 * i 
    print("5 * " + str(i) + " = " + str(result))

The result will look like this:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Now, if we want to print similar tables for number 2 to 7, we will have to add another loop to iterate through numbers from 2 to 7. Here is an example.   
# Print a table from 2 to 7
for number in range(2, 8): 
    for i in range(1, 11):
        result = number * i 
        print(str(number) + " * " + str(i) + " = " + str(result))


Once you execute the statement, we will see tables from 2 through 7.

A nested loop doesn't necessarily have to be only for "for" loop, it can be for a "while" or "if" loop or a combination of any. To explain this, let's try to find all even numbers between 1 and 10.
# Print an even number between 1 and 10
for number in range(1, 11): 
    if number % 2 == 0: 
        print(str(number) + " is an even number")
2 is an even number
4 is an even number
6 is an even number
8 is an even number
10 is an even number

I hope you have enjoyed this. In our next post, we will play around with formatting. Stay tuned.                      

Friday, December 27, 2019

Iteration

Today, we are going to learn about loops. There are two kinds of the loop, first, where we know the number of times we have to perform the task and are known as a definite loop (e.g. for loop), secondly, the loops where we are unsure how many times we need to iterate the steps and are known as indefinite loop(e.g. while loop). We will see these two loops and their usage.

Let's start with for loop. Suppose we have a student, for whom we need to get the total marks obtained and calculates his percentage. This program will ask the student to enter marks for 5 subjects and assuming each subject was of maximum 100 marks.
total_marks = 0
for subject in range(5):
    marks = float(input("Enter the marks obtained in subject "
                        + str(subject + 1) + ": "))
    total_marks += marks

average = total_marks / 500
percentage = average * 100

print("Your total marks are: " + str(total_marks))
print("Your percentage is: " + str(percentage) + "%")

If we execute this, we will get a result something like below:
Enter the marks obtained in subject 1: 79
Enter the marks obtained in subject 2: 89
Enter the marks obtained in subject 3: 93
Enter the marks obtained in subject 4: 91
Enter the marks obtained in subject 5: 83
Your total marks are: 435.0
Your percentage is: 87.0%

Let's now look at while loop. We use while loop to iterate the same steps, but we are not sure how many times we have to execute it, for example, we often see promotions like, offer valid till so and so date or until stock lasts. They are not sure how many people will avail the offer.

We will write a program to ask the user for a number and check if it is an even or odd number and then display it. It will prompt until the user quits.
Note: To check if a number is even or odd, we will have to divide it by 2 and check for its remainder. In order to do it, use the % operator.
num = input("Enter a number or q to quit: ")
while num != "q":
    if int(num) % 2 == 0:
        print(num + " is an even number")
    else:
        print(num + " is an odd number")
        num = input("Enter a number or q to quit: ")
print("Thank you!")

Run this and keep trying with some numbers and check the result.
Enter a number or q to quit: 12
12 is an even number
Enter a number or q to quit: 15
15 is an odd number
Enter a number or q to quit: 44
44 is an even number
Enter a number or q to quit: 25
25 is an odd number
Enter a number or q to quit: q
Thank you!

Hope you have enjoyed programming it. In our next post, we will play around with more controls. Stay tuned.

Tuesday, December 24, 2019

Getting started with simple programs

Welcome Friends,

Today we are going to write some simple programs. Let's open pycharm and create a python file with name as sum.py. Our first program will add two numbers and then it will display the sum of these two numbers. 

  a = 10  
  b = 20  
  total = a + b  
  print("Sum of two numbers is: " + str(total))

now, to run this, right-click and click on "Run sum.py". It will display a message as 


Sum of two numbers is: 30


Let's take another example to add two numbers that are provided by users. To get value from user, input() function will be used. This input function will get the value as a string, so we need to convert it into an integer.

  a = input("Enter the first number: ")
  b = input("Enter the second number: ")
  total = int(a) + int(b)
  print("Sum of two numbers is: " + str(total))

if we run this we will see a message something like this:


Enter the first number: 20
Enter the second number: 15
Sum of two numbers is: 35


Now, instead of displaying "Sum of two numbers is" if we display the actual numbers in the statement, it will make more sense. Also, to convert the number from string to int, we will do this when we are getting the value from the user. Let's have a look at this now:


a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
total = a + b
print("Sum of " + str(a) + " and " + str(b) + " is: " + str(total))


Result will be
Enter the first number: 20
Enter the second number: 30
Sum of 20 and 30 is: 50

So far so good. Let's now find the largest of 2 given numbers provided by a user. For now, we will assume the two numbers will not be the same. To compare numbers we use logical operators:
  <   smaller than
  >   greater than
  <= smaller than or equal to
  >= greater than or equal to
  == equal to


a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))

if a > b:
    print(str(a) + " is greater than " + str(b))
else:
    print(str(b) + " is greater than " + str(a))


Note: In python, indentation is very important. In the above example, if and else are reserved words and at the end of these lines, we have to put a colon(:). Now, let's run this

Enter the first number: 10
Enter the second number: 20
20 is greater than 10


Hope you have enjoyed programming it. In our next post, we will play around with more controls. Stay tuned.

Friday, December 20, 2019

Data types in Python

Welcome Friends,
Before we begin our programming, let's get used to with the various basic data types. 

1.    Integer (int): It takes the whole number including negative values for example 0, 1, 2, 3 etc as well as a negative number like -1, -2, -3, ...


If you notice, when we added these two number, it simply concatenated the numbers. We first need to convert these numbers into strings and then add it.

2.    Float (float): It can take decimal values, for example, 3.14, 7.99, 15.23 etc.


3.    String (str): It is a sequence of characters, for example, "Hello", "Welcome to Python world"
Note: you can assign a string variable in either single quotes (like ‘Hello’) or double quotes (like “Hello”)

4.    Boolean: It takes only two values, i.e. True or False


5.    List: It is a collection of objects or values. It is denoted by []

To add, use the command, append

To remove an element from the list using the keyword remove

6.    Tuple: It is similar to list but once it is created it cannot be changed and it is denoted in the () bracket.

7.    Dictionary (dict): It is used to store data in key-value pair and it is stored in {}


Hope, now we can get some basics with the data types and in our next blog, we will write some simple programs. Stay tuned.



Friday, December 13, 2019

First thing first

Welcome Friends,
Let's start our journey to learn python. In today's blog, we will install the latest version of python (i.e. v 3.8). It's a very straight forward to install.

Go to Python website and download it using the below link:
https://www.python.org/downloads/

If you have a 64-bit Windows machine, then you can download the 64-bit version of python from
https://www.python.org/downloads/release/python-380/

Once above is installed, let's install pycharm.
https://www.jetbrains.com/pycharm/download/#section=windows

After this is installed, let's launch pycharm. Now, we need to set up the interpreter. For this, click on Run --> Edit Configuration
Select the Python interpreter as Python 3.8

Once we are done, we are good to start with our first python program.

On pycharm, click on File --> New --> Python File
Enter the file name as "hello" and hit enter. This will open a new file with name as "hello.py"

Let's write "Hello World" with our first python program.
Write below in pycharm:
print("Hello World")

Now, right-click anywhere on the screen and select "Run hello.py"

Bingo! We completed our first program in python.

In our next post, we will get into more details. Stay tuned. Till then take care,

It's not late yet...

It's not late yet...

As there is a saying, it's never late to start especially something new.

I was working with a US-based IT service provider. I was doing good and got a few opportunities to travel and work from different countries. Things were smooth and every passing year, I was getting more in a comfort zone. At the same time, I was realizing, if I don't change, things will not be the same. We all know what happened with Nokia phones and the Kodak camera if you don't keep pace with the market changes. It took me over a decade to move out of my comfort zone and start learning these new technologies. I will start my journey with python and heads towards data science and then to Artificial Intelligence and Machine learning.

Although there is vast ocean of information available on internet, but we frequently struggle to know from where and how to get things installed. In this blog we will describe each step so as to make it easy for all of us. 

In the upcoming posts we will get started with our new journey. Stay tuned...