Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, 8 February 2017

Friday, 27 January 2017

pip in windows-python

use:
python -m pip

in windows pip is in script folder, so it is treat as module so "-m" is used 

Monday, 23 January 2017

Extract files or unzip the tar files - python


import tarfile

# tar file to extract

theTarFile = 'D:\gcs task 1\suvi.gz'
# tar file path to extract

extractTarPath = 'D:\gcs task 1'
# open the tar filetfile = tarfile.open(theTarFile)
if tarfile.is_tarfile(theTarFile):
    # list all contents    print "tar file contents:"    print tfile.list(verbose=False)
    # extract all contents    tfile.extractall(extractTarPath)else:
    print theTarFile + " is not a tarfile."

read file based on range -python

import itertools

with open('C:\Users\Vishnuyardini\Documents\my goal.txt', 'r') as text_file:
    for line in itertools.islice(text_file, 5, 7):
        print line

to read enter file and based on line number - python

fileOpen=open('C:\Users\Vishnuyardini\Documents\my goal.txt','r')
print("file name...."+fileOpen.name)
#to read file based on line number
line= fileOpen.readline()
print "Read Line: %s" % (line)
#read line till eof
while True :
    line = fileOpen.readline()
    print line
    if ("" == line):
        print "file finished"        break

Friday, 20 January 2017

TUPLE-LIST BASIC

QUESTION

Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.

INPUT:

Suppose the following input is supplied to the program: 45,69,78,33,12,98

OUPUT:

Then, the output should be:
['45', '69', '78', '33', '12', '98']
('45', '69', '78', '33', '12', '98')

STEPS I TRY:

1)Try to read input from console using raw input
2)Read about TUPLE(immutable object), from this i came to know what is tuple
3)Read about LIST, from this i came to know what is list and is functions
4)Did coding part

SOULTION 1 :

ConsoleValue=raw_input("Enter the number seprated by comma.. which to be displayed as tuple and list...")
singleValues = (ConsoleValue.split(","))
listValue=list(singleValues)
tupleValue=tuple(singleValues)
print(listValue)
print(tupleValue)

OUTPUT FOR SOLUTION 1:



 DISADVANTAGES I FELT FOR SOLUTION 1:
1) memory usage for variables

SOLUTION 2:

ConsoleValue=raw_input("Enter the number seprated by comma.. which to be displayed as tuple and list...")
print(list((ConsoleValue.split(","))))
print(tuple((ConsoleValue.split(","))))

OUTPUT FOR SOLUTION 2:


ADVANTAGES I FELT FOR SOLUTION 2:

1)memory usage for variable is reduces

POSSIBLE INPUTS I TRIED:

1)INPUT IS NOT SEPARATES BY COMMA


2)INPUT ARE SERATED BY DOT(.)



3)INPUT JUST COMMA(,)



4)JUST ENTER AS INPUT:





Thursday, 19 January 2017

converting tsv to csv file-Python

import re
tsv = open('D:\csv to tsv\ggtsvFile.tsv', 'r')
csv = open('D:\csv to tsv\SampleCSVFile_2kb.csv', 'w')
for fileContent in tsv:
    fileContent = re.sub("\t", ",", fileContent)
    print(fileContent)
    csv.write(fileContent)
csv.close()



converting csv to tsv file-Python

import re
csv = open('D:\csv to tsv\SampleCSVFile_2kb.csv', 'r')
tsv = open('D:\csv to tsv\ggtsvFile.tsv', 'w')
for fileContent in csv:
    fileContent = re.sub(",", "\t", fileContent)
    print(fileContent)
    tsv.write(fileContent)
tsv.close()

Tuesday, 17 January 2017

list the files in directory based on extension using raw input-python

import os
def openfolder():
    path = raw_input("Enter path extension..")
    files = os.listdir(path)
    fe=raw_input("enter the extension u need...")
    for file in files:
        if file.endswith(fe):
            print(file)


openfolder()

to list the files and folders in the folders-PYTHON

