• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Code Snippets / Python Code Examples

Python Code Examples

Author: PFB Staff Writer
Last Updated: January 30, 2021

This page contains all Python scripts that we have posted our site so far.

Examples

Using pywhois

pywhois is a Python module for retrieving WHOIS information of domains. pywhois works with Python 2.4+ and no external dependencies [Source]

Magic 8-ball

In this script I’m using 8 possible answers, but please feel free to add more as you wish. There are 20 answers inside the Magic 8 Ball, and you can find them all here.

CommandLineFu with Python

A common first step when you want to use a Web-based services is to see if they have an API. Luckily for me, Commandlinefu.com provides one and can be found here

Additional Resources

Recommended Course: Intro to Python

Find all mp3 files with os.walk and fnmatch

In an earlier post “OS.walk in Python“, I described how to use os.walk and showed some examples on how to use it in scripts. In this article, I will show how to use the os.walk() module function to walk a directory tree, and the fnmatch module for matching file names.

Port scanner in Python

This post will show how you can make a small and easy-to-use port scanner program written in Python.

Backup all MySQL databases, with timestamp.

Backup all MySQL databases, one in each file with a timestamp on the end using ConfigParser.

Google Command Line Script

To make a request to the Web search API, we have to import the modules that we
need.

SSH Connection with Python

Pxssh is based on pexpect. It’s class extends pexpect.spawn to specialize setting up SSH connections.

Date and Time Script

This script can be used to parse date and time.

Bitly Shortener with Python

Bitly allows users to shorten, share, and track links (URLs). It’s a way to save, share and discover links on the web.

Sending Mails using Gmail

The smtplib module defines an SMTP client session object that can be used to send
mail to any Internet machine with an SMTP or ESMTP listener daemon. SMTP stands for Simple Mail Transfer Protocol.

Finding all files in the path that ends with a certain extension

This short script uses the os.listdir function (that belongs to the OS module) to search through a given path (“.”) for all files that endswith “.txt”.

Command Line speedtest.net via tespeed

Speedtest.net is a connection analysis website where users can test their internet
speed against hundreds of geographically dispersed servers around the world.

Search computer for specific files

In this article, I will show how to use the os.walk() module function to walk a
directory tree, and the fnmatch module for matching file names.

Parsing with Json

Knowing how to parse JSON objects is useful when you want to access an API
from various web services that gives the response in JSON.

Get the Geo Location of an IP Address

Time for a script again, this one will geolocate an IP address based on input from the user. For this script, we will be using a bunch of Python modules to accomplish this.

Get the username from a prompts

This script will ask the user for its username, by using the raw_input function. Then a list of allowed users is created named user1 and user2. The control statement checks if the input from the user matches the ones that are in the allowed users list.

Tweet Search using Python

This script will use the Twitter API to search for tweets.

Date and Time in Python

In this post, I will show you how you can do it without that by just using
datetime.datetime.now() .

The second part of the post is about the the timedelta class, in which we can see
the difference between two date, time, or datetime instances to microsecond
resolution.

Python Game : Rolling the dice

This is a classic “roll the dice” program.

Monitor Apache / Nginx Log File

This small script will count the number of hits in a Apache/Nginx log file.

Log Checker in Python

This script will show all entries in the file that is specified in the log file
variable.

Count how many days until a birthday

In this post, I will show you how you can print out the dates and times by just using datetime.datetime.now().

Python : Guessing Game part 2

This small program extends the previous guessing game I wrote about in this
: post “Guessing Game”.

Guessing Game written in Python

This script is an interactive guessing game, which will ask the user to guess a
number between 1 and 99.

Python Password Generator

You can use Pythons string and random modules to generate passwords.

OS Walk

OS.walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up.

Convert KM/H to MPH

This script converts speed from KM/H to MPH, which may be handy for calculating
speed limits when driving abroad, especially for UK and US drivers.

Get all the links from a website

In this script, we are going to use the re module to get all links from any website.

Celsius and Fahrenheit Converter

This script converts temperature between Fahrenheit to Celsius.

Additional Resources

Recommended Course: Intro to Python

How to use the Vimeo API in Python

In this post we will be looking on how to use the Vimeo API in Python.

Show the most popular videos on YouTube

This program will show how we can use the API to retrieve feeds from YouTube.

Calculate the average score

This script will calculate the average of three values.

Check your external IP address

This is a simple Python script to check which external IP address you have.

Python Hangman Game

This is a Python script of the classic game “Hangman”.

Python Command Line IMDB Scraper

This script will ask for a movie title and a year and then query IMDB for it.

Python code examples

Here we link to other sites that provides Python code examples.

ActiveState Code – Popular Python recipes

Snipplr.com

Nullege – Search engine for Python source code

Snipt.net

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Code Snippets, Python Code Snippets Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us