Wednesday, 23 August 2017

Sentence capitalizer in python

Example:

def f1 (string1: str):
    sentences = string1.split(". ")
    sentences2 = [sentence[0].capitalize() + sentence[1:] for sentence in sentences]
    string2 = '. '.join(sentences2)
    return string2
print (f1("hello. my name is Joe. what is your name?"))

Output:

Hello. My name is Joe. What is your name?

Example:

import re
def sentence_case(text):    
     
    sentences = re.findall('[^.!?]+[.!?](?:\s|\Z)', text)

    sentences = [x[0].upper() + x[1:] for x in sentences]
   
    return ''.join(sentences)

print(sentence_case("hEllo. my name is Joe. what is your name?"))

Output:

HEllo. My name is Joe. What is your name?

Thursday, 17 August 2017

finding maximum and minimum numbers by using lambda function

Finding maximum number:

my_max=lambda a,b,c:a if a>b and a>c else(b if b>c else c)

print(my_max(4,7,6))

Output:

7

Finding minimum number:

my_min=lambda a,b,c:a if a<b and a<c else(b if b<c else c)

print(my_min(4,7,6))



Output:

4

Monday, 14 August 2017

Fibonacci series

write Fibonacci series up to n

def fib(n):
    a, b = 0, 1
    while b < n:
        print(b,)
        a, b = b, a+b
fib(100)

Output:


1
1
2
3
5
8
13
21
34
55
89

Friday, 11 August 2017

swapping of two numbers in python

swapping of two numbers by using temporary variable

a=int(input("enter a value: "))

b=int(input("enter b value: "))

print("before swapping: ","a=",a,"b=",b)

temp=a

a=b

b=temp

print("after swapping: ","a=",a,"b=",b)

Output:

enter a value: 4

enter b value: 5

before swapping:  a= 4 b= 5


after swapping:  a= 5 b= 4



swapping of two numbers 

a=int(input("enter a value: "))

b=int(input("enter b value: "))

print("before swapping: ","a=",a,"b=",b)

a,b=b,a

print("after swapping: ","a=",a,"b=",b)

Output:

enter a value: 4

enter b value: 5

before swapping:  a= 4 b= 5


after swapping:  a= 5 b= 4

Tuesday, 8 August 2017

how to check memory size of object in python

check memory size of objects:

import sys
sys.getsizeof(object)

note: 
memory represents in bytes

python 2.7.13

>>> import sys
>>> x=2
>>> type(x)
<type 'int'>
>>> sys.getsizeof(x)
12
>>> y=2.3
>>> sys.getsizeof(y)
16
>>> z=3+4j
>>> sys.getsizeof(z)
24
>>> a=True
>>> sys.getsizeof(a)
12
>>> b="siva"
>>> sys.getsizeof(b)
25
>>> c=[1,2,3]
>>> sys.getsizeof(c)
48
>>> c=[10,2.3,"siva"]
>>> sys.getsizeof(c)
48
>>> d=(1,2,3)
>>> sys.getsizeof(d)
40
>>> e={1,2,3}
>>> sys.getsizeof(e)
116
>>> f={'maths':75,'science':80}
>>> sys.getsizeof(f)
140
>>> 

python 3.6.2

>>> import sys
>>> x=2
>>> sys.getsizeof(x)
28
>>> y=2.3
>>> sys.getsizeof(y)
24
>>> z=3+4j
>>> sys.getsizeof(z)
32
>>> a=True
>>> sys.getsizeof(a)
28
>>> b="siva"
>>> sys.getsizeof(b)
53
>>> c=[1,2,3]
>>> sys.getsizeof(c)
88
>>> d=(1,2,3)
>>> sys.getsizeof(d)
72
>>> e={1,2,3}
>>> sys.getsizeof(e)
224
>>> f={'maths':75,'science':80}
>>> sys.getsizeof(f)
240

>>>