Showing posts with label Python programming. Show all posts
Showing posts with label Python programming. Show all posts

Monday, 29 August 2016

Python program to reverse a string using reversed() function

A simple python program to reverse a given string. Here we are goin to use reversed() function. We can't directly print reversed as it is not a string but it returns type 'reversed'. So we will join it to a empty string.

Syntax : ''.join(reversed(str))

Refer below for screenshot and program,



PROGRAM : 

input_string = str(input('Enter any string : '))
print("Reversed String is :" + ''.join(reversed(input_string)))


OUTPUT : 

Python - Reverse a string using reversed() function









Python program to reverse a string using slice operation

A simple python program to reverse a string. Here we use slice operation to reverse a string.

Syntax : str[begin:end:step]. So by leaving begin and end, and giving a step of -1, it reverses a string.

Refer below for screenshot and program,




PROGRAM :

input_string = str(input('Enter any string : '))
print("Reversed String is : " + input_string[::-1])


OUTPUT :  

Python - String reverse using slice operation












Python program to reverse a string without using slice and reversed functions

A simple python program to reverse a given string. Here we are not going to use slice operation or reversed function, which easily can reverse a string without lot of code. we are going to discuss about them in here : using slice and using reversed function.

The logic here is we read a string, read every character from the end and add it to new string.

Refer below for screenshot and program,




PROGRAM : 

input_string = str(input('Enter any string : '))


def reverse(string):
    i = len(string)
    rev = ""
    while i != 0:
        rev = rev + string[i - 1]
        i -= 1
    return rev


print("Reversed String is :" + reverse(input_string))

OUTPUT :  
Python - String reverse
Python - String reverse





Sunday, 17 May 2015

Python Lists - Add , Append , Modify ,Remove , Slice



In this post we will learn about Lists in python .

Here we perform the basic operations on list like adding items to a list , appending an item at the end of the list , modify the value of the item in the list , removing an item from the list , removing all the items from the list and some slicing operations.



PROGRAM :
lst = ['abc', 'def', 'ghi', 'jklm', 'nopqr', 'st', 'uv', 'wxyz', '23', 's98', '123', '87']

# prints the length of the list
print('Length of the list is : ', len(lst))

# appends an item to the list
lst.append('sdd')
print('\nThe list appended a new element "sdd" at the end \n', lst)

# changes the value of an item in a list
lst[0] = 'aaa'
print('\nThe value of the first element in the list is changed to "aaa" \n', lst)

# Slicing
# shows only items starting from 0 upto 3
print('\nList showing only items starting from 0 upto 3\n', lst[:3])

# shows only items starting from 4 to the end
print('\nList showing only items starting from 4 to the end\n', lst[4:])

# shows only items starting from 2 upto 6
print('\nList showing only items starting from 2 upto 6\n', lst[2:6])

# reverse all items in the list
print('\nList items reversed \n', lst[::-1])

# removing items from list
lst[:1] = []
print('\nFirst element is removed from the list \n', lst)

# removing whole list
lst[:] = []
print('\nComplete list removed \n', lst)


OUTPUT : 
Python Lists - Add , Append , Modify ,Remove , Slice


Saturday, 16 May 2015

Python program to calculate the sum of elements in a list


This is a Python program which calculates the sum of all the elements in a list.

Here we use a function called sum() to calculate the sum of all the items in a list.




PROGRAM : 
lst = []
num = int(input('How many numbers: '))
for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)
print("Sum of elements in given list is :", sum(lst))


OUTPUT : 
Python  - Sum of all items in a list



Similar Programs : 
C program to calculate the sum of elements in a array
Cpp program to calculate the sum of elements in a array
Java program to calculate the sum of elements in a array

Python program to find the largest and smallest element in a list



This is a Python program which finds out the smallest and largest element in the list.Here we use 2 functions min() and max() to check for the smallest and largest element respectively.




PROGRAM :
lst = []
num = int(input('How many numbers: '))

for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)

print("Maximum element in the list is :", max(lst), "\nMinumim element in the list is :", min(lst))


OUTPUT : 
Python - Largest and Smallest element in a list
 

Similar Programs : 
C program to find the largest element in an array
C program to find the smallest element in an array
Cpp program to find the largest element in an array
Cpp program to find the smallest element in an array
Java program to find the largest  element in an array
Java program to find the smallest element in an array

Python Program to find the Biggest and Smallest of 3 numbers


This is a Python program  which finds out the smallest and biggest numbers  of 3 numbers.Here we use 2 functions smallest() and biggest() to check for the smallest and biggest number respectively.

PROGRAM :
num1 = int(input('Enter First number : '))
num2 = int(input('Enter Second number : '))
num3 = int(input('Enter Third number : '))


