Tuesday, August 1, 2023

Python 2 Functions

Functions

 def hello():  
   print("Hello functions")  
 hello()  
Functions with Parameters
 def add(i, j):  
   return i + j  
 print(add(12, 23))  
Exception Handling
 try:  
   i = int(input("Enter value 1 : "))  
   j = int(input("Enter value 2 : "))  
   res = i / j  
   print(res)  
 except ValueError:  
   print("Invalid Input")  
 except ZeroDivisionError:  
   print("Cannot divide by zero")  
 def divide():  
   try:  
     i = int(input("Enter value 1 : "))  
     j = int(input("Enter value 2 : "))   
     res = i / j  
     print(res)  
   except ValueError:  
     print("Invalid Input")  
   except ZeroDivisionError:  
     print("Cannot divide by zero")  

divide()

Python

print

 print("Hello Python")  
Memory Variable
 name = "Shalvin P D"  
 print("Hello ", name)  
Multiple assignments
 name, passion = "Shalvin", "IT"  
 print(f"{name} - {passion}")  
input
 name = input("Enter your name : ")  
 print("Hello ", name)  
int()
 i = int(input("Enter value 1 : "))  
 j = int(input("Enter value 2 : "))  
 res = i + j  
 print(res)  
float()
 i = float(input("Enter value 1 : "))  
 j = float(input("Enter value 2 : "))  
 res = i + j  
 print(res)  
Lists
 technologies = [".Net", "Python", "Angular"]  
 for tech in technologies:  
   print(tech)  
Lists append()
 technologies = [".Net", "Python", "Angular"]  
 technologies.append("C#")  
 for tech in technologies:  
   print(tech)  
Lists remove()
 technologies = [".Net", "Python", "Angular", "C#", "PHP"]  
 technologies.remove("PHP")  
 for tech in technologies:  
   print(tech)  
Lists Concatenation

technologies1 = [".Net", "Python", "Angular", "C#"]

technologies2 = ["DevOps", "Jenkins"]

technologies = technologies1 + technologies2

for tech in technologies:
    print(tech)
Dictionary
 contact = {"name" : "Shalvin", "location": "Kochi"}  
 print(contact)  
 print(contact["name"])  
 print(f'{contact["name"]} : {contact["location"]}')  
List of Dictionaries
 contacts = [{"name" : "Shalvin", "location": "Kochi"},  
       {"name": "Praseed", "location": "UK"}]  
 print(contacts)  
 first_contact = contacts[0]  
 print(first_contact)  
 print(f"{first_contact['name']} - {first_contact['location']}")  
 
print("Listing all contacts") for contact in contacts: print(f"{contact['name']} - {contact['location']}")