## Convert Kilometers to Miles in python

Simple 3 line python program to convert user input kilometer to miles

``````#Accept Distance in Kilometer
#from km to miles need to divide appx  1.609344

km=float(input("\n\t Enter Kilometer to Convert it in miles: "))

#Now converting kilometer to Miles

miles=float((km/1.609344))

print("\n\t %f kilometer in Miles are %f "%(km,miles))
``````

Output

``````	 Enter Kilometer to Convert it in miles: 3

3.000000 kilometer in Miles are 1.864114``````

## Python Program to Find Largest & Smallest among N Numbers

In Python there are many ways to identify largest or smallest number from user input. Here we are using for loop.

``````nrange = int(input("\n\t From how many numbers you want to find smallest & largest number\t"))
num = int(input("\n\t Enter number 1 ==>\t"))
min_num = num
max_num = num
for x in range(nrange-1):

num=int(input('\n\t Enter number %d ==>\t'%(x+2)))
if ( num < min_num ):
min_num = num
if ( num > max_num ):
max_num = num

print('\n\t Maximum number is %d'%(max_num))
print('\n\t Minimum number is %d'%(min_num))``````

Output

``````
From how many numbers you want to find smallest & largest number	3
3

Enter number 1 ==>	3

Enter number 2 ==>	3

Enter number 3 ==>	4

Maximum number is 4

Minimum number is 3
>>> ``````

In above program we are accepting first number from user and declaring it as maximum and minimum number.

Further in for loop we are accepting numbers from user and comparing it with variables maximum and minimum to interchange.

Recommended to write logic on paper and execute it manually then use editor.

## How to replace blank spaces in string in python

Below code will replace blank spaces from string and print output.

``````str1= input('enter string to replace blank spaces')
print('String before replace -->',str1)
str2=str1.replace(" ","")
print('String after replace---->',str2)``````

output

``````String before replace --> This is Python Language
String after replace----> ThisisPythonLanguage``````

## Python code to print the key values in dictionary-like maximum, minimum, specific record

Here is complete running code for python which gives output for printing maximum, minimum from key elements, and associated value. Print complete dictionary. Print specific number record.

``````thisdict = {
"1": "C language",
"2": "Java Language",
"4": "Python Language",
"3": "C++",
}
maxNumber= max(thisdict,key=thisdict.get)
print(maxNumber)
print('\n\n')
minNumber = min(thisdict,key=thisdict.get)
print(minNumber)
print("\n\n")

# Print key-value pairs in dictionary whose key is maxNumber
for key, value in thisdict.items():
if key == maxNumber:
print(key, '::', value)
print('\n\n')

#print complete dictionary
print(thisdict)
print('\n\n')
#print 2nd record from dictionary

n=2
for index, (key, value) in enumerate(thisdict.items()):
if index == n:
print(key, '::', value)
break``````

Output

``````4

1

4 :: Python Language

{'1': 'C language', '2': 'Java Language', '4': 'Python Language', '3': 'C++'}

4 :: Python Language``````

To python code online refer free online compiler.

## Python program to reverse string using for loop

Below is running program in python which accept input string from user. using for loop it is reversing the string.

``````# Sample python program to reverse string using for loop

inputString = input('\n\nEnter string to perform reverse operation\t')

reverseString='' #Declaring empty reverse string variable.

for ch in inputString:
reverseString = ch + reverseString

print('Reverse of  ' ,inputString, '  is   =>',reverseString)``````

Output

``````
Enter string to perform reverse operation	reverse
Reverse of   reverse   is   => esrever
>>> ``````

## Use of interactive prompt or mode in python

On starting python interactive mode we get below output

``````Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
>>>``````

In interactive mode it is not necessary to use “print” command explicitly. see below

``````>>> print("python interactive")
python interactive
>>> "python interactive"
'python interactive'
>>>``````

Interactive prompt runs code and echo immediately. But code which we write on interactive prompt does not get saved. To perform experiment with functions in python we can use this prompt. interactive prompt runs one statement at one time.

Run python program online here

## How python code get executed – architecture

Python is interpreted language. When user execute any python file then internally it compiles the code in bytecode format. Byte code is lower level representation of source code. These compiled (bytecode) files are available on machine in .pyc format.

So when we run the program again without any change then python again load that .pyc files by skipping compilation step.

If python don’t have write access then those pyc files get created in memory and get discarded on exiting program.

Once program is successfully compiled to byte code then it gives those code to Python Virtual Machine. PVM (Python virtual machine) is just run time engine from Python. Which is completely hidden from end user or programmer. PVM converts this bytecode in machine executable.

## Python code to check if character is alphabet or number

Below python code will identify character whether it is alphabet or number using comparison and built in function like isalpha, isdigit

``````
# Program to check if character is alphabet or not.

#Take user input and store it in variable ch
ch = input("Enter a character to check if character is alphabet or not: ")
#comparing characters with a to Z or A to Z

if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')):
print(ch, "is an Alphabet")
else:
print(ch, "is not an Alphabet")

# using IsAlpha function
if (ch.isalpha()):
print(ch, "is an Alphabet identified using Is ALPHA function")
else:
print(ch, "is not an Alphabet")

# using IS digit function
if(ch.isdigit()):
print(ch, "is an number identified using IS Digit function")``````
``````
Enter a character to check if character is alphabet or not:
5
5 is not an Alphabet
5 is not an Alphabet
5 is an number identified using IS Digit function

Enter a character to check if character is alphabet or not:
s
s is an Alphabet
s is an Alphabet identified using Is ALPHA function``````

## Solved AttributeError: ‘str’ object has no attribute ‘IsAlpha’

python is case sensitive language. so better to write python IsAlpha function like isalpha in lower case without any spelling mistake.

AttributeError: ‘str’ object has no attribute ‘isDigit

For above error as well try to write function in lower case without any spelling mistake like “isdigit”