Search This Blog

Python simple and easy to learn

Python simple and easy to learn


from math import pi

check_data=True
check_value = True
check_sample =True
letters="Hello Krishna"
numbers = [6,7,8]
if check_data:
  print ("Hello World" "  " "Moorthi")
  if check_sample:
    print ("Hello Moorthi")
 
if check_value == False:
 print ("I am in False")
elif check_value == True:
 print ("I am in \n True")
else:
 print ("Checking")


for char in letters:
  print (char)
total=0
for num in numbers:
  total = total + num

  print ("Total : ",total)
for test in numbers:
  pass


def krishnaMethod():
  """ Documentation """
  print ("I am inside method ",min(numbers))
 
krishnaMethod()


def greet(*names, msg = "Good morning!"):
   """
   This function greets to
   the person with the
   provided message.

   If message is not provided,
   it defaults to "Good
   morning!"
   """
   for name in names:
    print("Hello",name + ', ' + msg)

greet("Kate","krishna","Moorthi")
greet("Bruce",msg="How do you do?")
print("Pi Value : ",pi)

 
while True:
  try:
    validnumber=int(input("Please eneter number :"))
    break
  except:
    print("Invalid data")
   
 

Output:

Hello World  Moorthi
Hello Moorthi
I am in 
 True
H
e
l
l
o
 
K
r
i
s
h
n
a
Total :  6
Total :  13
Total :  21
I am inside method  6
Hello Kate, Good morning!
Hello krishna, Good morning!
Hello Moorthi, Good morning!
Hello Bruce, How do you do?
Pi Value :  3.141592653589793
Please eneter number : 3

Simple Python Program to Write and read xml file

Simple Python Program to Write and read xml file
import os
import xml.dom.minidom

dirs = os.listdir()
ListofFileNames = []
for filename in dirs:
  print ("List of files : ",filename)
  ListofFileNames.append(filename)

print("Files Names : ",ListofFileNames)
print("Current Dir : ",os.getcwd())

filecomposite= open("Composite.xml","w+")
filecontent ='<note><to>Tove</to><from id="12345">Jani</from><heading>Reminder</heading><body>Don forget me this weekend!</body></note>'
filecomposite.write(filecontent)
filecomposite.close()

f=open("Composite.xml", "r")
if f.mode == 'r':
  contents =f.read()
  print (" File contents  :  ",contents)

def readxmltags():
  doc = xml.dom.minidom.parse("Composite.xml")
  elementValues = doc.getElementsByTagName("from")

 
  for elementvalue in elementValues:
        print("I am inside method - From element value : ",elementvalue.firstChild.data)
        attributeValue = elementvalue.getAttribute("id")
        print("Attribute values : ",attributeValue)
readxmltags()




Output :

List of files :  Composite.xml
List of files :  .bash_logout
List of files :  .profile
List of files :  .bashrc
List of files :  .pylint.d
List of files :  _test_runner.py
Files Names :  ['Composite.xml', '.bash_logout', '.profile', '.bashrc', '.pylint.d', '_test_runner.py']
Current Dir :  /home/runner
 File contents  :   <note><to>Tove</to><from id="12345">Jani</from><heading>Reminder</heading><body>Don forget me this weekend!</body></note>
I am inside method - From element value :  Jani
Attribute values :  12345