Python (Chapter 1) : Starting Programming


For any type of consultation, query or doubt. You may contact to the following: (+91) 9804 436 193 debabrataguha20@gmail.com 
and join the group  https://www.facebook.com/groups/331231161093613/ 



Hope you have installed python already and you are able to execute your python codes now.
  • After completing my course you would be able to complete your academic syllabus. But while learning, do not think about the academic syllabus. You just need to trust me. This is made for the people having zero experience in coding world.
  • It would be quick, if you read all the lines without thinking anything else or about any book suggested by your board or university. After 1 month ( yes!! just 1 month) you would be able to read your board/university suggested books & you would be able to understand everything and can do programming by your own.
  • I have written few lines in the chapters instead of putting all the theories in one shot. So after reading the chapter quickly, start solving the exercise. You have to solve the exercise in python interpreter. If you need any help for the installation of python in your smart phone or in your computer, just send me a mail to my mail id. Remember, it would be better to use the version 3 or later versions. If you do not know anything about python version, then simply jump into program. No need to be worried on python versions

Solve all the programs. Once solved, send all the programs to my mail id , I would check and revert you back with all the necessary corrections. But you need to give an honest effort to solve the exercise.



You have only 5 things to learn in this chapters. After that, you will be able to solve all the problems where we do not need FOR loop or IF statement.


1.1 First Thing to learn:

Let’s start with some problems in the real life world. Like if I ask you what is your height and suppose you give me the answer like 6 feet. Now If I ask you that I need the length in cm, because I have to fill an army examination form. it is mandatory there to give your height in cm . So how can you give me the length in cm?

Obvious answer is to search it in google. There are multiple small apps which can convert a length in foot to centimeter equivalent. And the answer would be 182.88 cm. You just need to write “foot to cm converter” in google search engine.


Now write 6 in 1st box and in another box the centimeter equivalent will automatically come.



 So my question is now , how does google transform the length from foot to cm. Or what about those small apps which can convert temperature from Centigrade to Fahrenheit and lots more. So We will start learning how simply we can write this logic by our own.


We can write our code many ways in python. 2 common ways are writing in interpreter and writing in files having extension .py. If you need any help on this, I can show the way separately.


Now If I want to write the simplest code by adding 2 numbers and print the sum, I will write the following code in python:

            a = 10
            b = 5
            c = a + b
print ( c )

Here we are working with 3 variables or boxes. 1 variable or box can hold any value inside. For example, box “a” is holding 10 , variable “b” is holding 5 and initially the variable “c” is not having any value. We would calculate the value of “c” at the runtime.
while running the program, python is putting the sum ( means 15 ) into the variable “c”.


Now, print () line basically prints something in the screen when you execute/run the program.

If we write , print (‘hi’) it would print “hi” in the screen.
If we write , print (‘Debabrata’) it would print “Debabrata” in the screen.
If we write , print (‘Debabrata Guha’) it would print “Debabrata Guha” in the screen.
If we write , print (‘c’) it would print “c”
If we write , print (c) without quotation , it would print the value of c, that is “15” in above program.
If we write , print (‘hi’,c) it would print “hi15”
So If we write , print (‘answer is : ‘,c) it would print “answer is : 15”


Now moving into our 2nd program,

If I ask you to take 2 numbers and show the addition and subtraction of them.

You can write the following program:

a = 10
b = 5
c = a + b
print ( c )
d = a - b
print ( d )

You can also write it in an alternative way like below:

a = 10
b = 5
c = a + b
print ( c )
c = a - b
print ( c )

Here in the alternative way we are calculating the sum and storing it into “c” and printing it , again we are calculating the difference and storing it inside “c” and printing it back.



Now, suppose we have a temperature in centigrade and we want the equivalent temperature in Fahrenheit. In this case we have to use the old physics formula as below:

c/5=(f-32)/9
or, 9c/5=f-32
or, f=(9c/5)+32



c = 16
f = ( 9 * c / 5 ) + 32
print( c , ’ Centigrade ‘ , f , ‘ Fahrenheit ’) 


So the output would be “16 centigrade= 60.8 Fahrenheit

And please be noted, in computer for multiplication you have to use “*” and for division you have to use “/”.





1.2   Second Thing to learn:

Now if we wish to make it more real time like the google app we saw earlier “Foot to cm converter”. We need to allow the user to enter the temperature in centigrade as per their choice and our program will return the temperature in Fahrenheit. Like in google we entered 6 by our choice and in another box it showed 182.88 cm.

So our programming would be


c = int ( input (‘ Enter temperature in centigrade ‘ ) )
f = ( 9 * c / 5 ) + 32
print (c , ” Centigrade = “ , f , ” Fahrenheit ” )



Here you have found 1 new line as underlined above. The highlighted line is being used to prompt a message to user saying “Enter the temperature in centigrade” , or can prompt “Please give a temperature value” etc etc. and then take an input from keyboard and convert it into Integer . Atlast store it inside the variable c.


If we do not write the “int” in the above program. Python will consider the input value as a word and we can not do any mathematical operations with a word. What will be the answer if I try to divide “Debabrata” by 5 ? It is nothing but the stupidity. It does not mean anything. So to convert this word into integer and store it into a variable we will use “int”.


Suppose we will take 2 numbers as input from users and will print the sum and product of them. Then the program will be like following:

a=int ( input(‘Enter 1st number’ ) )
b=int ( input(‘Enter 2nd number’) )
c=a+b
print (‘the sum is ‘ , c ) 
c=a*b
print ( ‘ the product is ‘ , c)


Here we have written input() 2 times to take 2 inputs from keyboard by user.

By default these inputs will be as words , we would use “int” to make the input value to integer.


Some examples of Integer numbers are 0, 1 , 2 , -3 , -9 , 999 etc 
Some examples of floating point numbers are 0.35 , 1.27  , 2.333333  , -3.12 , -9.0 , 999.9999999 etc
Some examples of words are “Hello”, “hi” , “hi5”, “hello100”, “100”, “





1.3   Third Thing to learn:

Now we can create any small app and give it to our younger brother or sister. We can create following apps by the programming logic we learnt.

  • Centigrade to Fahrenheit conversion app.
  • Fahrenheit to Centigrade conversion app.
  • Meter to KM, CM conversion and vice versa. We had a chapter in mathematics in our childhood days.
  • Mensuration mathematics like entering 2 sides of rectangle and print area and perimeter.
  • Area , perimeter calculation for a triangle, circle and square.
  • In algebra, you will be given the value of a & b and you need to calculate (a+b)2

These apps will be useful in mathematics or physics homework. They can quickly complete their homework without doing any calculation and complete their homework in rapid speed like in 5-10 mins instead of spending the full evening. They just need to execute the program written by you and input the values. For giving this idea, I think mathematics teacher will not be happy at all.

So now for the app, where the user will give radius of a circle and will expect the area and perimeter of the same, he/she will get the result always in integer. Because till now, we are dealing with only integers. But in real scenario, area and perimeter of circle almost always comes in decimal format, as we are multiplying the radius with PI (that is 3.14).

So here we will use “float” instead of “int”. Everything else will be same. Please follow the below program.

a = float ( print ( ‘ Enter the radius ‘ ) )
b = 2 * 3.14 * a
c = 3.14 * r * r

print ( ‘ the perimeter is ‘ , b ) 
print ( ‘ the area is ‘ , c )




Now take another example:

a = 9
b = 2
c = a / b
print ( c )

Above program will print the result as 4.5 by default.

But if we use “int” method at the time of storing the result into c, it would print the answer as 4. Integer variable never shows the decimal part, it discards the point values.

a = 9
b = 2
c = int ( a / b )
print ( c )

or we can use // to divide . // is a special key which returns integer value.

a = 9
b = 2
c = a // b
print ( c )





1.4   Fourth Thing to learn:

Suppose , it is needed to do determine x5 or y4 in any program. The way you will follow now is to write x*x*x*x*x and y*y*y*y.

But what will happen in following 2 cases:
  • If I need to determine x34 , will you write x 34 times?
  • If I need to determine xy and x ,y are unknown and will be given by user in run time. In this case you would not be able to write the code even.


So there is a short-cut to this. Simply write,

z = x ** y

or directly you can write

z = 5 ** 3

remember,

1.      Single star means multiplication whereas double star means power.





So now assume there is program where user needs to enter 2 number as x & y, in return he/she will get

x = input ( ‘ Enter 1st number ‘ ) 
y = input ( ‘ Enter 2nd number ‘ )

z =( x ** 2   +   y ** 2 ) ** 0.5
print   ( ‘ the result is ‘, z )





1.5   FIFth Thing to learn:

  
To calculate the remainder after mathematical division in python, we use ‘%’. Suppose user will give 2 number as input you need to calculate result, remainder after division, we need to write the program in following way:


a = input ( ‘ Enter 1st number ‘ ) 
b = input ( ‘ Enter 2nd number ‘ )

c = a // b 
d = a % b

print  ( ‘ the result is ‘ , c ) 
print ( ‘ the remainder is ‘ , d )





So if user enters 2 numbers as 38 & 5. The output will be as follows:

the result is 7 
                        the remainder is 3


Remember,
                              Here result came as 7 because // always returns an integer , so it can not return 7.6







So we are done with the 5 things, now we can build all the small apps where we do not need the help of IF statement or FOR loop. We will learn IF & FOR in immediately next chapters.


So now we can start solving the exercise. So before starting the exercise, we need to maintain 1 small best practice like below:

Try to give a meaningful names to variables. Instead of giving a,b,c,d  , try to give names like num1 , num2, div, rem respectively. Later when you will be dealing with a hundred line program, it will be tough to handle and remember those variables with names a,b,c etc. Better to call them by their meaningful names.



That’s all for this chapter. Now we can jump into the exercise. You can find all the exercise in the facebook group album named “Basic Exercise”. Please leave your comments here in the comment box. Give your feedbacks with the good parts of this material or suggest where you are still facing difficulties.




Comments

Popular posts from this blog

Java concepts Theory