Tuesday, November 26, 2019

single value Tuple in python

If we indicate the parenthesis with single value  then that should not be considered as tuple .

example 1:

t=(10)
print(type(t))
print(t)

output:
<class 'int'>
10

example 2:
t=('A')
print(type(t))

print(t)

output:
<class 'str'>
A

single value tuple indicates as (first value,)

example 3:
t=(10)
print(type(t))
print(t)
t=('A')
print(type(t))
print(t)
t=(10,)
print(type(t))

print(t)

output:
<class 'int'>
10
<class 'str'>
A
<class 'tuple'>
(10,)


Monday, November 25, 2019

write a program to search the vowel in the given string in python

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 26 07:16:18 2019

@author: anil durgam
program:
    write a program to search the vowel in the given string
   
"""
vowels=['a','e','i','o','u']
word=input("enter the some word to search for vowels\n")
found=[]
for letter in word:
    if letter in vowels:
        if letter not in found:
            found.append(letter)
print(found)
print("the number of different vowels present in",word,"is:",len(found) )


output:

enter the some word to search for vowels
anildurgam
['a', 'i', 'u']
the number of different vowels present in anildurgam is: 3


slice operator in python

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 26 06:53:23 2019

@author: anil
Topic:slice operator

"""
a='0123456789'
#a[begin index:end index:direction]
#begin index start from zero
#end index goes upto length of the string.in my case it is ten
#the last parameter indicates the direction if that is '+' its mean positive direction
#if that is '-' then negative direction and also step to increse
print(a[0:11:1])
#in end index forward direction then it should be '-1' need to delete
print(a[0:11:2])
print(a[0:11:3])
print(a[0:11:4])
print(a[0:11:5])
print(a[0:11:6])
print(a[0:11:7])
print(a[0:11:8])
print(a[0:11:9])

#begin index from different values
print()
print("slice operator begin value from different indexes")
print(a[1:11:2])
print(a[1:11:3])
print(a[1:11:4])

output:
0123456789
02468
0369
048
05
06
07
08
09

slice operator begin value from different indexes
13579
147
159


Happy face symbol using arcade game library in python

# -*- coding: utf-8 -*-
"""
Drawing an example happy face

If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.happy_face
"""

import arcade

# Set constants for the screen size
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Happy Face Example"

# Open the window. Set the window title and dimensions
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

# Set the background color
arcade.set_background_color(arcade.color.WHITE)

# Clear screen and start render process
arcade.start_render()

# --- Drawing Commands Will Go Here ---

# Draw the face
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

# Draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the left eye
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK,
                        start_angle, end_angle, 10)

# Finish drawing and display the result
arcade.finish_render()

# Keep the window open until the user hits the 'close' button
arcade.run()

Sunday, November 24, 2019

pygame introduction program

this code shows that how to display a window using pygame library


import pygame

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

pygame.init()

# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # --- Game logic should go here

    # --- Screen-clearing code goes here

    # Here, we clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.

    # If you want a background image, replace this clear with blit'ing the
    # background image.
    screen.fill(WHITE)

    # --- Drawing code should go here

    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit.
pygame.quit()

Thursday, November 21, 2019

tkinter label example

from tkinter import *
root = Tk()

one =Label(root,text="ONE",bg="red",fg="white")
one.pack()
two =Label(root,text="TWO",bg="green",fg="black")
two.pack(fill=X)
three =Label(root,text="THREE",bg="blue",fg="white")
three.pack(side=LEFT,fill=Y)


root.mainloop()

output:

tkinter example making of button


from tkinter import *

mm32=tk.Tk()

topframe = Frame(mm32)
topframe.pack()
bottomframe=Frame(mm32)
bottomframe.pack(side=BOTTOM)

button1=Button(topframe,text="MM32F",fg="red",font=('Helvetica', 15))
button2=Button(topframe,text="MM32L",fg="red",font=('Helvetica', 15))
button3=Button(topframe,text="MM32SPIN",fg="red",font=('Helvetica', 15))
button4=Button(topframe,text="MM32W",fg="red",font=('Helvetica', 15))
button5=Button(topframe,text="MM32P",fg="red",font=('Helvetica', 15))

