Posts

Showing posts from March, 2021

Test script -Open google and select a suggestion

from selenium import webdriver from selenium.webdriver.common.by import By import time driver = webdriver.Chrome(executable_path="C:/Users/Prashant/PycharmProjects/Automation.py/Drivers/chromedriver.exe") driver.implicitly_wait(5) driver.get("https://www.google.com/") print((driver.title)) driver.find_element(By.NAME,"q").send_keys("Naveen Automation Labs") options_list=driver.find_elements(By.CSS_SELECTOR,'ul.erkvQe li span') print(len(options_list)) for ele in options_list:     if ele.text=='naveen automation labs telegram':         ele.click()         break time.sleep(5) driver.close()

Array Program- to get number of occurrences of specified element

 from array import * count=0 Original_array=array('i', [1, 3, 5, 3, 7, 9, 3, 3, 8, 9, 11]) Num=int(input("Enter the number you want to search: " )) if Num in Original_array:     for i in Original_array:         if i==Num:             count+=1         else:             continue     print(count) else:     print("Number does not exist")

Array Programs-Reverse an array

# Reverse an array from array import * Original_array=array('i', [1, 3, 5, 3, 7, 1, 9, 3]) Original_array.reverse() print(" The new array is: " +str(Original_array))

Calculator program using python

 while True:     operation = input('''What would you like to do?                             1. Add                             2. Subtract                             3. Multiply                             4. Divide                                    ''')     if operation in ('1','2','3','4'):         try:             x=int(input("Enter number1: "))         except:             print("Value Error")             break         try:       ...

Program on lists to print a particular pattern

 list1 = ["Hello ", "take "] list2 = ["Dear", "Sir"] list=[] for item1 in list1: for item2 in list2: item=item1+item2 list.append(item) print(list)

Find factorial of a number!

 f = float(input(f'Insert number to find its factorial: ')) i=0 Factorial=1 while f>0 and i<f:     Factorial*=(f-i)     i+=1 print(f'Factorial value is {Factorial}')

Maximum of two numbers in Python

 num1 = float(input(f'Insert number 1: ')) num2 = float(input(f'Insert number2: ')) if num1>num2:     print(f'Greater number is {num1}') else:     print(f'Greater number is {num2}')

Python program to add two numbers

 Number1=int(input(f'Insert number 1: ')) Number2=int(input(f'Insert number2: ')) Sum=Number1+Number2 print(f'Sum is {Sum}') # If you do not add int before input, it will print 35 instead of 8 because it will take it as a string

A program in exceptions

  try:     Age = int(input("Age: "))     print(Age) except ValueError:     print("Invalid Value")

Making an emoji converter function!

 message = input(">") def emojis_function(message):     words=message.split(' ')     emojis={         ":)":":))",   #whenever :) will be written, it will be converted to :))              ":(":":(("     }     output=""     for word in words:         output+=emojis.get(word,word)+" "     return output   # return used to get value from the function print(emojis_function(message))

Emoji Converter example using dictionalries!

 message=input(">") words=message.split(' ') emojis={ ":)":":))", ":(":":((" } output="" for word in words:     output+=emojis.get(word,word)+" " print(output)

Print any number and convert it into digits!

 customers ={     "1":"One",     "2":"Two",     "3": "Three",     "4": "Four",     "5": "Five",     "6": "Six",     "7": "Seven",     "8": "Eight",     "9": "Nine" } Phone= input("Phone No.: ") output="" for ch in Phone:     output += customers.get(ch) + " " print(output)

Delete duplicates from list!- Method 2

 numbers=[7,11,115,340,11,400,110,115] uniques=[] for item in numbers:     if item not in uniques:         uniques.append(item) print(uniques)

Program to delete duplicate items in list!-Method 1

 numbers=[7,11,115,340,11,400,110,115] uniques=[] for item in numbers:     if numbers.count(item)>1:         numbers.remove(item)     else:         continue print(numbers)

Program to print minimum number in a list!

numbers=[7,11,115,340,400,110] min=numbers[0] for number in numbers:     if min>number:         min=number     else:         continue print(min)

Program to print max number in a list!

 numbers=[7,11,115,340,400,110] max=numbers[0] for number in numbers:     if max<number:         max=number     else:         continue print(max)

Print F pattern using nested loop!

 numbers=[5,2,5,2,2] for x in numbers:     for y in range(x+1):         num= 'x'*y     print(num)

Reversing a string using for loop!

 string='gops' rev_string="" n=len(string) for r in string:     rev_string+=string[n-1]     n-=1 print(rev_string)