Blog

python tutorials and learn python

Created with Sketch.

Python COPY File using shutil.copy(), shutil.copystat()

In Python, you can use the shutil module to copy files using the shutil.copy() function. Additionally, if you want to preserve the file metadata (such as timestamps and permissions), you can use shutil.copystat(). Here’s an example: In this example: Replace ‘path/to/source_file.txt’ with the actual path of the file you want to copy. Replace ‘path/to/destination_file.txt’ with…
Read more

Python Check If File or Directory Exists

In Python, you can use the os module to check if a file or directory exists. Here’s a simple example: In Python, you can use the os module to check if a file or directory exists. Here’s a simple example: pythonCopy code import os def check_file_exists(file_path): return os.path.exists(file_path) # Example usage: file_path = ‘path/to/your/file.txt’ if…
Read more

Python File Handling: Create, Open, Append, Read, Write

File handling is an essential aspect of programming, and Python provides built-in functions to perform various operations on files. Here’s a brief tutorial on file handling, covering creating, opening, appending, reading, and writing to files. 1. Creating a File: 2. Writing to a File:   3. Appending to a File:   4. Reading from a…
Read more

Python CALENDAR Tutorial with Example

Certainly! The calendar module in Python provides useful functions and classes related to calendars. Here’s a simple tutorial with examples: 1. Displaying a Calendar for a Given Month: 2. Displaying a Calendar for a Given Year:   3. Determining if a Year is a Leap Year:   4. Finding the First Weekday of the Month:…
Read more

Python DateTime, TimeDelta, Strftime(Format) with Examples

    In Python, the datetime module provides classes for working with dates and times. Here’s a brief overview of datetime, timedelta, and strftime with examples: 1. datetime: The datetime class is used to represent dates and times. Here’s how you can create a datetime object: 2. timedelta: The timedelta class represents the duration between…
Read more

Python Regex: re.match(), re.search(), re.findall() with Example

Regular expressions (regex) are powerful tools for pattern matching in strings. In Python, the re module provides functions like re.match(), re.search(), and re.findall() to work with regular expressions. Here are examples for each: 1. re.match(): re.match() checks for a match only at the beginning of the string: 2. re.search(): re.search() searches for the pattern anywhere…
Read more

Python OOPs: Class, Object, Inheritance and Constructor with Example

Object-oriented programming (OOP) is a paradigm that uses objects to structure code. In Python, you can create classes and objects to implement OOP concepts. Here’s a basic example that covers class creation, object instantiation, inheritance, and the use of constructors: 1. Creating a Class and Instantiating Objects:   In this example, we have created a…
Read more

Python For & While Loops: Enumerate, Break, Continue Statement

In Python, you can use for and while loops to iterate over sequences or execute a block of code repeatedly. Here are examples of using for and while loops, along with the enumerate, break, and continue statements: for Loop with enumerate: The enumerate function is used to iterate over both the elements and their indices…
Read more

Python IF, ELSE, ELIF, Nested IF & Switch Case Statement

In Python, conditional statements like if, else, elif are used for decision-making, and there is no explicit switch statement. Here are examples of how to use these conditional statements: 1. if, else: 2. if, elif, else (Multiple conditions): 3. Nested if:   4. Switch-like behavior using dictionaries: Although there’s no explicit switch statement in Python,…
Read more

Python Programming Practice-Strings

 Python Programming Practice-Strings Strings are a data type in Python for dealing with text. Python has a number of powerful features for manipulating strings.  Basics Creating a string A string is created by enclosing text in quotes. You can use either single quotes, ‘ , or double quotes, ” . A triple-quote can be used…
Read more