Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Building a Custom Password Generator in Python: A Comprehensive Guide

In today's digital age, security is paramount. One of the simplest yet crucial aspects of digital security is having strong passwords. In this blog post, we'll dive into creating a Python script that generates secure passwords based on user preferences. The source code is at the end of this post.

Understanding the Code:
Let's break down the code step by step: 

1. Imports: We start by importing the necessary modules string and secrets. string provides a collection of string constants, and secrets is used for generating secure random numbers suitable for managing data such as passwords.
 
import string
import secrets

Image Background Removal using Python

In the world of image processing and manipulation, one common task is removing backgrounds from images. This can be particularly useful for creating visually appealing graphics or preparing images for various applications. One tool that helps achieve this is REMBG, a Python library designed for background removal from images. In this blog post, we'll guide you through a simple Python program that utilizes REMBG to remove backgrounds from images

Setting Up the Environment:

Before we dive into the code, let's ensure we have the necessary Python libraries installed:

  1. rembg: A Python library that will handle the background removal for us.

    pip install rembg
  2. PIL (Python Imaging Library): A Python library for image processing.

    pip install pillow
  3. easygui: A GUI library for file selection.

    pip install easygui

Python - File Handling

In real-world application development, the logic that we implement uses some data and those data may come from different sources like databases, files, or API. As a python developer, we should have good knowledge of handling these different sources. In this tutorial, we will see how we can handle files and perform some operations using python in-built functions.

Basically, a file is a location to store information in non-volatile memory (Hard Disk). And, it is characterized by its name and extension.

When we want to perform some operation(Read/Write/Update) in a file, we will open it first. Then we perform our task and finally, we will close it to save the changes and deallocate the resources tied with the file.

Similarly, in python, File operation takes place in the following order:

  1. Open a file
  2. Perform operation (Read/Write/Update)
  3. Close the file

Python - Exception Handling

Errors are unavoidable. As a developer, we always have to encounter multiple errors in the initial phase of development. And at the end of the development, the business logic/application which we developed should not misbehave or terminate due to some unexpected events during execution.

In General, Errors in python can be of two types

  • Syntax Errors/Compile Time Error - Syntax mistakes which result in termination of the program.
#syntax error example
a = 5
if(a)
print("Value present")

#result
File "main.py", line 2
    if(a)
        ^
SyntaxError: invalid syntax

