import string
import secrets
Building a Custom Password Generator in Python: A Comprehensive Guide
Image Background Removal using Python
- rembg: A Python library that will handle the background removal for us.
pip install rembg
- PIL (Python Imaging Library): A Python library for image processing.
pip install pillow
- 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:
- Open a file
- Perform operation (Read/Write/Update)
- 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
- 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
- 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.
- The body statement of the while loop is separated from the rest of the code using indentation [tab].
For loop
- 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.
- 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
- 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
- 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.
- Dictionaries are written inside curly brackets ({}) with comma-separated key-value pairs.
Tuples in Python
- 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.
- Tuples can be created by placing all the elements separated by commas inside parentheses().
Sets in python
- 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
- 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.
- Strings can be created by enclosing the sequence of characters inside a single quote or double quote and assigning it to a variable.
- Sometimes triple quotes can also be used to create a string, even though triple quotes are used to represent Multiline strings and Docstrings.
- We can access the characters(element) in the string just like the index using index position.
Example python programs: Functions basics
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.
Lists in Python
- 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
- Set of instructions to perform a specific task.
- Below is the syntax of functions in python:
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

- 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

- 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...