Found 10476 Articles for Python

Python – Check if any list element is present in Tuple

AmitDiwan
Updated on 04-Sep-2021 10:44:01

915 Views

When it is required to check if any list element is present in a tuple or not, a Boolean value and a simple iteration are used.Below is a demonstration of the same −Example Live Demomy_tuple = (14, 35, 27, 99, 23, 89, 11) print("The tuple is :") print(my_tuple) my_list = [16, 27, 88, 99] print("The list is :") print(my_list) my_result = False for element in my_list:    if element in my_tuple :       my_result = True       break print("The result is :") if(my_result == True): print("The element is present in the ... Read More

Python Program to sort rows of a matrix by custom element count

AmitDiwan
Updated on 04-Sep-2021 10:42:37

211 Views

When it is required to sort rows of a matrix by custom element count, a method is defined that uses the list comprehension and ‘len’ method to find the output.Below is a demonstration of the same −Example Live Demodef get_count_matrix(my_key):    return len([element for element in my_key if element in custom_list]) my_list = [[31, 5, 22, 7], [85, 5], [9, 11, 22], [7, 48]] print("The list is :") print(my_list) custom_list = [31, 85, 7] my_list.sort(key=get_count_matrix) print("The resultant list is :") print(my_list)OutputThe list is : [[31, 5, 22, 7], [85, 5], [9, 11, 22], [7, 48]] The ... Read More

Python Program to Filter Rows with a specific Pair Sum

AmitDiwan
Updated on 04-Sep-2021 10:40:49

217 Views

When it is required to filter rows with a specific pair sum, a method is defined. It checks to see if elements in a specific index is equal to key, and returns output based on this.Below is a demonstration of the same −Example Live Demodef find_sum_pair(val, key):    for index in range(len(val)):       for ix in range(index + 1, len(val)):          if val[index] + val[ix] == key:             return True    return False my_list = [[71, 5, 21, 6], [34, 21, 2, 71], [21, 2, 34, 5], [6, 9, ... Read More

Program to check if number of compass usage to get out of a maze is enough in Python

Arnab Chakraborty
Updated on 30-Aug-2021 12:46:14

120 Views

Suppose, we are playing a game where we are trapped in a maze. We have to find our way out of the maze. The maze can be presented as an x m matrix, where n is the number of rows and m is the number of columns. Each cell/element of the matrix contains any of the symbols 'O', 'D', 'S', or '-'. 'O' means that the path is blocked, 'D' is the way out from the maze, 'S' is our starting position, and '-' means we can move through the path. We can move freely through any of the '-' ... Read More

Program to find out the minimum number of moves for a chess piece to reach every position in Python

Arnab Chakraborty
Updated on 30-Aug-2021 11:37:28

616 Views

Suppose, we have a chessboard and a special knight piece K, that moves in an L shape within the board. If the piece is in position (x1, y1) and if it moves to (x2, y2) the movement can be described as x2 = x1 ± a ; y2 = y1 ± b or x2 = x1 ± b ; y2 = y1 ± a ; where a and b are integer numbers. We have to find out the minimum number of moves for that chess piece to reach to reach position (n-1, n-1) on the chessboard from the position (0, ... Read More

Using djoser in Django for token authentication without views

Ath Tripathi
Updated on 26-Aug-2021 13:26:54

2K+ Views

Djoser is a simple authentication library for Django. It is used to generate tokens for authentication; this generated token is generated by taking three fields: username, email and password. It only works on POST request, but you can add its frontend.ExampleCreate a Django project and an app. I named them "DjoserExample" and "myapp".Install two packages −pip install djoser pip install djangorestframeworkIn settings.py, add the following lines −INSTALLED_APPS = [ #below every other apps    'myapp',    'rest_framework',    'rest_framework.authtoken',    'djoser' ] # Below template variable REST_FRAMEWORK = {    'DEFAULT_AUTHENTICATION_CLASSES': (       'rest_framework.authentication.TokenAuthentication',    ),   ... Read More

How to add Social Share buttons in Django?

Ath Tripathi
Updated on 26-Aug-2021 13:23:46

3K+ Views

We get to see Social Share buttons on most of the websites. They play an important role in ecommerce or any blogging or affiliate sites. As a web developer, you surely want people to like your website and want them to tell about your site to others on social media.In this article, we will see how to make an automated website share social button.ExampleFirst of all, create a project and an app.Install the django-social-share package −pip install django-social-shareIn settings.py, add django_social_share as an app in project.INSTALLED_APPS += ['django_social_share']In project's urls.py −from django.contrib import admin from django.urls import path, include ... Read More

Smooth profiling in Django

Ath Tripathi
Updated on 26-Aug-2021 13:22:21

543 Views

In this article, we are going to make a Django profiling. It will show a great deal of information like the total number of GET requests, database queries and many other reports for your website on a URL endpoint. It is great in production because you need to keep a check on many things when your site is in production.It is a great resource if you have to deploy a real-world project.ExampleFirst of all, create a project and an app. Do some basic settings and set up the urls.Now, install the djnago-silk package −pip install django-silkIn settings.py, add the following ... Read More

Django – Showing model data directly into table with sorting and pagination

Ath Tripathi
Updated on 26-Aug-2021 13:20:45

2K+ Views

In this article, we will see how to make a table in Django which will render model data. We are not going to use the tag of html. We will use a simple Django table library which provides features to directly show Django model data in a table with pagination feature.ExampleFirst of all, create a project and an app and set up the urls.Install the django_tables2 package −pip install django_tables2In settings.py −INSTALLED_APPS+=["django_tables2"]In models.py, create a simple model for testing −from django.db import models # Create your models here. class Data(models.Model):    Name=models.CharField(max_length=100)    salary = models.CharField(max_length=20)In urls.py, add ... Read More

QR code generating website in Django

Ath Tripathi
Updated on 26-Aug-2021 13:16:49

2K+ Views

We sometimes need to generate the QR code of an URL in our website. QR codes are scanned for verification, website login, opening websites and many things like that. In this article, we will see how to implement that. We are going to create a qrgenerator website in Django.ExampleCreate a Django project and an app. Create a media folder at the same level of project and app.Go to settings.py in the project folder and add the app name in INSTALLED_APPS and add this at the bottom −MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') MEDIA_URL = '/media/'Here we set up our media folder where ... Read More

Advertisements