Python Quiz

 This Quiz covers the basic concepts of Python

  • Basics of Python 
  • Control Structures
  • Functions
  • Collections
  • Libraries
  • Built-in functions
  • Modules and packages 
  • File and exception handling
  • Loop control statements - Break, Continue, Pass in Python

    Break, Continue, Pass in Python:
    • As we know, in Python we have two types of the loop 'for' and 'while' which allows us to iterate over a list, tuple, string, dictionary, and set. 
    • But there are some cases where we want our loop to exit completely or skip a part of the loop or ignores some particular condition. 

    While loop

    What is While loop in Python?
    • A While loop in Python is used to execute a set of statements/ iterate over a block of code repeatedly as long as the given condition is true.
    • While loop is preferred when we don't know the number of iterations.
    Syntax:
                        while condition:
                                body statements
    • The body statement of the while loop is separated from the rest of the code using indentation [tab].

    For loop

    What is For loop in Python?
    • A for loop in python has the ability to iterate over items of any sequence (List, tuple, string) or any other iterable objects.
    • Iterating over a sequence is called traversal.
    Syntax:
                        for var in sequence:
                                body statements
        
    • Here, var is a variable that is used to take the next values from the sequence on every iteration until the end of the sequence is reached.

    If, If...else, If...elif...else, Nested If

    What is a selection statement in Python?
    • In some cases in programming, Our program has to decide whether the following block of codes needs to be executed or not, based on some conditions.
    • Such Decision making statements/selection statements decide the direction of the flow of program execution.
    • Decision making statements/selection statements available in Python are
          • if (Simple If)
          • if-else
          • else-if ladder
          • Nested if
    • In python, body statements inside the selection statement are indicated by the indentation. The very first unindented line marks the end of the selection statements.

    Dictionaries in Python

    What is Dictionary in python?
    • Dictionary is an unordered collection of data in the form of key-value pair.
    • The first element in the key-value pair is the key and the second element is the value.
    • Like list, dictionaries are mutable.
    Creating a dictionary
    • Dictionaries are written inside curly brackets ({}) with comma-separated key-value pairs.

    Tuples in Python

    1) What is Tuple?
    • Just like List, Tuple is a collection of ordered elements. But the difference between list and Tuple is, tuples are immutable i.e. the elements in the Tuple cannot be changed, once it is assigned.
    • Tuples also can store both homogeneous and heterogeneous elements.
    2) Creating a Tuple
    • Tuples can be created by placing all the elements separated by commas inside parentheses().

    Sets in python

    What is Set?
    • Set is an unordered collection data type with no duplicate elements. (unique elements)
    • Using curly braces {} or keyword set, we can create a set in Python.
    • In general, the set function is used to eliminate duplicate elements in a list.

    Python Comments

    • Comments are the lines that are used to make your code more readable.
    • Comment lines are mainly used for documentation purposes to make the readers understand the source code.
    • These are the lines skipped during the program's execution by the compilers and the interpreters.
    • Sometimes, comments can also serve to prevent the execution of a line when testing code.
    • There are three types of comments in python.
      • Single line comments 
      • Multi-line comments
      • Docstring comments

    Strings in Python

    What is String in python?
    • In Python, String is a sequence of characters enclosed by either single quotes or double-quotes.
      • Example: 'Python', "Geek4Tutorial"
    • Strings are immutable.
    • Each value in a string is called character. 
    Creating a String:
    • Strings can be created by enclosing the sequence of characters inside a single quote or double quote and assigning it to a variable.
    Strings in Python
    • Sometimes triple quotes can also be used to create a string, even though triple quotes are used to represent Multiline strings and Docstrings.
    Strings in Python
    • We can access the characters(element) in the string just like the index using index position.

    Example python programs: Functions basics

    Simple python programs to practice:

    1) Write a Python function Factorial(n) which returns the factorial of the given number(n).

    2) Write a Python function, Square(num) which returns the square of the given number(num).

    3) Write a Python function, Sum(n) which returns the sum of first n numbers.

    4) Write a Python function, Palindrome(num) that accepts an integer num as input and returns true/false based on condition.

        Ex: num = 12321 ---> output = 12321 ----> then given number is palindrome.

    5) Write a Python function, check_strong_number(num) that accepts a positive as input, and returns true/false based on condition.
    • A number is said to be a Strong number if the sum of the factorial of each digit is equal to the given number.
       Ex: 145 is strong number as (1! =1) + (4! = 24) + (5! = 120) = 145.

    Lists in Python

    What is a List?
    • The list is one of the most common collection datatypes in Python which is ordered and mutable (changeable).
    • It is used to store a group of an element together in a sequence.
    • A list can store both homogenous and heterogeneous elements i.e., a single list may contain data types like integers, floats, strings as well as objects.
      • Individual value in a list is called an element.
      • These elements will be stored in a contiguous memory location.
        • For Ex:
          • Number1 = 10
          • Number2 = 20
          • Number3 = 30
          • Number4 = 40 
      • These individual variables can be represented in a list as
        • Number_list = [ 10, 20, 30, 40 ]

      Functions in Python basics

      Functions In python:
      • Set of instructions to perform a specific task.
      • Below is the syntax of functions in python:
                         def function_name([arg1,....,arg n]):
                                statement 1
                                ........
                                statement n
                                [return value]
                         variable = function_name([value1,......,value n])

      Control Structures

      • In general, Control structures decide the flow of the program execution.
      • The most commonly used control structures in python are
          • Sequential: control flows through all the statements, in the order of how the code is written.
          • Selection: Depends on the condition, control flows to the different statements.
          • Iteration: Certain blocks of code(statements) will be executed repeatedly.

        Basics of Python

        Code4tutorial basics of python
        • Python was created by Guido van Rossum in 1991 as a general-purpose programming language.
        • It is interpreted and interactive language. 
        • Python is an open-source language under a general public license (GPL). 
        • It supports both object-oriented and structured styles of programming.

        About: Introduction to Python Course

        Geek4Tutorial - Python Course
        Introduction to Python:
         
        This course is to introduce the basics of the Python programming language.
         
        About Python:
        • Python was created by Guido van Rossum in 1991 as a general-purpose programming language. It is interpreted and interactive language.
        • Python is an open-source language under a general public license (GPL). 
        • It supports both object-oriented and structured styles of programming.

        You might also like

        Deploy your Django web app to Azure Web App using App Service - F1 free plan

        In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...