1.
Python program to add two numbers
# Python3 program to add two numbers
num1 = 15
num2 = 12
# Adding two nos
sum = num1 + num2
# printing values
print("Sum of {0} and {1} is {2}" .format(num1, num2, sum))
2. # Python3 program to add two numbers
number1 = input("First number: ")
number2 = input("\nSecond number: ")
# Adding two numbers
# User might also enter float numbers
sum = float(number1) + float(number2)
# Display the sum
# will print value in float
print("The sum of {0} and {1} is {2}" .format(number1, number2, sum))
3. Maximum of two numbers in Python
# Python program to find the
# maximum of two numbers
def maximum(a, b):
if a >= b:
return a
else:
return b
# Driver code
a=2
b=4
print(maximum(a, b))
4. using max()
# Python program to find the
# maximum of two numbers
a=2
b=4
maximum = max(a, b)
print(maximum)
5. Using Ternary Operator
# Python program to find the
# maximum of two numbers
# Driver code
a=2
b=4
# Use of ternary operator
print(a if a >= b else b)
6. Python Program for simple interest
# Python3 program to find simple interest
# for given principal amount, time and
# rate of interest.
def simple_interest(p,t,r):
print('The principal is', p)
print('The time period is', t)
print('The rate of interest is',r)
si = (p * t * r)/100
print('The Simple Interest is', si)
return si
simple_interest(8, 6, 8)
7. Python Program to check Armstrong Number
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
8. Python Program to check Armstrong Number using functions()
9. Python Program to Check Prime Number
Example 1: Using a flag variable
# Program to check if a number is prime or not
num = 29
# To take input from the user
#num = int(input("Enter a number: "))
# define a flag variable
flag = False
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break
# check if flag is True
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
10. Example 2: Using a for...else statement
# Program to check if a number is prime or not
num = 407
# To take input from the user
#num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
11. Python program to check if a string is palindrome or not
Method #1
1. Find reverse of string
2. Check if reverse and original are same or not.
# function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
12. # Program to check if a string is palindrome or not using functions
my_str = 'aIbohPhoBiA'
# make it suitable for caseless comparison
my_str = my_str.casefold()
# reverse the string
rev_str = reversed(my_str)
# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
13.
Method using one extra variable: In this method, the user takes a character of string
one by one and store it in an empty variable. After storing all the characters user will
compare both the string and check whether it is palindrome or not.
# Python program to check
# if a string is palindrome
# or not
x = "malayalam"
w = ""
for i in x:
w=i+w
if (x == w):
print("Yes")
else:
print("No")
14. Program 3: Palindrome number program using while loop
Num = int(input("Enter a value:"))
Temp = num
Rev = 0
while(num > 0):
dig = num % 10
revrev = rev * 10 + dig
numnum = num // 10
if(temp == rev):
print("This value is a palindrome number!")
else:
print("This value is not a palindrome number!")
15. Reverse words in a given String in Python
## initializing the string
string = "I am a python programmer"
## splitting the string on space
words = string.split()
## reversing the words using reversed() function
words = list(reversed(words))
## joining the words and printing
print(" ".join(words))
16. Reverse the string "Hello World":
txt = "Hello World"[::-1]
print(txt)