import os
def openfolder():
    path = raw_input("Enter path..")
    files = os.listdir(path)
    for file in files:
        print(file)
openfolder()

Thursday, 5 January 2017

PYTHON -Loops & Conditions

IF STATEMENT:


a=10
if a>0:
  print("a is greater than zero, because a value is",a)


IF.. ELSE STATEMENT

a=0
if a<10:
  print("a is greater than zero, because a value is",a)
else:
  print ("a is greater than zero, because a value is",a)

IF..ELIF..ELSE

a=1
b=2
c=3
if a>b and a>c:
  print("a is greater than b and c")
elif b>c and b>a:
  print("b is greater than a and c")
elif c>a and c>b:
  print("c is greater than a and b")
else:
  print("All are equal")


WHILE WITH NESTED IF:

n = 1
while n <= 20:
    if n % 2 == 0:
        print (n," ")
    n = n + 1
print ("there, done.")



WHILE WITH NESTED WHILE:


n = 1
while n<=5:
  m=1
  while m<=n:
    print(m,"\t")
    m=m+1
  n=n+1
  print("\n")
print("done")
 


Wednesday, 4 January 2017

PYTHON -string & command lines

>>> word1="good"

ERROR:

>>> w2=good
Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    w2=good
NameError: name 'good' is not defined

PROPER WAY TO USE STRINGS

>>> w1="good"
>>> w2="morning"
>>> s1=w1+""+w2

>>> print(s1);
goodmorning

>>> s1=w1+"   "+w2
>>> print(s1)
good   moring

COMMAND LINE


>>> #print("fsdkncks")
>>> #print("this is command line this wont print in console")

PYTHON-math calculations >,<,<=,>= based on ()

>>> 1+2*3
7


>>> (1+2)*3
9

>>> 4-40-3
-39

>>> 4-(40-3)
-33

>>> print("which is great",5>2)
which is great True

>>> print("5 > 2 which is great",5>2)
5 > 2 which is great True

>>> print("5>=2 Is it greater or equal",5>=2)
5>=2 Is it greater or equal True

>>> print("5<=2 is it less or equal",5<=2)
5<=2 is it less or equal False

>>> print("2<1 which is less",2<1)
2<1 which is less False


>>> (4<2)
False

>>> (4>2)>(1>2)
True

HERE ONLY (1>2) GET EXECUTE AND RETURN THE RESULT

>>> (1>2)>(4<2)
False
>>> (1<2)>(4<2)
True
>>> (1>2)>(4>2)
False
>>> (1<2)>(4<2)
True

Tuesday, 3 January 2017

PYTHON-math calculations +,-,*,/,%

>>> 1+1
2
>>> 12345+54321
66666
>>> 4-2
2
>>> 2*5
10

this is 2 power 5=32
>>> 2**5
32

this is 5 power 2=25
>>> 5**2
25

>>> print("1+2 is 3 its is addition", 1+2)
1+2 is 3 its is addition 3

>>> print("1-2 is -1 its is sub-ration 1-2 =",1-2)
1-2 is -1 its is sub-ration 1-2 = -1

>>> 21/3
7.0

>>> 23/3
7.666666666666667

>>> 23.0/3
7.666666666666667

>>> 23.0/3.0
7.666666666666667

>>> 23%3
2

>>> print("23%3 this will return the reminder",23%3)
23%3 this will return the reminder 2


>>> v=5
>>> v+5
10

>>> v**5
3125


PYTHON-print & variables

TO PRINT

print("hello world")
hello world

ASSIGNING VALUE TO VARIABLES

  my_int=7
  my_float=7.7
  my_bool=True

PRINTING ASSIGNED VALUES

print(my_int)
print(my_float)
print(my_bool)

NOTE:
value assigned to the boolean variable start with CAPITIAL LETTER (True,False)

REASSIGNING THE VARIABLE VALUES

  my_int=1
  my_float=1.7
  my_bool=False

PRINTING REASSIGNED VALUES

print(my_int)
print(my_float)
print(my_bool)