Python Program
1. Aim: - Python program to demonstrate basic data-type in python
Program:-
print("Demonstrate basic data-type in Python") x = 20 #int
print(x) print(type(x)) x = 20.5 #float print(x) print(type(x))
x = 1j #complex
print(x) print(type(x)) #String
print("String data-type
in Python") x = "Name :- Ravish Gupta" print(x)
print(type(x))
x = ["Go", "Edu", "Hub"] #list
print(x)
print(type(x))
Output:-
Demonstrate basic data-type in Python 20
<class 'int'>
20.5
<class
'float'> 1j
<class 'complex'>
String data-type
in Python Name :- Ravish
Gupta
<class 'str'>
['Go', 'Edu', 'Hub']
<class 'list'>
2. Aim: - write a program that takes 2 numbers as
command line arguments and print its
Addition/ Subtraction/ Multiplication/ Division
in python
Program:-
print("write a program that takes 2 numbers as command line arguments and print its Addition/ Subtraction/ Multiplication/ Division in python")
num1 =
int(input("Enter First Number: "))
num2 = int(input("Enter Second Number:
"))
print("Enter which operation would you like to perform?")
ch = input("Enter
any of these char for specific
operation +,-,*,/: ")
result = 0 if ch == '+':
result = num1 + num2 elif ch == '-':
result = num1 - num2
elif ch == '*':
result = num1 * num2 elif ch == '/':
result = num1 / num2 else:
print("Input character is not recognized!")
print(num1, ch , num2,
":", result)
3. Aim: - write a program for checking whether
the given number
is an even number or not in
python.
Program:-
print("Program for checking whether
the given number
is an even number or not in
python")
num = int(input("Enter any number: "))
flag = num%2
if flag == 0:
print(num, "is an even number") elif flag == 1:
print(num, "is an odd number") else:
print("Error, Invalid input")
4. Aim: - write a Program for calculate the simple
interest and compound interest in python Program:-
print("Program for calculate the simple interest
and compound interest
in python")
#Input
P = input("\n Enter the principal
amount: ") T = input("\n Enter the time: ")
R = input("\n Enter the rate: ") #Process
Si = (int(P) * float(T)
* float(R) ) /100
Ci = int(P)
* (((1 + float(R)/100) ** int(T)) - 1) #Output
print("\n
Simple Interest = ",Si) print("\n Compound
Interest = ",Ci)
Output:-
Program
for calculate the simple interest and compound interest in python Enter
the principal amount: 1000
Enter the time: 6 Enter the rate: 9
Simple Interest = 540.0
Compound Interest = 677.1001108410006
5. Aim: - write a Python program to find the largest
number among the three input numbers take three numbers from user
Program:-
print("Python program
to find the largest number
among the three input numbers
take three numbers from user")
num1 =
float(input("Enter first number: "))
num2 = float(input("Enter second number: ")) num3 =
float(input("Enter third
number: "))
if (num1 > num2) and (num1 > num3):
largest = num1
elif (num2 > num1) and (num2
> num3):
largest = num2 else:
largest = num3
print("The largest
number is",largest)
Output:-
Python program to
find the largest number among the three input numbers take three numbers from user
Enter first
number: 45 Enter second number: 100 Enter
third number: 12
The largest number is 100.0
6. Aim: - write a Python Program to Convert Binary
to Decimal and Decimal to Binary
Program:-
print("Python
Program to Convert Binary to Decimal and Decimal to Binary")
b_num = list(input("Input a binary number:
")) value = 0
for i in range(len(b_num)): digit = b_num.pop() if digit
== '1':
value = value + pow(2, i) print("The decimal
value of the number is",
value) def convertToBinary(n):
if n > 1:
convertToBinary(n//2) print(n % 2,end
= '')
# decimal number
dec = int(input("Input a decimal number: "))
convertToBinary(dec) print()
Output:-
Python
Program to Convert Binary to Decimal and Decimal to Binary Input a binary number: 10110
The decimal
value of the number is 22 Input
a decimal number: 22
10110
7. Aim: - write a Python Program for Exponential
Series
Program:-
print("Exponental Series ")
x = int(input("Enter the value of x: ")) n = int(input("Enter the value of n: ")) s = 1
nr = 1
for i in range(1,n+1): nr = ((nr**x)/i)
print(nr) s+=nr
print("The sum of the series: ", s)
Output:-
Exponential Series Enter
the value of x: 5 Enter the value of
n: 6 7.7910288904852e-268
The sum of the series: 1.0
8. Aim: - write a Program to check if a string is
palindrome or not
Program:-
print("Program to check if a string
is palindrome or not")
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.")
Output:-
Program to check if a string is palindrome or not the string is a palindrome.
9. Aim: - write a Python Program for Plotting A Sine
Wave Using Matplotlib
Program: - Plotting A Sine Wave Using Matplotlib
import numpy as np
import
matplotlib.pyplot as plot # Get x
values of the sine wave time = np.arange(0, 10, 0.1);
# Amplitude of
the sine wave is sine of a variable like time
amplitude = np.sin(time)
# Plot a
sine wave using time and amplitude obtained for the
sine wave plot.plot(time, amplitude)
# Give a title
for the sine wave plot
plot.title('Sine wave')
# Give x
axis label for the sine wave plot
plot.xlabel('Time')
# Give y axis label for the sine wave plot
plot.ylabel('Amplitude = sin(time)') plot.grid(True,
which='both') plot.axhline(y=0, color='k')
plot.show()
# Display the sine wave plot.show()
Output:-
10. Aim: - write a Python Program for Plotting a Cosine
Waves Using Python Matplotlib
Program: - Plotting a Cosine
Waves Using Python Matplotlib
import numpy as np
import matplotlib.pyplot as plot # Get x values of the cosine
wave time = np.arange(0,
20, 0.2);
# Amplitude of the cosine
wave is cosine of a variable like time amplitude = np.cos(time)
# Plot a
cosine wave using time and amplitude
obtained for the cosine
wave plot.plot(time, amplitude)
# Give a title
for the cosine
wave plot plot.title('Cosine wave')
# Give x
axis label for the cosine
wave plot plot.xlabel('Time')
# Give y axis label for the cosine wave plot plot.ylabel('Amplitude = cosine(time)')
# Draw the grid for the graph plot.grid(True, which='both')
plot.axhline(y=0, color='b')
# Display the cosine wave plot plot.show()
Comments
Post a Comment