Posts

Showing posts from 2020

Python code to check which number is greater

Python code to check which number is greater? # Python program to find the largest number among the three input numbers # take three numbers from user num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) num3 = float(input("Enter third number: "))   if (num1 > num2) and (num1 > num3): largest = num1 elif (num2 > num1) and (num2 > num3): largest = num2 else: largest = num3   print("The largest number is",largest) Sample Output: Enter first number: 100 Enter second number: 120 Enter third number: 10 The largest number is 120

Insert pandas dataframe into Mongodb

Image
Insert pandas dataframe into Mongodb Pandas is most commonly used open-source Python Library for data manipulation, it provides high-performance data manipulation and analysis using its powerful data structures. Here we use pandas library to insert dataframe into Mongodb. I am taking Yahoo finance library to get dataset for a ticker and save that data into Mongo. First, we import all the necessary libraries from pandas_datareader import data as pdr import pandas as pd from pymongo import MongoClient import yfinance as yf yf.pdr_override() We get data from yahoo finance  # download dataframe data1 = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-01-15") Now, we make a connection to Mongodb. Here I am connecting to local MongoDb Server on port 27018 and then creating a database with the name `finance`. MongoDB has a collection(table) and I name it as `mycollection`. #Step 1: Connec...

Python text to speech conversion

Python text to speech conversion In order to convert a given text to speech, In python, we use `pyttsx3` module. pyttsx3 is a text-to-speech conversion library in Python.  It works offline, and is compatible with both Python 2 and 3. Use this command for installation: pip install pyttsx3 Usage: First we need to import the module ` pyttsx3 ` using import statement. Then we need to initialize it using ` init() ` function. After initializing, we need the program to speak using ` say() ` function. This takes two arguments as below: say(text, name string) text : Any text you wish to hear. name : To set a name for this speech. (optional) To run the speech we use runAndWait() . The say() function texts won’t be said unless the interpreter encounters runAndWait() . Example Code #1: # importing the pyttsx library  import pyttsx3     # initialisation  engine = pyttsx3.init()     # testing  engine....

Python desktop application

Image
Simple Python desktop application This is a continuation to my previous tutorial ( Python GUI using PyQt ). Anyway I will recap the tutorial on how to develop Python Desktop Application using PyQt5. PyQt is a GUI widgets toolkit. It is a Python interface for Qt, one of the most powerful, and popular cross-platform GUI library.  PyQt API is a set of modules containing a large number of classes and functions. While QtCore module contains non-GUI functionality for working with file and directory etc.,  Environments: PyQt is compatible with all the popular operating systems including Windows, Linux, and Mac OS.  Pip Install: The latest version of PyQt is PyQt5 5.14.1.  You can install pyqt using pip install PyQt5 In this tutorial, we are going to create a simple GUI app to add and delete records in a table using SQLite database. First, we need to create a database connection file with name `create_db.py`. A connection with ...

Python GUI using PyQt

Image
Create GUI application using Python PyQt5 PyQt is a GUI widgets toolkit. It is a Python interface for Qt, one of the most powerful, and popular cross-platform GUI library.  PyQt API is a set of modules containing a large number of classes and functions. While QtCore module contains non-GUI functionality for working with file and directory etc.,  QtGui module contains all the graphical controls. In addition, there are modules for working with XML (QtXml), SVG (QtSvg), and SQL (QtSql), etc. Environments: PyQt is compatible with all the popular operating systems including Windows, Linux, and Mac OS.  Pip Install: The latest version of PyQt is PyQt5 5.14.1.  You can install pyqt using pip install PyQt5 In this tutorial, we are going to create a simple GUI app to add and delete records in a table using SQLite database. First, we need to create a database connection file with name `create_db.py`. A connection with a SQLite datab...

Python Send Email Via Gmail SMTP

Image
Python Send Email Via Gmail SMTP we will learn how Python sends Email via Gmail SMTP Server. We will use following Python modules for this: 1) smtplib 2) MIMEText 3) MIMEMultipart In order to work with Gmail SMTP, you need to give permission to access less secure apps from your Google account. To give access, go through below instructions: 1) Go to your Google Account . 2) On the left navigation panel, select Security. 3) Scroll down to the section 'Less secure app access'.  4) Click on the button 'Turn on access (not recommended)' 5) Now turn on the button to give access to apps. Now open any of your IDE and paste the below code, save file as send_email.py in a directory. Code: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText mail_content = '''Hi, This is a testing email sent using Python and Gmail smtp.''' # The mail addresses and password sender_addr...

Python - convert speech to text

Python - convert speech to text We can convert a speech to text in a easy way using python. Please follow below steps to implement the same. Step 1: Install Python & pip in your machine. Step 2: Install below python libraries 1) pip install SpeechRecognition 2) pip install PyAudio Step 3: Open any IDE and write the below code. Code: import speech_recognition as sr r = sr.Recognizer() with sr.Microphone() as source: print("SAY SOMETHING") audio = r.listen(source) print("TIME OVER, THANKS") try: print("Text: "+r.recognize_google(audio)); except: pass; In the above code, it takes 'English' as default language. If you want to speak any other language and convert to speech, you need to modify the code little bit. I have added a language option at 'recognize_google' line. In my case, I have added the language 'Telugu' (te-IN). Language Code:       print("Text: "+r.recognize_...