Assign Multiple Variables with Python List Values

Pradeep Elance
Updated on 13-May-2020 14:08:51

2K+ Views

Depending on the need of the program we may an requirement of assigning the values in a list to many variables at once. So that they can be further used for calculations in the rest of the part of the program. In this article we will explore various approaches to achieve this.Using for inThe for loop can help us iterate through the elements of the given list while assigning them to the variables declared in a given sequence.We have to mention the index position of values which will get assigned to the variables.Example Live DemolistA = ['Mon', ' 2pm', 1.5, '11 ... Read More

Append Multiple Lists at Once in Python

Pradeep Elance
Updated on 13-May-2020 14:06:09

8K+ Views

For various data analysis work in python we may be needed to combine many python lists into one list. This will help processing it as a single input list for the other parts of the program that need it. It provides performance gains by reducing number of loops required for processing the data further.Using + operatorThe + operator does a straight forward job of joining the lists together. We just apply the operator between the name of the lists and the final result is stored in the bigger list. The sequence of the elements in the lists are preserved.Example Live DemolistA ... Read More

Access Nth Element from Python Tuples in List

Pradeep Elance
Updated on 13-May-2020 14:03:45

958 Views

A python list can contain tuples as its elements. In this article we will explore how to access every nth element form the tuples that are present as the elements in the given tuple.Using indexWe can design a for loop to access the elements from the list with the in clause applied for nth index. Then we store the result into a new list.Example Live DemoAlist = [('Mon', '3 pm', 10), ('Tue', '12pm', 8), ('Wed', '9 am', 8), ('Thu', '6 am', 5)] #Given list print("Given list: ", Alist) # Use index res = [x[1] for x in Alist] ... Read More

Get Positive Elements from Given List of Lists in Python

Pradeep Elance
Updated on 13-May-2020 14:00:46

504 Views

Lists can be nested, means the elements of a list are themselves lists. In this article we will see how to find out only the positive numbers from a list of lists. In the result a new list will contain nested lists containing positive numbers.With for inHere we simply apply the mathematical operator to check for the value of elements in a list using a for loop. If the value is positive we capture it as a list and Outer for loop stores as a final list of lists.Example Live DemolistA = [[-9, -1, 3], [11, -8, -4, 434, 0]] ... Read More

Styling HTML Pages in Node.js

Shyam Hande
Updated on 13-May-2020 13:37:19

7K+ Views

In html files we can simply add style in head section −                    //add css code           We can add inline css styles as well in html directly.Generally css is separated from the html code. Third option add css is to include a css file .How to serve static files in Node.js?Generally css files are added with below tag −     Express js provides a middleware to seve static file. This middleware gives read access to given folder.app.use(express.static(path.join(__dirname, ‘public’)));path: its our core module ... Read More

Serving HTML Pages from Node.js

Shyam Hande
Updated on 13-May-2020 13:34:35

2K+ Views

So far we sent html code directly from the send(0 function in response object. For sending larger code, we definitely require to have a separate file for html code.sendFile() function−Response object gives a sendFile() function to return a html file to client.How to provide path to html file in sendFile() ?We import the path core module of node.js.const path = require(‘path’);path has a join function . __dirname is a global variable which holds the actual path to project main folder.path.join(__dirname, ‘views’, ‘add-user.html’); This will refer to the actual file location of add-user html code.App.jsconst http = require('http'); const express = ... Read More

Filtering Paths and Creating HTML Page in Express.js

Shyam Hande
Updated on 13-May-2020 13:32:50

413 Views

We added express router to handle routes. One single router file handles multiple routes.Adding a path for router in App.js −const http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const route = require('./routes'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use('/test', route); app.use((req, res, next)=>{    res.status(404).send(' Page not found '); }); const server = http.createServer(app); server.listen(3000);In router middleware we used path −/p>app.use('/test', route);Router will handle all paths starting with /test e.g. /test/add-usernameWe have to change action in form in routes.js file −router.get('/add-username', (req, res, next)=>{    res.send('     Send '); });Routes.js file −const express ... Read More

Adding 404 Page in Express

Shyam Hande
Updated on 13-May-2020 13:29:27

997 Views

Now we have a App.js and route.js for handling routes. For any other http requests for which we have not added any request handling will results into an error page. Example for url ‘test’ −App.jsconst http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const route = require('./routes'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use(route); const server = http.createServer(app); server.listen(3000);Showing meaningful error message on incorrect url’s−We can add a all catch middleware for incorrect url at the end of all middleware’s in App.js −const http = require('http'); const express = require('express'); const ... Read More

Use Express Router in Node.js

Shyam Hande
Updated on 13-May-2020 13:27:17

2K+ Views

In earlier examples, we wrote all routing code in a single file App.js. But in real world scenarios, we have to split the code into multiple files.We can create separate files and import them but express gives a router mechanism which is easy to use.Create a separate file called route.js (name can be anything)Create router using express −const express = require('express'); const router = express.Router();exporting router −module.exports = router;Adding routing functions −router.get('/add-username', (req, res, next)=>{    res.send(' Send '); });  router.post('/post-username', (req, res, next)=>{    console.log('data: ', req.body.username);    res.redirect('/'); });Similar to functions we used in App.js ... Read More

Using POST Request in Middleware in Express

Shyam Hande
Updated on 13-May-2020 13:25:04

1K+ Views

We used use() function to execute middleware’s . Example we used below will be executed for both http GET and POST method −const http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use('/', (req, res, next)=>{    next(); }); app.use('/add-username', (req, res, next)=>{    res.send(' Send '); }); app.use('/post-username', (req, res, next)=>{    console.log('data: ', req.body.username);    res.redirect('/'); }); app.use('/', (req, res, next)=>{    res.send(' first midleware: Hello Tutorials Point '); }); const server = http.createServer(app); server.listen(3000);How to restrict middleware only for GET or only for POST http method ... Read More

Advertisements