01. Python Basics and Strings

Problem

If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator ‘%’).

# Obtain sum of digits of a 5 digit number
sum = 0
num = int(input('Enter a 5 digit number: '))
a = num % 10   	 
n = num // 10    	
sum = sum + a  	
a = n % 10 			
n = n // 10 
sum = sum + a 
a = n % 10 			
n = n // 10 
sum = sum + a 
a = n % 10 			
n = n // 10 
sum = sum + a 
a = n % 10 			
sum = sum + a 
print('Sum of 5 digits is = ', sum)

Interaction

Enter a 5 digit number: 12345
Sum of 5 digits of is = 15

Problem

Write a program to receive Cartesian co-ordinates (x, y) of a point and convert them into polar co-ordinates (r, ).

# Convert Cartesian co-ordinates to Polar co-ordinates 
import math
x = float(input('Enter x co-ordinate: '))
y = float(input('Enter y co-ordinate: '))
r = math.sqrt(x * x + y * y) 
theta = math.atan2(y, x) 
theta = theta * 180 / 3.14 
print('r = ', r, '\ntheta = ', theta)

Interaction

Enter x co-ordinate: 7
Enter y co-ordinate: 7
r =  9.899494936611665 
theta =  45.022824653356906

Problem

Write a program to receive values of latitude (L1, L2) and longitude (G1, G2), in degrees, of two places on the earth and outputs the distance between them in nautical miles. The formula for distance in nautical miles is:
D = 3963 acos ( sin L1 sin L2 + cos L1cos L2 * cos ( G2 – G1 ).

# Calculate distance between two places in Nautical Miles 
import math
lat1 = float(input('Enter Latitude of Place 1: '))
lon1 = float(input('Enter Longitude of Place 1: ')) 
lat2 = float(input('Enter Latitude of Place 2: '))
lon2 = float(input('Enter Longitude of Place 2: '))
lat1 = lat1 * 3.14 / 180 
lat2 = lat2 * 3.14 / 180 
lon1 = lon1 * 3.14 / 180 
lon2 = lon2 * 3.14 / 180
d = 3963 * math.acos(math.sin(lat1) * math.sin(lat2) + math.cos(lat1) * math.cos(lat2) * math.cos(lon2 - lon1)) 
print('Distance between Place 1 and Place 2 = ', d)

Interaction

Enter Latitude of Place 1: 45
Enter Longitude of Place 1: 45
Enter Latitude of Place 2: 90
Enter Longitude of Place 2: 90
Distance between Place 1 and Place 2 = 3111.8790644500605

Problem

Wind chill factor is the felt air temperature on exposed skin due to wind. The wind chill temperature is always lower than the air temperature, and is calculated as per the following formula:
wcf = 35.74 + 0.6215t + ( 0.4275t – 35.75 ) * v0.16

where t is the temperature and v is the wind velocity. Write a program to receive values of t and v and calculate wind chill factor.

# Calculation of wind chill factor 
temp = float(input('Enter values of temperature: '))
vel = float(input('Enter values of velocity: ')) 
wcf = 35.74 + 0.6215 * temp + (0.4275 * temp - 35.75) * pow (vel, 0.16) 
print('Wind Chill Factor = ', wcf)

Interaction

Enter values of temperature: 30
Enter values of velocity: 0.16
Wind Chill Factor = 37.28608546612262

Problem

If value of an angle is input through the keyboard, write a program to print all its Trigonometric ratios.

# Print all Trignometric ratios of an angle
import math
angle = float(input('Enter an angle: '))
angle = angle * 3.14 / 180   
s =  math.sin(angle)  
c = math.cos(angle) 
t = math.tan(angle) 
print('sin = ', s,'\ncos = ', c, '\ntan = ', t)

Interaction

Enter an angle: 45
sin = 0.706825181105366 
cos = 0.7073882691671998 
tan = 0.9992039901050427

Problem

If value of an angle is input through the keyboard, write a program to print all its Trigonometric ratios.

# Interchanging of contents of two variables c & d 
c = int(input('Enter the number at location C: '))
d = int(input('Enter the number at location D: '))  
d, c = c, d  
print('New Number at location C = ', c) 
print('New Number at location D = ', d) 

Interaction

Enter the number at location C: 10
Enter the number at location D: 6
New Number at location C =  6
New Number at location D =  10

Problem

Write a program that takes a string as a parameter and returns a string with every successive repetitive character replaced with a *. For example ‘bookkeeping’ is returned as ‘bo*k*e*ping’.

s = input('Enter any string: ')
t = ''
i = 0
while i < len(s) - 1 :
    if s[i] == s[i + 1] :
        t = t + s[i]
        t = t + '*'
        i += 2
    else :
        t = t + s[i]
        i += 1
t = t + s[-1]
print(t)

Interaction

Enter any string: bookkeeping
bo*k*e*ping

Problem

Write a program that reports whether a string is anagram of another string. A pair of strings form an anagram if the letters in one string can be arranged to form the other.

# To check whether a string is anagram of another string
s1 = input('Enter a string: ')
s2 = input('Enter a string: ')
if sorted(s1) == sorted(s2) :
    print('Strings form anagram')
else :
    print(' Strings do not form anagram ')

Interaction

Enter a string: rail safety
Enter a string: fairy tales
Strings form anagram

Problem

Write a program that takes a sentence as an input and displays the number of words in the sentence.

s = input('Enter a sentence: ')
count = len(s.split(sep = ' '))
print('Number of words = ', count)

Interaction

Enter a sentence: A very talented man
No of words in a sentence: 4 

Problem

Write

Interaction