button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=LEFT)
button5.pack(side=LEFT)

mm32.mainloop()

OUTPUT:


tkinter introduction


from tkinter import *
root = Tk()
root.mainloop()

output:




from tkinter import *

mm32=tk.Tk()

thelabel=tk.Label(text="MM32_DATASHEETS", font=('Helvetica',20), fg='green')
thelabel.pack()
mm32.configure(background="light green")

mm32.mainloop()

output:

Wednesday, November 20, 2019

drop down list using tkinter library in python

try:
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
def select():
    sf = "value is %s" % var.get()
    root.title(sf)
    # optional
    color = var.get()
    root['bg'] = color
root = tk.Tk()
# use width x height + x_offset + y_offset (no spaces!)
#root.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
#root.geometry("%dx%d+%d+%d" % (220, 40, 200, 150))
root.title("MM32_MICROCONTROLLER_DATA")
root.configure(background="light green")
var = tk.StringVar(root)
# initial value
var.set('MM32F_SERIES')
choices = ['MM32F_SERIES', 'MM32L_SERIES', 'MM32SPIN_SERIES', 'MM32W_SERIES','MM32P_SERIES']
Fchoices = ['MM32F003', 'MM32L_SERIES', 'MM32SPIN_SERIES', 'MM32W_SERIES','MM32P_SERIES']
option = tk.OptionMenu(root, var, *choices)
option.pack(side='left', padx=1, pady=1)
button = tk.Button(root, text="check value slected", command=select)
button.pack(side='left', padx=20, pady=1)
root.mainloop()

output





matrix list in python

a=[[10,20,30],[25,40,50],[60,70,80]]
print(a)
for r in a:
    print(r)

output:
[[10, 20, 30], [25, 40, 50], [60, 70, 80]]
[10, 20, 30]
[25, 40, 50]
[60, 70, 80]

nested list in python

a=[10,20,[30,40]]
print(a)
print(a[0])
print(a[1])
print(a[2][0])
print(a[2][1])

output:
[10, 20, [30, 40]]
10
20
30
40

clear method in python

clear method is used for to clear entire list ,list elements

a=[10,20,30]
print(a)
a.clear()
print(a)

output:
[10, 20, 30]
[]

list elements check using print

a=[10,20,30]
print(10 in a)
print(100 in a)
print(100 not in a)

output:
True
False
True

comparing the list elements in python


comparing list elements.

x=['abc','bcd','cde']
y=['abc','bcd','cde']
z=['ABC','BCD','CDE']
print(x==y)
print(x==z)
print(x!=z)

output:
True
False
True

example 2:
x=['abc','bcd','cde']
y=['abc','bcd','cde']
z=['ABC','BCD','CDE']
print(x[0] is y[0])
print(x==z)
print(x!=z)

output:
True
False
True

mathematical operation on the list in python

a1=[20,5,16,10,8]
a2=[10, 20, 30, 40]
c=a1+a2
print(c)

output:
[20, 5, 16, 10, 8, 10, 20, 30, 40]

copy method in python

a1=[20,5,16,10,8]
y=a1.copy()
print(a1)
print(y)

output:
[20, 5, 16, 10, 8]
[20, 5, 16, 10, 8]

slice operator using list

list we can copy  by using slice operator  ,the element of the first  list should same in the next list but when you change the element from the cloned list  that elements only changes previous list should same like before.

x=[10,20,30,40]
y=x[:]
print(x)
y[1]=111
print(id(x))
print(id(y))
print(y)
print(x)

output:
[10, 20, 30, 40]
2576163316104
2576163860680
[10, 111, 30, 40]
[10, 20, 30, 40]

list alias in python

In the list we can make many alias for the single list

x1 = [5, 8, 10, 16, 20]
y=x1
print(x1)
print(y)
print(id(x1))
print(id(y))

output:
[5, 8, 10, 16, 20]
[5, 8, 10, 16, 20]
2576163839240
2576163839240

sorting the element of the list in reverse using sort method

