Check Right Angle Triangle using sides.

As you aware that this is very common question asked in exams in school. Basic logic to get this is to check if any of the sides square value is equal to the addition value of squares of other two side. For example if a, b, c are three sides then if a**2 = b**2+c**2 then this triangle will be the right angle triangle. This logic is valid for any of the side from a, b, c. Lets have a look into the program.

Program to Check Right Angle Triangle

Output –

First Side – 6
Second Side – 5
Third Side – 4
NOT A RIGHT ANGLE TRIANGLE

First Side – 3
Second Side – 4
Third Side – 5
Given data is making a RIGHT ANGLE TRIANGLE

Posted in Uncategorized

Dictionary and Json in Python

Dictionary and Json in Python

Dictionary is one of the important feature of Python of key : value pair. Dictionary is mutable datatype of python. Python dictionary is similar to Json data which is used widely now a days.

Python comes with pre-loaded feature of JSon in-coding and de-coding. To use python json in-coding you need to import json in your file. Here is the example-

Dictionary and Json in Python

Once you run this program the output will look like this –

output – Dictionary and Json
Posted in Uncategorized

Saving Python object to File (serialization)

Saving Python object to File (Serialization)

Serialization and de-serialization is the process to store python object structure into file and viceversa. All because of using pickle module this process is known as pickling (serialization) and un-pickling(de-serialization). Pickling can get achieved by dump() method and un-pickling can be achieved by load() method of pickle module. But using this module in your program first you need to import pickle in your file. Here is the example of pickling(serialization).

Dump Student Object to file

This code will create a class called Student and at line no 29 Object of Student is created stu1. After that data is being loaded using readMarks() method at line no 31. One data is loaded, created file object f1 of ‘student.log’ file at line no 34. Later on used pickle.dump() method to dump the data into file and at last closed() the file object.

Posted in Uncategorized

Inserting data in Mysql using Python

Inserting data in Mysql using Python

As you are aware of using mysql-connector in last tutorial Connect Python with MySql. Now we will learn, how to insert data in mysql using python basic commands. The step is quite similar to select data from table. The difference is only you have to create a insert query with place holders like query = “insert into emp(empcode,empname,job,mgr,sal, comm, deptno) values (%s, %s, %s, %s, %s, %s, %s)” and then you need to create a tuple of values like values = (234, ‘Ramesh’, ‘SalesOp’, 124, 56730, 234, 22). Once it is done just pass these two as parameter to execute() function of cursor like mycursor. execute (query, values). Lets see one complete example for inserting record in mysql using Python.

Example : Insert data in mysql using Python

The output will look like this-

1, ‘Akash’, ‘teacher’, 1, Decimal(‘20000’), Decimal(‘1200’), 1)
(2, ‘Suraj’, ‘teacher’, 2, Decimal(‘23000’), Decimal(‘1300’), 2)
(3, ‘Aman’, ‘prof’, 3, Decimal(‘30000’), Decimal(‘2000’), 4)
(234, ‘Ramesh’, ‘SalesOp’, 124, Decimal(‘56730’), Decimal(‘234’), 22)

Here 234-Ramesh is the new data inserted in the table.

Posted in Uncategorized

Python with Mysql

Connect Python with MySql

Python is having very good support to connect any database. The most popular database used is mysql. Here is the example to connect Python mysql.

  1. You need to download mysql-connector from https://pypi.org from your command prompt.
  2. In command prompt type command pip install mysql-connector and wait till the package is getting install. After completion of installation you will get message “Successfully installed mysql-connector-2.1.6
  3. If pip is not working in command prompt then you need to set the path of python script and python installation folder into environment variable of your computer.
  4. Open idle prompt and check the import of mysql-connector by using import statement (import mysql.connector as mycon)
  5. If you are not getting any exception from Python Interpreter then your system is ready with mysql-connector to connect Python with mysql.

This is how you can download the connector from pip and test it. Now the next step to test the connection of python with mysql. Here is the program to test and get data from mysql in python console.

Program to test Python connection with MySql Database

Output of above program will be

(1, ‘Akash’, ‘teacher’, 1, Decimal(‘20000’), Decimal(‘1200’), 1)
(2, ‘Suraj’, ‘teacher’, 2, Decimal(‘23000’), Decimal(‘1300’), 2)
(3, ‘Aman’, ‘prof’, 3, Decimal(‘30000’), Decimal(‘2000’), 4)
(4, ‘Rakesh’, ‘businesman’, 4, Decimal(‘40000’), Decimal(‘3000’), 3)

Customizing to the output as your requirement – You can customize the output as per your requirement. Here I have iterated the output in two loops. First loop is to get row from cursor and second loop is to get individual elements from the row. So variable a will have row data and variable b will have every element of the row. Lets look into the for loop.

Data iteration from the cursor object.

Output of above program will be

1 Akash teacher 1 20000 1200 1
2 Suraj teacher 2 23000 1300 2
3 Aman prof 3 30000 2000 4

Posted in Uncategorized

GCD and LCM in Python

GCD and LCM in Python

The logic to find the GCD and LCM is quite simple. First write a function to get GCD and then write function to get LCM of the number. GCD can get calculated by the logic to find reminder after dividing one number by another, next time dividing the divisor by reminder left out. Continue the process till divisor is greater then zero. Now to get LCM we need to apply logic to divide the multiplication of numbers by the GCD value of numbers. Like if a and b are two numbers then the LCM will (aXb)/gcd(a,b). Lets look into the program.

GCD and LCM calculation in Python.

Output will be like this –

Enter first number – 4
Enter second number – 12
GCD of 4 and 12 is = 4
LCM of 4 and 12 is = 12.0

Posted in Uncategorized