def largest(num1, num2, num3):
    if (num1 > num2) and (num1 > num3):
        largest = num1
    elif (num2 > num1) and (num2 > num3):
        largest = num2
    else:
        largest = num3
    print("The largest of the 3 numbers is : ", largest)


def smallest(num1, num2, num3):
    if (num1 < num2) and (num1 < num3):
        smallest = num1
    elif (num2 < num1) and (num2 < num3):
        smallest = num2
    else:
        smallest = num3
    print("The smallest of the 3 numbers is : ", smallest)


largest(num1, num2, num3)
smallest(num1, num2, num3)


OUTPUT : 
Python - Biggest and Smallest of 3 numbers



Similar Programs :
C Program to find the Biggest and Smallest of 3 numbers
Cpp Program to find the Biggest and Smallest of 3 numbers
Java Program to find the Biggest and Smallest of 3 numbers

Thursday, 14 May 2015

Python Program to check a number is Armstrong number or not


This a Python program to check whether a given number is armstrong or not .
Generally a number is said to be an armstrong number if the cubes of the digits is equal to its original number .





PROGRAM  :
num = int(input('Enter any number : '))
sum = 0
copy = num
while copy > 0:
    remainder=copy%10
    sum += remainder ** 3
    copy //= 10
if num == sum:
    print(num,"is an Armstrong number")
else:
    print(num,"is an not Armstrong number")


OUTPUT : 
Python - Armstrong or not

Python - Armstrong or not




Similar Programs :  
C Program to check a number is Armstrong number or not
Cpp Program to check a number is Armstrong number or not
Java Program to check a number is Armstrong number or not

Python program to check whether a number is Palindrome or not


This is a Python program which checks whether a number is palindrome or not .
Generally a number is said to be a palindrome number if its reverse is same as the original number .

For Example : 121 is a palindrome as its reverse is also 121 where as , 231 is not a palindrome as its reverse is 132 .





PROGRAM :
num = int(input('Enter any number : '))
if str(num) == str(num)[::-1]:
    print("Given number is a PALINDROME number")
else:
    print("Given number is NOT a palindrome number")


OUTPUT : 
Python - Palindorme or not

Python - Palindorme or not



Similar Programs : 
C program to check whether a number is Palindrome or not
Cpp program to check whether a number is Palindrome or not
Java program to check whether a number is Palindrome or not

Python program to find reverse of a number

This is a Python program which finds the reverse of a given  number.

PROGRAM : 
num = int(input('Enter any number : '))
print ('Reverse of the given number is : ',str(num)[::-1])


OUTPUT : 
Python - Reverse of a number




Similar Programs :
C program to find reverse of a number
Cpp program to find reverse of a number
Java program to find reverse of a number

Python program to find average of N numbers



This is a Python program  to find average of N  numbers.
Generally average  is calculated as the sum of all the numbers divided by total numbers.

Average = (n1+n1+.........)/N

PROGRAM :
num = int(input('How many numbers: '))
sum = 0
for n in range(num):
    numbers = int(input('Enter number '))
    sum = sum + numbers

avg = sum/num
print('Average of ', num , 'numbers is :',avg)



OUTPUT  : 
Python - Average of N numbers



Similar Programs :
C program to find average of N numbers
Cpp program to find average of N numbers
Java program to find average of N numbers

Wednesday, 13 May 2015

Python Program to find whether a number is EVEN or ODD

This is a basic program which checks whether the number is Even or Odd.

PROGRAM :
num = int(input('Enter a number: '))

if(num % 2 ==0):
    print(num , "is EVEN")
else:
    print(num , "is ODD")



OUTPUT : 
Python - Even or Odd

Python - Even or Odd



Similar Programs : 
C Program to find whether a number is EVEN or ODD
Cpp Program to find whether a number is EVEN or ODD
Java Program to find whether a number is EVEN or ODD

Python program for performing Arithematic Operations


This is basic python program for all starters. It simply takes two integer numbers adds , subtracts , multiples and divides them.





PROGRAM :
num1 = int(input('Enter First number: '))
num2 = int(input('Enter Second number '))
add = num1 + num2
dif = num1 - num2
mul = num1 * num2
div = num1 / num2
print('Sum of ' ,num1 ,'and' ,num2 ,'is :',add)
print('Difference of ',num1 ,'and' ,num2 ,'is :',dif)
print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)
print('Division of ',num1 ,'and' ,num2 ,'is :',div)



OUTPUT : 
Python  - Arithematic Operations




Similar Programs :
C program for performing Arithematic Operations
Cpp program for performing Arithematic Operations
Java program for performing Arithematic Operations