a1=[20,5,16,10,8]
a1.sort()
print(a1)
a1.sort(reverse=True)
print(a1)

output:

[5, 8, 10, 16, 20]
[20, 16, 10, 8, 5]

example:
if reverse is false then it will give normal sorting order only
a1=[20,5,16,10,8]
a1.sort()
print(a1)
a1.sort(reverse=True)
print(a1)
a1.sort(reverse=False)
print(a1)

output:
[5, 8, 10, 16, 20]
[20, 16, 10, 8, 5]
[5, 8, 10, 16, 20]

Tuesday, November 19, 2019

sort method in python

a1=[30,5,14,8,0]
a1.sort()
print(a1)

output:
[0, 5, 8, 14, 30]

example2:

a1=[30,5,14,8,0]
a1.sort()
print(a1)
a2=["bat","ant","dog","cat"]
a2.sort()
print(a2)

output:
[0, 5, 8, 14, 30]
['ant', 'bat', 'cat', 'dog']

example3:
if upper case strings and lowercase strings are present then it will shows the uppercase string first then it will shows the lower case strings

a1=[30,5,14,8,0]
a1.sort()
print(a1)
a2=["Bat","ant","Dog","Cat"]
a2.sort()
print(a2)

output:
[0, 5, 8, 14, 30]
['Bat', 'Cat', 'Dog', 'ant']

example 4:
strings and integers with in list then they will not sorted .compiler will shows the error

a1=[10,'B',20,'C',4,'A']
a1.sort()
print(a1)

output:
TypeError: '<' not supported between instances of 'str' and 'int'

reverse method in python



l1=[10, 30, 40, 50, 60, 70]
l1.reverse()
print(l1)

output:
[70, 60, 50, 40, 30, 10]

pop method in python

pop method removes the last element of the python and also return the which element is deleted

l1=[10, 30, 40, 50, 60, 70]
print(l1.pop())
print(l1)
print(l1.pop())
print(l1)

output:
70
[10, 30, 40, 50, 60]
60
[10, 30, 40, 50]

remove method in python

l1=[10,20,30,40]
print(l1)
l1.remove(20)
print(l1)

output:
[10, 20, 30, 40]
[10, 30, 40]

example 2:

l1=[10,20,30,40,50,60,70]
x=int(input("enter elements to be removed"))
if x in l1:
    l1.remove(x)
    print("removed successfully")
    print(l1)
else:
    print("specified element is not available")

output:
enter elements to be removed10
removed successfully
[20, 30, 40, 50, 60, 70]

enter elements to be removed20
removed successfully
[10, 30, 40, 50, 60, 70]

enter elements to be removed82
specified element is not available


append and extend methods in python


l1=[10,20,30]
l2=[40,50,60]
l1.extend(l2)
print(l1)
l1.extend('anil')
print(l1)
l1.append('anil')
print(l1)

output:
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 'a', 'n', 'i', 'l']
[10, 20, 30, 40, 50, 60, 'a', 'n', 'i', 'l', 'anil']

extend method in python

l1=["chicken","mutton","fish"]
l2=["biryani","fry","roast"]
l1.extend(l2)
print(l1)
print(l2)

output:
['chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']
['biryani', 'fry', 'roast']


example 2:
l1=["chicken","mutton","fish"]
l2=["biryani","fry","roast"]
l1.extend(l2)
print(l1)
print(l2)
l2.extend(l1)
print(l2)

output:

['chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']
['biryani', 'fry', 'roast']
['biryani', 'fry', 'roast', 'chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']

example3:
If we are adding a string using extend method then extire string added to given list but individual character.

l1=[10,20,30]
l2=[40,50,60]
l1.extend(l2)
print(l1)
l1.extend('anil')
print(l1)

output:
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 'a', 'n', 'i', 'l']

insert method in python

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]

example 2:

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)
l.insert(50,888)
print(l)
l.insert(-20,333)
print(l)

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]
[10, 50, 20, 30, 40, 888]
[333, 10, 50, 20, 30, 40, 888]

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)
l.insert(50,888)
print(l)
l.insert(-20,333)
print(l)
print(l.index(888))
print(l.index(333))

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]
[10, 50, 20, 30, 40, 888]
[333, 10, 50, 20, 30, 40, 888]
6
0



