Posts

PYTHON PROGRAM THAT ASKS USER FOR LARGE INTEGERS AND INSERTS COMMAS INTO IT ACCORDING TO THE STANDARD AMERICAN CONVENTION FOR COMMAS IN LARGE NUMBERS

Image
 Write a program that asks the user for a large integer and inserts commas into it according to the  standard American convention for commas in large numbers. For instance, if the user enters 1000000, the output should be 1,000,000 CODE: n=int(input("Enter a number:")) print("The number seperated with commas is:{:,}".format(n)) EXPLANATION: The  format()  method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {} Inside the placeholders you can add a formatting type to format the result SYNTAX: string .format( value1, value2... ) PARAMETER VALUES: Parameter Description value1, value2... Required. One or more values that should be formatted and inserted in the string. The values can be A number specifying the position of the element you want to remove. The values are either a list of values separated by commas, a key=value list, or a combination of both. The values can be of any data

PYTHON PROGRAM THAT ASKS THE USER TO ENTER TWO STRINGS OF SAME LENGTH AND WILL ALTERNATE THE CHARACTERS OF THE TWO STRINGS.FOR EXAMPLE: abcde and ABCDE THEN THE PROGRAM WILL PRINT AaBbCcDdEe

Image
Write a program that asks the user to enter two strings of the same length. The program should  then   check  to see if the strings are of the same length. If they are not, the program should print an appropriate   message and exit. If they are of the same length, the program should alternate the characters of the two   strings. For example, if the user enters abcde and ABCDE the program should print out AaBbCcDdEe. CODE: def alternate_string(s1,s2):     new_string=""     if len(s1)==len(s2):         print("The two strings are of same length")         for i in range(len(s1)):             new_string=new_string+s2[i]+s1[i]     else:         print("The two strings are not of same length")     return new_string string1=input("Enter string-1:") string2=input("Enter string-2:") print("New string is:",alternate_string(string1,string2)) EXPLANATION: In this program we have defined a function named alternate_string which accepts two st

PYTHON PROGRAM THAT ASKS THE USER TO ENTER A WORD AND PRINTS OUT WHETHER THAT WORD CONTAINS ANY VOWELS

Image
Write a program that asks the user to enter a word and prints out whether that word contains any vowels CODE: def vowels_str(string):     s=""     vowels="aeiouAEIOU"     for i in range(len(string)):         if string[i] in vowels:             if string[i] not in s:                 s=s+string[i]     if s!="":         print("The string contains vowels")     else:         print("The string doesn't contain vowels")     return s str=input("Enter a string:") print("Vowels in a given string :",vowels_str(str)) EXPLANATION: In this code, we write a function to check whether the string contains any vowels. Here we create an empty string(s="") and put set of all the vowels in the variable vowels. Next using for loop we check if the entered string contains any vowels and then add that string to the empty string and also prints an appropriate message. If the vowel is repeated in the string then we don't add th

PYTHON PROGRAM THAT ASKS USER FOR TWO NUMBERS AND PRINTS CLOSE IF NUMBERS ARE WITHIN .001 OF EACH OTHER AND NOT CLOSE OTHERWISE

Image
  Write a program that asks the user for two numbers and prints Close if the numbers are within .001 of each other and Not close otherwise. CODE: a=float(input("Enter a-value:")) b=float(input("Enter b-value:")) if abs(a-b)<=0.001:     print("Close") else:     print("Not close") EXPLANATION: In this code, the program asks the user to enter two numbers a and b (a>b or b>a) It finds their difference and returns an appropriate message if their difference is within 0.001(<=0.001). Here we use abs() function to find their absolute difference. Absolute  value of a number is the value without considering its sign. Hence  absolute  of 10 is 10, -10 is also 10. If the number is a complex number, abs() returns its magnitude. OUTPUT:

PYTHON PROGRAM THAT GENERATES A RANDOM NUMBER BETWEEN 1 AND 10 AND ASKS THE USER TO GUESS THE NUMBER

Image
  Generate a random number between 1 and 10. Ask the user to guess the number and print a message based on whether they get it right or not. CODE: import random number=random.randint(1,10) guess_number=0; count=0; while guess_number!=number:     guess_number=int(input("Guess the number:"))     count+=1     if guess_number<number:         print("You have guessed a smaller number")     elif guess_number>number:         print("You have guessed a bigger number")     else:         print("Your guess is right")         print("You have tried to guess the random number in",count,"tries") print("Done") EXPLANATION: In this program, firstly we import random module (in-built module in python) into the program . Then to generate a random number between 1 and 10, we use randint() function present in random module. The program will ask the user to guess the number.It will then print a message based on whether they get it righ

PYTHON PROGRAM THAT ALLOWS THE USER TO SPECIFY HOW HIGH THE TRIANGLE SHOULD BE AND PRINTS USING FOR LOOP

Image
  Use a for loop to print a triangle like the one below. Allow the user to specify how high the triangle should be. * *   * *   *   * *   *   *   * CODE: n=int(input("Enter the height of the triangle:")) for i in range(1,n+1):     for j in range(1,i+1):         print("*",end=" ")     print("\n") EXPLANATION: This program will ask the user to enter the height of the triangle. According to the height of the triangle, using for loop it prints the triangle. Outer for loop will iterate n times and inner for loop will iterate i times to print the mentioned triangle. In the print statement present inside the loop, end=" " will print space after the print statement has been executed. In the last line of code "\n" is a line break function which is used to break line and to make next  sentences to compile in the new line(next line). OUTPUT:

PYTHON PROGRAM THAT ASKS USER FOR NAME AND HOW MANY TIMES TO PRINT IT

Image
  Write a program that asks the user for their name and how many times to print it. The program should print out the user’s name the specified number of times CODE: name=input("Enter name:") n=int(input("Number of times to print the name:")) print(name*n) EXPLANATION: For sequences such as string, list and tuple, asterisk(star) * is a repetition operator. For numeric data types, * is used as multiplication operator. OUTPUT: