0% found this document useful (0 votes)
8 views5 pages

Python Programes

The document contains various Python programs demonstrating arithmetic operations, palindrome checking, recursion, time difference between Mars and Earth, factorial calculation, and Fibonacci sequence generation. Each program includes code snippets and explanations of their functionality. The examples are aimed at helping users understand basic programming concepts in Python.

Uploaded by

kavish.jadhav.05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Python Programes

The document contains various Python programs demonstrating arithmetic operations, palindrome checking, recursion, time difference between Mars and Earth, factorial calculation, and Fibonacci sequence generation. Each program includes code snippets and explanations of their functionality. The examples are aimed at helping users understand basic programming concepts in Python.

Uploaded by

kavish.jadhav.05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Programs

1. Write a python Program for Arithmetic operations?


2. Write a python Program palindrome or not?
3. Write a python Program recursion and Non recursion?
4. Write a python Program mars to earth time difference?
5. Write a python Program to find the factorial of a number provided by the
user.
6. Write a python Program to display the Fibonacci sequence up to n-th term

Write a python Program palindrome or not?

s = "malayalam" # string

i,j = 0, len(s) - 1 # two pointers

is_palindrome = True # assume palindrome


while i < j:

if s[i] != s[j]: # mismatch found

is_palindrome = False

break

i += 1

j -= 1

if is_palindrome:

print("Yes")

else:

print("No")

Write a python Program for Arithmetic operations?

num1 = int(input('Enter First number: '))


num2 = int(input('Enter Second number '))
add = num1 + num2
dif = num1 - num2
mul = num1 * num2
div = num1 / num2
floor_div = num1 // num2
power = num1 ** num2
modulus = 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)
print('Floor Division of ',num1 ,'and' ,num2 ,'is :',floor_div)
print('Exponent of ',num1 ,'and' ,num2 ,'is :',power)
print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus)

Write a python Program recursion and Non recursion?


def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result

print("\n\nRecursion Example Results")


tri_recursion(6)

Write a python Program mars to earth time difference?


'''
Plot Earth-Mars relative distance for years 2040-2050
'''

# AWP libraries
import spice_data as sd
import spice_tools as st

# 3rd party libraries


import spiceypy as spice
import numpy as np
import matplotlib.pyplot as plt
plt.style.use( 'dark_background' )

if __name__ == '__main__':
spice.furnsh( sd.leapseconds_kernel )
spice.furnsh( sd.de432s_kernel )

et0 = spice.str2et( '2040-01-01' )


et1 = spice.str2et( '2050-01-01' )
ets = np.arange( et0, et1, 50000 )
rs = st.calc_ephemeris( 399, ets, 'J2000', 4 )[ :, :3 ]
dists = np.linalg.norm( rs, axis = 1 ) / 149.6e6
ts = ( ets - et0 ) / ( 3600 * 24 * 365.0 ) + 2040.0

plt.figure( figsize = ( 12, 8 ) )


plt.plot( ts, dists, 'm' )
plt.xlabel( 'Time (years)' )
plt.ylabel( 'Earth-Mars Relative Distance (AU)' )
plt.title( 'Earth-Mars Relative Distance 2040-2050' )
plt.grid()
plt.show()

Write a python Program to find the factorial of a number provided by the user.

# Python program to find the factorial of a number provided by the user.

# change the value for a different result


num = 7

# To take input from the user


#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Write a python Program to display the Fibonacci sequence up to n-th term

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

You might also like