write a program in python to append all even number upto 100 which are divisible by 10

write a program in python to append all even number upto 100 which are divisible by 10

l=[]
for x in range(101) :
    if x%10==0:
        l.append(x)
    else:
        x=x+1
print(l)
 
output:
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

or
l=[]
for x in range(0,101,10) :
        l.append(x)
print(l)


output:
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

write a program in python to append all even number upto 100


l=[]
for x in range(101) :
    if x%2==0:
        l.append(x)
    else:
        x=x+1
print(l)
 
output:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]


write a program in python to append all odd numbers upto 100

l=[]
for x in range(101) :
    if x%2==1:
        l.append(x)
    else:
        x=x+1
print(l)

output:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

append method in python

single element adding by using append method
l=[]
l.append(10)
print(l)

output:
[10]


multiple methods adding by using append method
l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)

output:
[10, 20, 30, 40]

try and except method in python

l=[10,20,30,40,10,20,10,10]
target=int(input("enter the value to search"))
try:
    print(target,"available and its first occurence is at:",l.index(target))
except ValueError:
    print(target,"not available")

output
enter the value to search20
20 available and its first occurence is at: 1


enter the value to search30
30 available and its first occurence is at: 2


enter the value to search50
50 not available

count method in python

l=[10,20,30,40,10,20,10,10]
print(l.count(20))

output:
2


index method
it tells first occurrence of the given number

l=[10,20,30,40,10,20,10,10]
print(l.count(20))
print(l.index(10))

output:
2
0


if the value is  not there in the given string then it will show the value error

l=[10,20,30,40,10,20,10,10]

print(l.index(50))


output:

ValueError: 50 is not in list



l=[10,20,30,40,10,20,10,10]
target=int(input("enter the value to search"))
if target in l:
    print(target,"available and its first occurence is at:",l.index(target))
else:
    print(target,"not available")

output:
enter the value to search10
10 available and its first occurence is at: 0



enter the value to search30
30 available and its first occurence is at: 2


enter the value to search50
50 not available

Saturday, November 16, 2019

a function which is outside the method is called as method

a function  which is outside the method is called as method.
def f1():
    print("this is the function")
class student:
    def info(self):
        print("this is the method")
f1()
s=student()
s.info()

output:
this is the function
this is the method

Friday, November 15, 2019

list in python

l=[10,20,30,40]
print(l[0])
print(l[-4])


output:
10
10

example 2:
l=[10,20,30,40]
print(l[0])
print(l[-1])

output:
10
10

example3:
l=[10,20,[30,40]]
print(l[2])
print(l[-1])

output:
[30, 40]
[30, 40]

example4:
l=[10,20,[30,40]]
print(l[2][0])
print(l[2][1])

output:
30
40



list elements printing using while loop

list=[1,2,3,4,5,6,325,7,8]
i=0
while i<len(list):
    print(list[i])
    i=i+1

output:
1
2
3
4
5
6
325
7
8

list elements printing using for loop

list=[1,2,3,4,5,6,7,8,9]
for x in list:
    print(x)

output:
1
2
3
4
5
6
7
8
9

printing the even places of the list elements
list=[1,2,3,4,5,6,7,8,9]
for x in list:
    if x%2==0:
        print(x)

output:
2
4
6
8

Sunday, November 10, 2019

merging strings in python

s1=input("enter the first string::")
s2=input("enter the second string::")
output=''
i=j=0
while i<len(s1) or j<len(s2):
    output=output+s1[i]+s2[j]
    i=i+1
    j=j+1
print(output)

output:
enter the first string::anil

enter the second string::durg
adnuirlg


example 2:
s1=input("enter the first string::")
s2=input("enter the second string::")
output=''
i=j=0
while i<len(s1) or j<len(s2):
    if i<len(s1):
        output=output+s1[i]
        i=i+1
    if i<len(s2):
        output=output+s2[j]
        j=j+1
