Python Articles

Page 811 of 852

How to read all HTTP headers in Python CGI script?

harsh manvar
harsh manvar
Updated on 27-Feb-2020 2K+ Views

It is possible to get a custom request header's value in an apache CGI script with python. The solution is similar to this.Apache's mod_cgi will set environment variables for each HTTP request header received, the variables set in this manner will all have an HTTP_ prefix, so for example x-client-version: 1.2.3 will be available as variable HTTP_X_CLIENT_VERSION.So, to read the above custom header just call os.environ["HTTP_X_CLIENT_VERSION"].The below script will print all HTTP_* headers and values −#!/usr/bin/env python import os print "Content-Type: text/html" print "Cache-Control: no-cache" print print "" for headername, headervalue in os.environ.iteritems():     if headername.startswith("HTTP_"):         print "{0} = {1}".format(headername, headervalue)   ...

Read More

How to use else conditional statement with for loop in python?

Pythonista
Pythonista
Updated on 27-Feb-2020 280 Views

The else block in a loop (for as well as while) executes after all iterations of loop are completed and before the program flow exits the loop body. The syntax is as follows −Syntaxwhile expr==True:     #statements to be iterated while expr is true. else:    #this statement(s) will be executed afteriterations are over#this will be executed after the program leaves loop bodyexamplefor x in range(6): print (x) else: print ("else block of loop") print ("loop is over")OutputThe output is as shown below −0 1 2 3 4 5 else block of loop loop is over

Read More

How to generate prime numbers using Python?

Pythonista
Pythonista
Updated on 26-Feb-2020 5K+ Views

A prime number is the one that is not divisible by any other number except 1 and itself.In Python % modulo operator is available to test if a number is divisible by other. Assuming we have to find prime numbers between 1 to 100, each number (let us say x) in the range needs to be successively checked for divisibility by 2 to x-1. This is achieved by employing two nested loops.for x in range(1,101): for y in range(2,x): if x%y==0:break else: print (x,sep=' ', end=' ')Above code generates prime numbers between 1-1001 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Read More

How to print a value for a given key for Python dictionary?

Jayashree
Jayashree
Updated on 26-Feb-2020 8K+ Views

Python dictionary is collection of key value pairs. Value associated with a certain key is returned by get() method.>>> D1={'a':11,'b':22,'c':33} >>> D1.get('b') 22You can also obtain value by using key inside square brackets.>>> D1['c'] 33

Read More

How to indent multiple if...else statements in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 3K+ Views

Use of indented blocks is an important feature of Python. Indent level of the block is more than previous statements. Hence, if multiple if statements are present in a program in nested fashion, each subsequent indented block will have increasing level of indent.if expr1==True:     if expr2==True:         stmt1     else:          if expr3==True:         stmt2 else:     if expr3==True:        stmt3     else:        stmt4

Read More

Can we use break statement in a Python if clause?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 253 Views

Python's break keyword is used as decision control statement. It causes the remaining iterations to be abandoned and control of execution goes to next statement after the end of loop. Invariably it is executed conditionally and appears inside if block within a loop.while expr==True:     stmt1         stmt2         if expr2==True:        break     stmt3     stmt4   However it can't be used in an if block if it is not a part of loop. 

Read More

Can we use continue statement in a Python if clause?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 253 Views

Python's continue statement is a loop control statement. It causes starting next iteration of loop after abandoning current iteration. Invariably is is executed conditionally i.e. in if blockwhile expr==True:     stmt1     stmt2     if expr2==True:        continue     stmt3     stmt4However, it can't be used in an if block if it is not a part of loop. If used, interpreter will throw syntax error.

Read More

What are the >> and << operators in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 284 Views

The symbols > are defined as left and right shift operators respectively in Python. They are bitwise operators. First operand is a bitwise representation of numeric object and second is the number of positions by which bit formation is desired to be shifted to left or right.The >> a=60 >>> bin(a) '0b111100' >>> b=a> b 240 >>> bin(b) '0b11110000'You can see two bits on right set to 0On the other hand >> operator shifts pattern to right. Most significant bits are set to 0>>> a=60 >>> bin(a) '0b111100' >>> b=a>>2 >>> b 15 >>> bin(a) '0b111100'

Read More

How does == operator works in Python 3?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 205 Views

The == symbol is defined as equality operator. It returns true if expressions on either side are equal and false if they are not equal>>> (10+2) == 12 True >>> 5*5 == 5**2 True >>> (10/3)==3 False >>> 'computer'=="computer" True >>> 'COMPUTER'.lower()=='computer' True

Read More

What does 'is not' operator do in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 579 Views

In Python, is and is not operators are called identity operators. Each object in computer's memory is assigned a unique identification number (id) by Python interpreter. Identity operators check if id() of two objects is same. 'is not' operator returns true of id() values are different and false if they are same.>>> a=10 >>> b=a >>> id(a), id(b) (490067904, 490067904) >>> a is not b False >>> a=10 >>> b=20 >>> id(a), id(b) (490067904, 490068064) >>> a is not b True

Read More
Showing 8101–8110 of 8,519 articles
« Prev 1 809 810 811 812 813 852 Next »
Advertisements