- 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.
weight = float(input("enter ur weight: "))
pound = (2.2 * weight)
print("After coverting Kgs into pounds is %0.2f" %pound)
- 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.
a=int(input("enter value of a: "))
b=int(input("enter value of b: "))
c=int(input("enter value of c: "))
Total = (a+b+c)
Average = Total/3
print("sum of three numbers is ", Total)
print("Avg of three numbers is ", Average)
print("sum of three numbers is %d and Avg of three numbers is %0.2f " %(Total,Average))
- Write a program that uses a for loop to print the numbers 8, 11, 14, 17, 20, . . . , 83, 86, 89.
for i in range(8,90,3):
print(i,end=' ')
- 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 no of times
username=input("enter ur name")
n=int(input("how many no of times to print"))
c=1
while c<=n:
print("u r name is ",username)
c=c+1
- Use a for loop to print a triangle like the one below. Allow the user to specify how high the triangle should be.
n=15
for i in range(0,n):
for j in range(0,i+1):
print("*",end='')
print()
or
for i in range(1,15):
print(i*"*")
- 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.
import random as ra
random_num = ra.randint(1, 10)
guess=int(input('Guess a number between 1 and 10 until you get it right :'))
while random_num != guess:
print("try again ")
guess = int(input('Guess a number between 1 and 10 until you get it right : '))
print('Well guessed!')
- 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.
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if abs(num1 - num2) <= 0.001:
print("Close")
else:
print("not close")
- Write a program that asks the user to enter a word and prints out whether that word contains any vowels.
vowels = ['A', 'E', 'I', 'o', 'U', 'a', 'e', 'i', 'o', 'u']
a=input("enter word")
c=0
for i in a:
if(i in vowels):
c=c+1
if(c!=0):
print("string contains vowels")
- 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.
a=input("enter string 1: ")
b=input("enter string 2: ")
#result=''
if(len(a)==len(b)):
for i in range(len(a)):
result=a[i]+b[i]
print(result,end='')
else:
print("please enter equal length strings")
- 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.
n=int(input())
print("{:,}".format(n))
- In algebraic expressions, the symbol for multiplication is often left out, as in 3x+4y or
3(x+5). Computers prefer those expressions to include the multiplication symbol, like
3*x+4*y or 3*(x+5). Write a program that asks the user for an algebraic expression
and then inserts multiplication symbols where appropriate.
n=input()
l=list(n)
print("list is",l)
res=''
i=0
while(i<len(l)):
if l[i]=='(':
ind=l.index(')')
str=''.join(l[i:ind+1])
res=res+'*'+str
i=i+len(str)
elif l[i].isalpha():
res=res+'*'+l[i]
i=i+1
else:
res=res+l[i]
i=i+1
print(res)
- Write a program that generates a list of 20 random numbers between 1 and 100.
(a) Print the list.
(b) Print the average of the elements in the list.
(c) Print the largest and smallest values in the list.
(d) Print the second largest and second smallest entries in the list
(e) Print how many even numbers are in the list.
import random as ra
list=[]
for i in range(20):
list.append(ra.randint(1, 100))
print(list)
#print the average of the elements in the list
sum=0
for i in range(20):
sum=sum+list[i]
print("average of all the elements in the list is ", sum/len(list))
#Print the second largest and second smallest entries in the list
lar=lar2=small=small2=list[0]
for i in list[1:]:
if i > lar:
lar2 = lar
lar = i
elif lar2 < i:
lar2 = i
if i < small:
small2 = small
small = i
elif small2 > i:
small2 = i
print("Largest element is : ", lar)
print("Second Largest element is : ", lar2)
print("Smallest element is : ", small)
print("Second Smallest element is : ", small2)
#Print the even numbers in the list
for num in list:
if num%2==0:
print(num,end=' ')
#list comprehension
even = [num for num in list if num % 2 != 0]
print(even)
- Write a program that asks the user for an integer and creates a list that consists of the
factors of that integer.
x=int(input("enter any number: "))
list1=[]
for i in range(1, x + 1):
if x % i == 0:
list1.append(i)
print(list1)
#list comprehension
list=[i for i in range(1, x + 1) if x % i == 0]
print(list)
- Write a program that generates 100 random integers that are either 0 or 1. Then find
the longest run of zeros, the largest number of zeros in a row. For instance, the longest
run of zeros in [1,0,1,1,0,0,0,0,1,0,0] is 4.
import random as ra
list=[]
for i in range(100):
list.append(ra.randint(0, 1))
print(list)
c=0
maxc=0
for i in list:
if i == 0:
c += 1
else:
if c > maxc:
maxc = c
c = 0
print("the longest run of zeros",maxc)
- Write a program that removes any repeated items from a list so that each item appears
at most once. For instance, the list [1,1,2,3,4,3,0,0] would become [1,2,3,4,0].
list=[1,1,2,3,4,3,0,0]
list1=[]
for i in list:
if i not in list1:
list1.append(i)
print(list1)
- Write a program that asks the user to enter a length in feet. The program should then
give the user the option to convert from feet into inches, yards, miles, millimeters,
centimeters, meters, or kilometers. Say if the user enters a 1, then the program
converts to inches, if they enter a 2, then the program converts to yards, etc. While
this can be done with if statements,it is much shorter with lists and it is also easier to
add new conversions if you use lists.
n=float(input("Enter Feet"))
list=[]
list.append(n*12)
list.append(n* 0.33333)
list.append(n*0.00018939)
list.append(n*304.8,2)
list.append(n*30.48)
list.append(n/ 3.2808)
list.append(n/ 3280.8)
print(list)
print("0.Convert Feet to Inches")
print("1.Convert Feet to Yards")
print("2.Convert Feet to Miles")
print("3.Convert Feet to millimeters")
print("4.Convert Feet to centimeters")
print("5.Convert Feet to meters")
print("6.Convert Feet to kilometers")
op=int(input("Choose Options from Above"))
print(list[op])
or
n=float(input("Enter Feet"))
print("1.Convert Feet to Inches")
print("2.Convert Feet to Yards")
print("3.Convert Feet to Miles")
print("4.Convert Feet to millimeters")
print("5.Convert Feet to centimeters")
print("6.Convert Feet to meters")
print("7.Convert Feet to kilometers")
op=int(input("Choose Options from Above"))
if(op==1):
Inches=n*12
print("Feet to Inches value is ", Inches)
if(op==2):
Yards= n * 0.33333
print("Feet to Yards value is ", Yards)
if(op==3):
Miles= n * 0.00018939
print("Feet to Miles value is ", Miles)
if(op==4):
MM= n * 304.8
print("Feet to millimeters value is ", MM)
if(op==5):
CM= n * 30.48
print("Feet to centimeters value is ", CM)
if(op==6):
meters= n / 3.2808
print("Feet to meters value is ", round(meters,3))
if(op==7):
KM= n / 3280.8
print("Feet to kilometers value is ", KM)
- Write a function called sum_digits that is given an integer num and returns the sum of the digits of num.
def sum_digits(n):
sum=0
while n>0:
m=n%10
sum=sum+m
n=n//10
return sum
n=int(input("enter value: "))
print("sum of digits is: ", sum_digits(n))
or
def Sum(n):
sum = 0
for i in n:
sum += int(i)
return sum
n = input("enter value: ")
print("sum of digist is: ",Sum(n))
- Write a function called first_diff that is given two strings and returns the first location in which the strings differ. If the strings are identical, it should return -1.
def first_diff(str1,str2):
if str1 == str2:
return -1
else:
return str1.find(str2)
str1=input("enter string 1: ")
str2=input("enter string 2: ")
print(first_diff(str1,str2))
- Write a function called number_of_factors that takes an integer and returns how many factors the number has.
def number_of_factors(n):
list=[i for i in range(1, n + 1) if n % i == 0]
return len(list)
print("number has %d factors" %number_of_factors(20))
- Write a function called is_sorted that is given a list and returns True if the list is sorted and False otherwise.
def is_sorted1(list2):
c = 0
list1=list2[:]
list1.sort()
if(list2 != list1):
c = 1
if(not c):
return True
else:
return False
list2 = [121, 14, 15, 82, 100]
print(is_sorted1(list2))
- Write a function called root that is given a number x and an integer n and returns x1/n. In the function definition, set the default value of n to 2.
import math
#Method 1
def root(x,n):
return pow(x,1/n)
n=2
x=int(input("enter value"))
print("Method 1 using pow ", root(x,n), "
Method 2, If n value is 2 only using sqrt function ",math.sqrt(x))
- Write a function called primes that is given a number n and returns a list of the first n primes. Let the default value of n be 100.
def primes(n):
list=[]
for num in range(n+1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
list.append(num)
return list
n=int(input("enter n value: "))
print("list of n prime numbers are :", primes(n))
- Write a function called merge that takes two already sorted lists of possibly different lengths, and merges them into a single sorted list.
(a) Do this using the sort method. (b) Do this without using the sort method.
#Without sort Method
def merge(list1,list2):
list3=list1+list2
print(list3)
for i in range (len(list3)):
for j in range(i + 1, len(list3)):
if(list3[i] > list3[j]):
temp = list3[i]
list3[i] = list3[j]
list3[j] = temp
print(list3)
list1=[1,2,3,5,6]
list2=[3,5,9,10]
merge(list1,list2)
#sort Method
def merge(list1,list2):
list3=list1+list2
list3.sort()
print("After merging two list in sorted order",list3)
list1=[1,2,3,5,6]
list2=[3,5,9,10]
merge(list1,list2)
- Write a program that asks the user for a word and finds all the smaller words that can be made from the letters of that word. The number of occurrences of a letter in a smaller word can't exceed the number of occurances of the letter in the user's word.
from itertools import permutations
s=input('Enter a word:::')
for i in range(2,len(s)):
for p in permutations(s,i):
print(''.join(p),end=' ')
- Write a program that reads a file consisting of email addresses, each on its own line. Your program should print out a string consisting of those email addresses separated by semicolons
import re
fo = open("sample.txt", "r+")
str = fo.read()
print ("Read String is : ", str)
lst = re.findall('\S+@\S+', str)
print(";"+lst[0]+";")
fo.close()
26 to 34 Programs Click Here