print(output)

output:
enter the first string::anil

enter the second string::durgam
adnuirlgam

printing unicode character in place of numbers in python

s= input("Enter some string:")
output=''
for x in s:
    if x.isalpha():
        output=output+x
        previous=x
    else:
        newch=chr(ord(previous)+int(x))
        output=output+newch
print(output)

output:
Enter some string:a4b2d9
aebddm

using sorted keyword and extracting alphabets and numbers using python

s= input("Enter some string:")
s1=s2=output=''
for x in s:
    if x.isalpha():
        s1=s1+x
    else:
        s2=s2+x
print(s1)
print(s2)


output:
Enter some string:a1b2c3
abc
123

example 2:
s= input("Enter some string:")
s1=s2=output=''
for x in s:
    if x.isalpha():
        s1=s1+x
    else:
        s2=s2+x
for x in sorted(s1):
    output=output+x
for x in sorted(s2):
    output=output+x
print(output)

output:
Enter some string:a1b2c3d4
abcd1234


example 3:
s= input("Enter some string:")
output=''
for x in s:
    if x.isalpha():
        output=output+x
        previous=x
    else:
        output=output+previous*(int(x)-1)
print(output)


output:
Enter some string:a4b2c5d2
aaaabbcccccdd

Wednesday, November 6, 2019

printing the character at even and odd positions

# -*- coding: utf-8 -*-
"""
Created on Thu Nov  7 07:46:59 2019

@author: Onyx1
"""

a=input("Enter some string::")
i=0
print("The Characters at even positions:")
while i<len(a):
    print(a[i],end=',')
    i=i+2
i=1
print("The Characters at odd positions:")
while i<len(a):
    print(a[i],end=',')
    i=i+2

output:
Enter some string::anildurgam
The Characters at even positions:
a,i,d,r,a,The Characters at odd positions:
n,l,u,g,m,




example2:
a=input("Enter some string::")
i=0
print("The Characters at even positions:")
while i<len(a):
    print(a[i],end=',')
    i=i+2
i=1
print()
print("The Characters at odd positions:")
while i<len(a):
    print(a[i],end=',')
    i=i+2

output:
Enter some string::anildurgam
The Characters at even positions:
a,i,d,r,a,
The Characters at odd positions:
n,l,u,g,m,

printing the character at even and odd positions in python

a=input("enter some string::")
print("Character of the even positions",a[::2])
print("Character of the even positions",a[1::2])


output:
enter some string::anil
Character of the even positions ai
Character of the even positions nl

reverse the given string using slice operator


a=input("Enter some string::")
print(a[::-1])

output:
Enter some string::anil durgam
magrud lina


method 2:
 using inbuilt functions


a=input("Enter some string::")
for x in reversed(a):
    print(x)

output:
Enter some string::anil durgam
m
a
g
r
u
d

l
i
n
a

join method

a=input("Enter some string::")
print(''.join(reversed(a)))

output:
Enter some string::anil
lina


without new line of the reversed string

a=input("Enter some string::")
for x in reversed(a):
    print(x,end='')

output:
Enter some string::anil
lina

Tuesday, November 5, 2019

format method in python

# -*- coding: utf-8 -*-
"""
Created on Wed Nov  6 07:28:23 2019
@author: Anil Durgam
"""

name="anil"
age=29
salary = 20000
print("{}'s age is {} and his salary is{}".format(name,age,salary))
print("{0}'s age is {1} and his salary is{2}".format(name,age,salary))
print("{x}'s age is {y} and his salary is{z}".format(x=name,y=age,z=salary))
print("{x}'s age is {y} and his salary is{z}".format(z=salary,x=name,y=age))

output:
anil's age is 29 and his salary is20000
anil's age is 29 and his salary is20000
anil's age is 29 and his salary is20000
anil's age is 29 and his salary is20000

isalnum,isalpha,islower,isspace methods in python


"""
Created on Sun Nov  3 09:54:15 2019

@author: anil durgam
"""

