Wednesday, October 30, 2019

Learning Python

 

Install Python 3.7

 

Installing packages for windows



C:\Users\username>pip install mysql-connector-python
Collecting mysql-connector-python
     |████████████████████████████████| 348kB 2.2MB/s
Collecting protobuf>=3.0.0 (from mysql-connector-python)
     |████████████████████████████████| 440kB ...
Requirement already satisfied: setuptools in c:\users\mhossai5\appdata\local\programs\python\python38\lib\site-packages (from protobuf>=3.0.0->mysql-connector-python) (41.2.0)
Collecting six>=1.9 (from protobuf>=3.0.0->mysql-connector-python)
Installing collected packages: six, protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.18 protobuf-3.10.0 six-1.12.0
WARNING: You are using pip version 19.2.3, however version 19.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.



UPGARDE PIP
python -m pip install --upgrade pip



import sys
print(sys.version_info)



PROGRAM CONNECTION TO MYSQL


# Author:     
#
# Created:     14/10/2019
# Copyright:   (c) MHossai5 2019
# Licence:     <your licence>
#-------------------------------------------------------------------------------

def main():
    pass

if __name__ == '__main__':
    main()
import mysql.connector
from mysql.connector import Error

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='test',
                                         user='root',
                                         password='')

    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("Your connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if (connection.is_connected()):
        cursor.close()
        connection.close()
        print("MySQL connection is closed")


PROGRAM WITH PANDAS


import pandas  as pd

from pandas import DataFrame
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [32000,35000,37000,45000]
        }
df = DataFrame(Cars, columns= ['Brand', 'Price'])
export_excel = df.to_excel (r'C:\Users\user\Desktop\export_dataframe4.xlsx', index = None, header=True) #Don't forget to add '.xlsx' at the end of the path
print (df)


import matplotlib.pyplot as plt


#df = pd.read_csv("export_dataframe4.csv")
dn = pd.read_excel(r"C:\Users\user\Desktop\export_dataframe4.xlsx")

dn.plot()
plt.show()




PROGRAM WITH METHODS



#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:     
#
# Created:     16/10/2019
# Copyright:    2019
# Licence:     <your licence>
#-------------------------------------------------------------------------------

def main():
    pass

if __name__ == '__main__':
    main()


    my_list = [1,2,3]
    squaresl = [num*num for num in my_list]
    print(squaresl)


    myl_list = [1,2,3]
    for num in myl_list:
      squarepl = (num*num)
      print(squarepl)



def my_function(x):
  return x[::-1]

mytxt = my_function("this text will look like backwards")

print(mytxt)


def fun_fib():
    a,b=0,1

    for i in range(6):
       a,b=b,a+b
       print (a,b)


fun_fib()

MySQL DB connection



https://www.geeksforgeeks.org/mysqldb-connection-python/


https://www.w3schools.com/python/python_mysql_getstarted.asp

https://pynative.com/install-mysql-connector-python/

https://pynative.com/python-mysql-select-query-to-fetch-data/

No comments:

Post a Comment

Rock Music Bangladesh

‘বাংলাদেশের করগাথা: আজম খানের উত্তরাধিকার‘ ‘History of Bangladesh Rock: The Legacy of Azam Khan’ বই # প্রথম_সংস্করণের_ভূমিকাঃ ৫ই জুন, ...