Monday, June 14, 2021

Check a number is palindrome in python

 palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar.

Ex: 121, ATA etc...

We can do this in many ways, here are some.

1. With the help of while loop

2. With the help of inbuilt functions

While loop:

number=int(input("Enter a num:"))
tem=number
rever=0
while(number>0):
    digit=number%10
    rever=rever*10+digit
    number=number//10
if(tem==rever):
    print("The number : "+str(number)+" is palindrome.")
else:
    print("The number : "+str(number)+" is not a palindrome")


In-built function:

strin=input(("Enter a str:"))
if(strin==strin[::-1]):
    print("The str is a palindrome")
else:
    print("Not a palindrome")

Wednesday, May 11, 2016

converting curl command to python

For completing this we need to install  curl_to_requests Once we install we are ready to go ... for Converting cURL commands into equivalent Python Requests code

below is the sample code of use:

import curl_to_requests
curl_cmd = """curl 'https://github.com/mosesschwartz/curl_to_requests' \-H 'Accept-Encoding: gzip, deflate, sdch' \-H 'Accept-Language: en-US,en;q=0.8' \-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36' \-H 'Accept: text/html, */*; q=0.01' \-H 'Referer: https://github.com/mosesschwartz/curl_to_requests' \-H 'Connection: keep-alive' --compressed"""
print curl_to_requests.curl_to_requests(curl_cmd)

 Here is the sample output:

import requests

headers = {'Accept-Language': ' en-US,en;q=0.8', 'Accept-Encoding': ' gzip, deflate, sdch', 'Accept': ' text/html, */*; q=0.01', 'User-Agent': ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36', 'Connection': ' keep-alive', 'Referer': ' https'}

r = requests.GET('https://github.com/mosesschwartz/curl_to_requests', headers=headers)

Tuesday, February 23, 2016

Working with Excel Files

Working with Excel Files

XLRD package is for reading data and formatting information from older Excel files (ie: .xls)

Installing the pkg :

pip install xlrd

Usage :

import xlrd

fil="test.xlsx"  # This is sample file with some data

workbook= xlrd.open_workbook(fil)

Sheet=workbook.sheet_by_name("Data_set1")      #Data_set1 this is the name that is displayed at bottom left cornet over the present working sheet

ccount=Sheet.ncols     #to get the column count

rcount=Sheet.nrows     #to get the row count

for r in range(rcount):
for c in range(ccount) :
val=[Sheet.cell_value(r,c)]
print (str(Sheet.cell_value(r,c)))

sample exel file


Working with regular expressions

Regular expressions in py

If we got the regular expression then using with that we can get the format string/ numbers 

In order to play with them we need to have a pkg called "exrex"
To install the pkg use below cmd :

pip install exrex

Exrex is a command line tool and python module that generates all - or random - matching strings to a given regular expression and more. It's pure python, without external dependencies.
There are regular expressions with infinite matching strings (eg.: [a-z]+), in these cases exrex limits the maximum length of the infinite parts.
Features
  • Generating all matching strings
  • Generating a random matching string
  • Counting the number of matching strings
  • Simplification of regular expressions
USAGE:
>>> import exrex
>>> exrex.getone('(ex)r\\1')
'exrex'
>>> list(exrex.generate('((hai){2}|world!)'))
['haihai', 'world!']
>>> exrex.getone('\d{4}-\d{4}-\d{4}-[0-9]{4}')
'3096-7886-2834-5671'
>>> exrex.getone('(1[0-2]|0[1-9])(:[0-5]\d){2} (A|P)M')
'09:31:40 AM'
>>> exrex.count('[01]{0,9}')
1023
>>> print '\n'.join(exrex.generate('This is (a (code|cake|test)|an (apple|elf|output))\.'))
This is a code.
This is a cake.
This is a test.
This is an apple.
This is an elf.
This is an output.
>>> print exrex.simplify('(ab|ac|ad)')
(a[bcd])


Thursday, November 26, 2015

Operators used in Python

Types of Operator

Python language supports the following types of operators.
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators

Python Arithmetic Operators:
Operator
Description
+ Addition
Adds values on either side of the operator.
- Subtraction
Subtracts right hand operand from left hand operand.
* Multiplication
Multiplies values on either side of the operator
/ Division
Divides left hand operand by right hand operand
% Modulus
Divides left hand operand by right hand operand and returns remainder
** Exponent
Performs exponential (power) calculation on operators
//
Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.

Python Comparison Operators:

These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

Operator
Description
==
If the values of two operands are equal, then the condition becomes true.
!=
If values of two operands are not equal, then condition becomes true.
<> 
If values of two operands are not equal, then condition becomes true.
> 
If the value of left operand is greater than the value of right operand, then condition becomes true.
< 
If the value of left operand is less than the value of right operand, then condition becomes true.
>=
If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.
<=
If the value of left operand is less than or equal to the value of right operand, then condition becomes true.

Python Assignment Operators: 

Operator
Description
=
Assigns values from right side operands to left side operand
+= Add AND
It adds right operand to the left operand and assign the result to left operand
-= Subtract AND
It subtracts right operand from the left operand and assign the result to left operand
*= Multiply AND
It multiplies right operand with the left operand and assign the result to left operand
/= Divide AND
It divides left operand with the right operand and assign the result to left operand
%= Modulus AND
It takes modulus using two operands and assign the result to left operand
**= Exponent AND
Performs exponential (power) calculation on operators and assign value to the left operand
//= Floor Division
It performs floor division on operators and assign value to the left operand

Python Bitwise Operators:
Bitwise operator works on bits and performs bit by bit operation.

Operator
Description
& Binary AND
Operator copies a bit to the result if it exists in both operands
| Binary OR
It copies a bit if it exists in either operand.
^ Binary XOR
It copies the bit if it is set in one operand but not both.
~ Binary Ones Complement
It is unary and has the effect of 'flipping' bits.
<< Binary Left Shift
The left operands value is moved left by the number of bits specified by the right operand.
>> Binary Right Shift
The left operands value is moved right by the number of bits specified by the right operand.

Python Logical Operators: 
There are following logical operators supported by Python language.

Operator
Description
and Logical AND
If both the operands are true then condition becomes true.
or Logical OR
If any of the two operands are non-zero then condition becomes true.
not Logical NOT
Used to reverse the logical state of its operand.

Python Membership Operators:
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples.

Operator
Description
in
Evaluates to true if it finds a variable in the specified sequence and false otherwise.
not in
Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

Python Identity Operators:
Identity operators compare the memory locations of two objects. There are two Identity operators explained below:

Operator
Description
is
Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
is not
Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

Python Operators Precedence:
The following table lists all operators from highest precedence to lowest.
Operator
Description

**
Exponentiation (raise to the power)

~ + -
Ccomplement, unary plus and minus (method names for the last two are +@ and -@)

* / % //
Multiply, divide, modulo and floor division

+ -
Addition and subtraction

>> <<
Right and left bitwise shift

&
Bitwise 'AND'
^ |
Bitwise exclusive `OR' and regular `OR'
<= < > >=
Comparison operators
<> == !=
Equality operators
= %= /= //= -= += *= **=
Assignment operators
is is not
Identity operators
in not in
Membership operators
not or and
Logical operators