s=input("Enter any character:")
if s.isalnum():
    print("Alpha numeric character")
    if s.isalpha():
        print("Alphabet character")
        if s.islower():
            print("Lower case alphabet character")
        else:
            print("Upper case alphabet character")
    else:
        print("It is a digit")
elif s.isspace():
    print("It is space character")
else:
    print("Non space Special character")


output:

Enter any character:5
Alpha numeric character
It is a digit



Enter any character:a
Alpha numeric character
Alphabet character
Lower case alphabet character


Enter any character:
It is space character
    

Sunday, November 3, 2019

startwith,endswith methods in python

Example 1:

s="Learning Python is very easy"
print(s.startswith("Learning"))
print(s.endswith("Easy"))
print(s.endswith("easy"))

output:
True
False
True

upper,lower,title,swapcase,capitalize methods in python


example 1:

s="Anil Durgam Learning Python"
print(s.upper())
print(s.lower())
print(s.swapcase())
print(s.title())
print(s.capitalize())

output:
ANIL DURGAM LEARNING PYTHON
anil durgam learning python
aNIL dURGAM lEARNING pYTHON
Anil Durgam Learning Python
Anil durgam learning python

join method in python

join method is used for to join the strings . especially used for the list and tuples

example 1:
list joining using join method
s=["anil", "durgam", "embedded"]
l="-".join(s)
print(l)

output:
anil-durgam-embedded

example 2:
tuple joining with join method
s=("anil", "durgam", "embedded")
l="-".join(s)
print(l)

output:
anil-durgam-embedded

example 3:
s=("anil", "durgam", "embedded")
l=":".join(s)
print(l)

output:
anil:durgam:embedded

example 4:
s=("anil", "durgam", "embedded")
l=" ".join(s)
print(l)

output:
anil durgam embedded

example 5:
s=("anil", "durgam", "embedded")
l="".join(s)
print(l)

output:
anildurgamembedded

rsplit method in python

rsplit method is used for  to print the given string in right to left direction

example 1:
s="10 20 30 40 50 60 70 80"
l=s.rsplit(" ",3)
print(l)
for x in l:
    print(x)

output :
10 20 30 40 50
60
70
80

example 2:

s="10 20 30 40 50 60 70 80"
l=s.rsplit(" ",4)
print(l)
for x in l:
    print(x)

output:

10 20 30 40
50
60
70
80

default value for the rsplit is  -1
s="10 20 30 40 50 60 70 80"
l=s.rsplit(" ",-1)
print(l)
for x in l:
    print(x)

output:
10
20
30
40
50
60
70
80

Saturday, November 2, 2019

split method in python


example 1:
split method is used for to split the given string in the left to right direction

s="anil durgam embedded"
l=s.split()
print(l)

output:
['anil', 'durgam', 'embedded']

example2:

s="anil durgam embedded"
l=s.split()
print(l)
for x in l:
    print(x)

output:
['anil', 'durgam', 'embedded']
anil
durgam
embedded


example 4:

s="03-11-2019"
l=s.split("-")
print(l)
for x in l:
    print(x)

output:
['03', '11', '2019']
03
11
2019


example 5:

s="anil durgam embedded developer india"
l=s.split(" ",3)
print(l)
for x in l:
    print(x)

output:
anil
durgam
embedded
developer india

example 6:
s="10 20 30 40 50 60 70 80"
l=s.split(" ",3)
print(l)
for x in l:
    print(x)

output:
10
20
30
40 50 60 70 80


replace method in python

s="ababa"
print(s)
s1=s.replace('a','b')
print(s1)

output:
ababa
bbbbb

example 2:


s="ababa"
print(s)
print("address of ::",id(s))
s1=s.replace('a','b')
print(s1)
print("address of ::",id(s1))

output:

ababa
address of :: 1358579616712
bbbbb
address of :: 1358567155280


example 3:

string data type is immutable but create the new object



s="ababa"
print(s)
print("address of ::",id(s))
s=s.replace('a','b')
print(s)
print("address of ::",id(s))

output:
ababa
address of :: 1358579616712
bbbbb
address of :: 1358579722816

python class topic video