Skip to main content

Featured

Write a Python Program to find palindrome of a given string

If you reverse the word then you get the same word that word is palindrome. def   palindrome ( str ):     pointer =  str       rev_str =  reversed (pointer)       if   list (pointer) ==  list (rev_str):         print ( "It is palindrome" )       else :         print ( "It is not palindrome" )  palindrome( 'level' ) palindrome( 'madam' ) palindrome( 'life' ) palindrome( 'refer' ) Output:  It is palindrome It is palindrome It is not palindrome It is palindrome

Program

 In todays post lets some basic programs

Program to add two numbers:

a=10
b=20
c=a+b
print(c)

Output: 30

The above example program is about addition of two given number which everyone is well known about that

Program to print sum of n numbers:

num = int(input("enter the value of n:"))
temp = num
sum = 0
if num<=0:
     print("enter positive number")
else:
     while num>0:
          sum = sum+num
          num = num-1
print("sum of ", temp,"number is",sum)

Output:

enter the value of n:5
sum of 5 number is 15

The above example is to add sum of n numbers in this program first it gets a number(num) from user here the temp is used to hold the value of num. If the given number negative number it informs the user to print a positive number. If the number is positive it goes inside the while loop and calculate the sum and finally print the sum

Program to print sum of given n number:

n = int(input("enter how many number:"))
sum = 0
for i in range(0,n):
      num = int(input("enter the number:"))
      sum = sum+num
print("total:",sum)

Output:

Enter how many number:2
Enter the number:2
Enter the number:3
total:5

The before program is same as the above program here  the program gets the number which is to be added from the user itself. The calculation is based on the input value

Program to swap two variables:

a,b = 10,20
print("before swapping")
print(a,b)
a,b = b,a
Print("after swapping")
print(a,b)

Output:

10,20
20,10

Swapping is very simple in python. Swappyin nothing but changing a value of variable to another variable.

Program to describe usage of global variable:

x = 200
def sample ():
    x = 100
    print(x)
sample()
print(x)

Output:

100
200

In this program the variable x is declared two times one is inside a function and another above the program as globally. The variable x=100 cannot be used outside of the function where as the variable x=200 can be used anywhere of the program.

To make it more clear and understandable go to the python console page and try the code with different variable and values or try the program in your editor. If you have any doubt feel free to comment!!!!!

Comments

Popular Posts