Python Programing Lab Exercise - 2(a)
Aim: 
        Write a program to compute distance between two points taking input from the user  (Pythagorean Theorem).

Description: 
               The Pythagorean theorem is the basis for computing distance between two points. Let (x1,y1) and (x2,y2) be the co-ordinates of points on xy-plane. From Pythagorean theorem, the distance between two points is calculated using the formulae: To find the distance, we need to use the method sqrt(). This method is not accessible directly, so we need to import math module and then we need to call this method using math static object.

To find the power of a number, we need to use ** operator.

Algorithm:

Input: x1,y1,x2 and y2

Output: Distance between two points. 

Step1: Start 

Step2: Import math module 

Step3: Read the values of x1,y1,x2 and y2 

Step4: Calculate the distance using the formulae math.sqrt( (x2 - x1)**2 + (y2 - y1)**2 ) and store the result in distance 

Step5: Print distance 

Step6: Stop.

Program:


import math
x1=int(input("Enter number:"))
x2=int(input("Enter number:"))
y1=int(input("Enter number:"))
y2=int(input("Enter number:"))
distance=math.sqrt((x2-x1)**2+(y2-y1)**2)
print("Distance between two points is : ",distance)

                OR
import math;
x1=int(input("Enter x1--->"))
y1=int(input("Enter y1--->"))
x2=int(input("Enter x2--->"))
y2=int(input("Enter y2--->"))
d1 = (x2 - x1) * (x2 - x1);
d2 = (y2 - y1) * (y2 - y1);
res = math.sqrt(d1+d2)
print ("Distance between two points:",res);



Output:


netaji gandi Sunday, June 30, 2019
Python Programing Lab(R16) EXERCISE - 1(a)
Aim: 

Running instructions in Interactive interpreter and a Python Script.
Procedure to Install and Run programs in Python: 


                                           In order to install python, Visit https://www.python.org. When we visit the Python for Windows download page, we‟ll immediately see the division. Right at the top, square and center,the repository asks if you want the latest release of Python 2 or Python 3 (2.7.13 and 3.8.0,

respectively) as shown in below Figure.


The version we want depends on our end goal. Here we will install Python 2.7.13. Click on Download Python 2.7.3 then python-2.7.3.msi file will be downloaded.


Run the installer, then a window will be opened as shown below. Select “Install for all users,” and then click “Next”.


After Clicking on “Next”, a window will be opened as shown below. On the directory selection screen, leave the directory as “Python27” and click “Next”.



After Clicking on “Next”, a window will be opened as shown below. On the customization screen, scroll down, click “Add python.exe to Path,” and then select “Will be installed on local hard drive.” then click “Next.”




We don‟t have to make any more decisions after this point. Just click through the wizard to complete the installation. When the installation is finished, set the variable path. After setting up the path, we can confirm the installation by opening up Command Prompt and type the following command as shown below.


                               Now, we can say that Python 2.7.13 is installed on our machine.

Different Ways of Invoking Python:
  • Python GUI
  • Python Commend Line
  • Command prompt from windows

After Clicking on IDLE(Python GUI), a window will be opened as shown below.

Python command line:

Click on start -> all programs -> python 2.7 -> Python (Command line)

After Clicking on Python (command line), a window will be opened as shown below:


Command prompt from windows:

To open Python from Windows command prompt, We need to set path. The procedure to set the path is as followes:
Go to My Computer -> right click and open properties, then a window will be opened as shown below:
Now, Click on Advanced system settings -> Environmental Variables -> system variables and under system variable, click on Path variable and click on Edit. Then, a window will be opened
as follows:

Add python path in variable value and click on OK as followes:

Now Open Command prompt from windows (cmd), and type the command “python” as follows:



netaji gandi Saturday, June 29, 2019
Python Programing Lab EXERCISE - 1(b)

Aim: 

Write a program to purposefully raise Indentation Error and Correct it.


Description:

Most of the programming languages like C, C++, Java use braces { } to define a block of code. Python uses indentation. A code block (body of a function, loop etc.) starts with indentation and ends with the first unindented line. The amount of indentation is depends on our choice, but it must be consistent throughout that block. Generally, Four whitespaces are used for indentation and is preferred over tabs. The enforcement of indentation in Python makes the code look neat and clean. This results into Python programs that look similar and consistent. Incorrect indentation will result into Indentation Error.

Program that shows Indentation Error: 

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




Output:


                    

Program without Indentation Error:
     
      a = 10
      b = 5
      c = a + b
      print c

Conclusion:

I get the knowledge on installing and executing programs in Python and also how Indentation plays a vital role in Python.

netaji gandi
Key Features of Python

What are the key features of Python?

  • Python is an interpreted language. That means that, unlike languages like Cand its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.
  • Python is dynamically typed, this means that you don’t need to state the types of variables when you declare them or anything like that. You can do things like x=111 and then x="I'm a string" without error
  • Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++’s publicprivate).
  • In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects
  • Writing Python code is quick but running it is often slower than compiled languages. Fortunately,Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it’s really quite quick because a lot of the number crunching it does isn’t actually done by Python
  • Python finds use in many spheres – web applications, automation, scientific modeling, big data applications and many more. It’s also often used as “glue” code to get other languages and components to play nice.

netaji gandi

Java Programming Lab

☕ Java Programming Lab Select a laboratory session to view programs and documentation Week 01 ...