# Useful program in python

The best way to learn any program is to practice the program. You can practice these small program using notepad or some some editor like pycharm.

To run the python program you need to python installed on your system. If you have already installed python on your system then the best way to run the python program is to create a file "program.py" and write your code there.

To run the fille "program.py" follow the steps:

1. Open terminal in MAC or linux (In windows open Commad Prompt and go inside python directory)

2. Create file program.py and write your code

3. Run command:    python program.py

#### Python program to print a Hello world!

``````#  Print Hello, world!

print('Hello, world!')``````

Output

Hello, world!

#### Python program to Multiply Two Numbers

``````# Program to multiply two number

x = 78.5
y = 89.3

# Multiply two numbers
z = float(x) * float(y)

# Display the result
print('The Multiplication of {0} and {1} is {2}'.format(x, y, z))``````

Output

The Multiplicaton of 78.5 and 89.3 is 7010.05

#### Python program to Swap the Variables

``````# Python program to swap the variables
a = 5
b = 10
c = 9
d = 6

# create a temporary variable and swap the values
var = a
a = b
b = c
c = d
d = var

print('The value of a after swapping: {}'.format(a))
print('The value of b after swapping: {}'.format(b))
print('The value of c after swapping: {}'.format(c))
print('The value of d after swapping: {}'.format(d))``````

Output

The value of a after swapping: 10
The value of b after swapping: 9
The value of c after swapping: 6
The value of d after swapping: 5

#### Python program to Convert Celsius to Fahrenheit

``````# Program to convert temperature in celsius to fahrenheit
# Place your value for a different result

celsius = 40.5

# calculate fahrenheit by using formula
fahrenheit = (celsius * 9/5) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))``````

Output

40.5 degree Celsius is equal to 104.9 degree Fahrenheit

#### Python program to Display Calender

``````# Python program to display calendar

# import module
import calendar

yy = 2018
mm = 13

# display the calendar
print(calendar.month(yy, mm))``````

Output

April 2018
Mo Tu We Th Fr Sa Su
1
2  3  4  5  6  7  8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

#### Python program for the Multiplication table

``````num = int(input("Enter the number to print the tables for:"))

# To take input from the user

# use for loop
for i in range(1, 30):
print(num,'x',i,'=',num*i)``````

Output

Enter the number to print the tables for:30
30 x 1 = 30
30 x 2 = 60
30 x 3 = 90
30 x 4 = 120
30 x 5 = 150
30 x 6 = 180
30 x 7 = 210
30 x 8 = 240
30 x 9 = 270
30 x 10 = 300
30 x 11 = 330
30 x 12 = 360
30 x 13 = 390
30 x 14 = 420
30 x 15 = 450
30 x 16 = 480
30 x 17 = 510
30 x 18 = 540
30 x 19 = 570
30 x 20 = 600
30 x 21 = 630
30 x 22 = 660
30 x 23 = 690
30 x 24 = 720
30 x 25 = 750
30 x 26 = 780
30 x 27 = 810
30 x 28 = 840
30 x 29 = 870

#### Python program  to find the sum of Natural Number

``````num = int(input("Enter the number :"))

if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)``````

Output

Enter the number :78
The sum is 3081

#### Python program  to Display the Fibonnacci Sequence

``````# Program to display the Fibonacci sequence up to n-th term where n is implement by the user

# take input from the user
nterms = int(input("Enter a number: "))

# first two terms
num1 = 0
num2 = 1
count = 2

# check whether the number is  valid or not
if nterms <= 0:
print("Plese enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence:")
print(num1)
else:
print("Fibonacci sequence:")
print(num1, ",", num2, end=', ')
while count < nterms:
nth = num1 + num2
print(nth, end=' , ')
# update values
num1 = num2
num2 = nth
count += 1``````

Output

Enter a number: 8
Fibonacci sequence:
0 , 1, 1 , 2 , 3 , 5 , 8 , 13 ,

#### Python program  to Check whether the string is Palindrome or not

A Palindrome is a string which start with some kind of  letter and end with that letter like bob, mom etc

``````# take input from the user
my_string = input("Enter a string: ")

my_string = my_string.casefold()

# reverse the string
reverse_string = reversed(my_string)

# check if the string is equal to its reverse
if list(my_string) == list(reverse_string):
print("It is palindrome")
else:
print("It is not palindrome")``````

Output 1

Enter a string: BOB
It is palindrome

Output 2

Enter a string: Backward
It is not palindrome

#### Python program  to Multiply Matrices

``````# Program to multiply matrices by using the nested loops

# 3x3 matrix
X = [[12, 70, 3],
[9, 6, 6],
[7, 80, 9]]
# 3x4 matrix
Y = [[9, 0, 2, 2],
[46, 7, 13, 10],
[40, 5, 9, 1]]
# result is 3x4
result = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]

# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]

for res in result:
print(res)``````

Output

[3448, 505, 961, 727]
[597, 72, 150, 84]
[4103, 605, 1135, 823]

Keywords: