Posts

Showing posts from October, 2020

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:   

PYTHON PROGRAM THAT USES FOR LOOP TO PRINT THE NUMBERS 8,11,14,17,20,.........,83,86,89

Image
  Write a program that uses a for loop to print the numbers 8, 11, 14, 17, 20, . . . , 83, 86, 89. SYNTAX OF FOR LOOP:  for iterator_var in sequence:         statement(s) It can be used to iterate over a range and iterators. RANGE() FUNCTION: The range() function returns a sequence of numbers, starting from 0 by default, increments by 1 by default and stops before a specified number. SYNTAX OF RANGE() FUNCTION: range(start value,stop value,step count) EXAMPLE: range(5) --->0,1,2,3,4  ;  range(1,5) ---> 1,2,3,4 ; range(1,7,2) --->1,1+2=3,3+2=5 -->1,3,5   CODE: for i in range(8,90,3):     print(i,end=",") EXPLANATION: In this code the loop iterates over a range(8,90,3) i.e.,it prints 8 and then increments the value by 3. It will continue this process until it reaches 89. In the print statement end="," indicates that each number will be separated by comma in the output. OUTPUT:

PYTHON PROGRAM TO PRINT TOTAL AND AVERAGE OF THREE NUMBERS

Image
  Write a program that asks the user to enter three numbers (use three separate input statements). Create variables called total and average that hold the sum and average of the three numbers and print out the values of total and average. CODE:       a=float(input("Enter a-value:"))       b=float(input("Enter b-value:"))       c=float(input("Enter c-value:"))       total=a+b+c       average=total/3       print("Total is:",total)       print("Average is:",average) EXPLANATION: In first three lines of code, the program asks the user to enter three values (a,b,c). In the next two lines, the programs computes the total and average of the three entered numbers and stores the value in total and average variables respectively. Using print() function it prints the total and average of the three numbers. OUTPUT:

PYTHON PROGRAM TO CONVERT KILOGRAMS TO POUNDS

Image
 BASIC PROGRAM IN PYTHON: 1)  Write a program that asks the user for a weight in kilograms and converts it to pounds. There are 2.2 pounds in a kilogram.      CODE:      weight=float(input("Enter weight in kilograms:"))      pounds=2.2*weight      print("Weight in pounds:",pounds) EXPLANATION: In the first line of code, the input() function in python  asks the user to enter the weight in kilograms. In second line,weight in kilograms will be converted to pounds. In third line, the print() function in python is used to print the weight in pounds. OUTPUT: