This is a huge collection of Python Examples and Python Programs. These Python Tutorials provide detailed Python code examples with great explanation.
In these Python Examples, we cover most of the regularly used Python Modules; Python Basics; Python String Operations, Array Operations, Dictionaries; Python File, Input & Output Operations; Python JSON Processing; Python GUI.
Python Basic Examples
Python Basics – These Python Examples are for understanding the basic core concepts of Python Language.Python Conditional Statements
- Python Comments
- Python Datatypes
- Python Console Input and Output
- Python Type Casting
- Python Conditional Statements
- Python Looping Statements
- Python Indentation
- Python Enum Example
- Python Type Conversion
- Python Arithmetic Operators
- Python Logical Operators
- Python Identity Operators
- Python Comparison Operators
- Python Bit-wise Operators
- Python Walrus Operator
- Python Functions
- Python Classes and Objects
- Python Keywords
- Python Range Function – range()
- Python Lambda Function
- Python filter()
- Python map()
- Python Global Variable
- Python Regular Expressions
- Python Multi-threading Tutorial
Python Builtin Functions
Python Random Module Examples
Python Random Module – random module is used to pick an item randomly from a range. It provides many functions to simulate random behavior.
Python String Examples
Python String Operations are one of the most frequently used tasks while developing Python Applications. These Python Examples will demonstrate various String Operations in Python.
Python List Examples
Python List Operations – Python Lists are kind of arrays that can store items of different datatypes. These series of Python Examples explain CRUD Operations, and element wise operations on Python Lists.
Python Dictionary Examples
Python Dictionary is a datatype that stores non-sequential key:value pairs. This series of Python Examples will let you know how to operate with Python Dictionaries and some of the generally used scenarios.
- Python Dictionary – Introduction to Python Dictionary
- Python Dictionary Methods – Method of dict class
- Python Dictionary Operations – Howtos on Dictionary
Python Set Examples
Python Set is a non-ordered collection of items. Following list of Python Examples will present you about a Set in Python and different methods available for Set class.
- Python Set – Introduction to Python Set
- Python Set Methods – Methods of Set class
- Python Set Operations – Howtos on Set
Python Tuple Examples
Tuple is an immutable and ordered collection or items. Following list of Python Examples will provide more examples for different operations on Tuples.
- Python Tuple
- Python Tuple Operations
- Python Tuple Length
- Python Tuple – While Loop
- Python Tuple – For Loop
Python File Operations
Perform file operations like read, write, append, update, delete on files, folders etc. programmatically with Python.
- Python – Read Text File
- Python – Read Text File Line by Line
- Python – Write String to Text File
- Python – Delete or Remove File
- Python – Create a Directory
- Python – Check if Specified Path is File or Directory
- Python – Get the List of all Files in a Directory and its Sub-directories recursively.
- Python – Append Text to File
- Python – Replace a String in Text File
- Python – Replace multiple spaces with single space in a Text File
- Python – Count Number of Words in a Text File
- Python – Count Number of Characters in a Text File
Python JSON Examples
- Python – Create JSON
- Python – Parse JSON String
- Python – Convert Class Object to Python
- Python – Convert Tuple to JSON Array
- Python – Convert CSV to JSON
- Python – Convert JSON to Dictionary
- Python – Convert JSON to List
- Python – Convert List to JSON
- Python – Read JSON File
- Python – Write JSON to File
Python CSV Examples
Python Input Output Operations Examples
These examples cover programs that read input from user, or display output to user via console.
- Python Example to Print to Console Output
- Python input() Function
- Python Example to Read Number from Console Input
- Python Example to Read String from Console Input
- Python Print without new line at the end
Python Exception Handling
Exception Handling deals with the possible exceptions that could occur in your programs, and how to handle them using Python statements like try-except, etc.
- Python Try-Except – Handle exceptions thrown at runtime programmatically.
- Python TypeError: method() takes 0 positional arguments but 1 was given
- Python ModuleNotFoundError: No module named ‘openpyxl’
- Python Requests TimeoutError: [WinError 10060] A connection attempt failed
- Python NameError: name ‘string’ is not defined
- Python NameError: name ‘logging’ is not defined
Python Logging Examples
Python logging library helps you manage your logging strategy and how to write useful messages to the log files for maintaining your application.
Get started with Python Logging.
- Python Logging Basic Configuration
- Python Logging Messages to a Log File
- Python Logging – Set DEBUG Level
- Python Logging – Set INFO Level
- Python Logging – Set WARNING Level
- Python Logging – Set ERROR Level
- Python Logging – Set CRITICAL Level
- Python Logging – Set Logger Level
- Python Logging – Format Record
- Python Logging – Format Time
- Python Logging – Format Exception
- Python Logging – Format Stack
Python Date and Time
- Python DateTime.now()
- Python DateTime Format
- Python Compare DateTime
- Python – Convert String to Datetime
- Python – Convert Datetime to String
- Python Datetime – Get Month as Number
- Python Datetime – Get Month Name Full Version
- Python Datetime – Get Month Name Short Version
- Python Datetime – Get Day of Month
- Python Datetime – Get Weekday as Number
- Python Datetime – Get Weekday Full Version
- Python Datetime – Get Weekday Short Version
Python sqlite3 Examples
Python sqlite3 library provides a lightweight database that does not require a separate process running to access database. You may use sqlite3 database for prototype developments. And you can easily port this application to larger databases, as sqlite3 uses SQL.
Get started with Python sqlite3 Tutorial. More detailed topics are given below.
- Python sqlite3 – Create Connection Object
- Python sqlite3 – Create Table
- Python sqlite3 – Check if Table Exists
- Python sqlite3 – INSERT INTO table
- Python sqlite3 – SELECT FROM TABLE
- Python sqlite3 – INSERT Multiple Rows to TABLE
- Python sqlite3 – DELETE all rows FROM TABLE
Python Debugging Examples
Python GUI Examples
Python Tkinter library is used to develop GUI based Python applications.
- Python Tkinter – Set Specific or Fixed Window Size
- Python Tkinter – Window Background Color
- Python Tkinter Button Example
- Python Tkinter – Login Form Example
Python General Examples
- Python – Add Two Numbers
- Python – Generate a Random Number
- Python – Smallest of Three Numbers
- Python – Largest of Three Numbers
- Python – Sum of Two Numbers
- Python – Sum of First N Natural Numbers
- Python – Swap Two Numbers
- Python – Find if given year is Leap Year in Georgian Calendar
- Python – Matrix Transpose
- Python – Matrix Addition
- Python – Star Pattern Programs
- Python – Check if Number is Armstrong
Python Programs with Other Applications
- Python – Open URL in Chrome Browser from Python Application
- Python – Open URL in Firefox Browser from Python Application
Python Mathematical Libraries
Numpy Tutorial – Complete List of Numpy Examples.
Python Numpy Library is very useful when working with 2D arrays or multidimensional arrays. For example, if you are working with images, you have to store the pixel values in a two or three dimensional arrays.
Python supports only single dimensional arrays. It does not support multidimensional arrays. Numpy is an extension to Python Arrays, that not only supports multidimensional arrays, but also many mathematical operations on them.
Python Pandas Examples
Python pandas is used for data analysis.
- Python Pandas Examples
- Pandas Series Example
- Pandas – Create or Initialize DataFrame
- Pandas DataFrame – Change Column Labels
- Pandas – Construct DataFrame from Dictionary
- Pandas DataFrame – Load Data from CSV File
- Pandas DataFrame – Sort by Column
- Pandas DataFrame – Iterate Rows – iterrows()
- Pandas DataFrame – Add Row
- Pandas DataFrame – Add Column
- Pandas DataFrame – Delete Column
- Pandas Append or Concatenate DataFrames
- Pandas DataFrame – Reset Index
Python Matplotlib Examples
Python Image Processing
Pillow Tutorial – Python Pillow Library is used to perform Image Processing. It has functions to perform operations on images like: reading pixels of image; modifying pixels at specific locations; resizing; rotating; flipping; adjusting brightness, contrast and sharpness; etc.
Python OpenCV Examples
Python OpenCV library can be used to manipulate images and computer vision problems like object detection, object tracking, etc.
- Python Read Image to Array – OpenCV cv2.imread()
- Python OpenCV cv2.imwrite() – Save Image
- Python OpenCV Resize Image
- Python OpenCV cv2 Find Contours in Image
- Python OpenCV – Add Two Images
- Python Convert Image to Black and White (Binary)
- Python Extract Red Channel from Color Image
- Python Extract Green Channel from Color Image
- Python Extract Blue Channel from Color Image
- Python Remove Green Channel from Color Image
- Python Remove Blue Channel from Color Image
- Python Remove Red Channel from Color Image
Python Requests is a HTTP library for Python.
- Python Requests – GET
- Python Requests – POST
- Python Requests – PUT
- Python Requests – HEAD
- Python Requests – DELETE
- Python Requests – PATCH
- Python Requests – OPTIONS
- Python Requests – Send Parameters in URL
Python MongoDB – PyMongo
PyMongo helps Python applications to connect to MongoDB and perform operation on the database.
Python MongoDB – PyMongo Tutorial covers basic scenarios when you use MongoDB as your Database for your Python Applications.
Python PDF Examples
Python Natural Language Processing
NLTK Tutorial – Python NLTK Library can be used to apply NLP techniques like Classification, Tokenization, Stemming, Lemmatization, Parsing, etc. It has also an interface to connect to different third party corpora.