Table of Contents
ToggleHi, guys welcome to my new article in which I am going to explain some basic Python programs with their output and how to run them.
With the help of this article, you can create all the programs in Python with all the knowledge about Python programs and their syntax and how to run them.
Python Programming
Python is a high-level programming language that is used for web development, computing, data analysis, artificial intelligence, and other purposes. It is also known for its simplicity, readability, and versatility, and has a large and active community of users and developers. Python code is often written in an interpreted format, which means that it does not need to be compiled before it is executed.
Some Common Python Programs
- Program to Print Hello World
- Performing Arithmetic
- Find the area of 2D shapes
- Program to check leap year
- Find the factorial of a number
- Print Fibonacci series
- To check Armstrong’s number
- Find the sum of natural numbers
- To find the ASCII value of the character
- To make a simple calculator
- Programs for swapping two variables
- Program to check whether the number is positive, negative, or 0.
Write a program to print Hello World
print('Hello, World')
//Output in compiler
Hello World
Write a program for performing arithmetic operations
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
# Add two numbers
add = float(num1) + float(num2)
# Subtract two numbers
sub = float(num1) - float(num2)
# Multiply two numbers
mul = float(num1) * float(num2)
#Divide two numbers
div = float(num1) / float(num2)
print('The sum of {0} and {1} is {2}'.format(num1, num2, add))
print('The subtraction of {0} and {1} is {2}'.format(num1, num2, sub))
print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul))
print('The division of {0} and {1} is {2}'.format(num1, num2, div))
//Output in compiler
Enter first number: 20
Enter second number: 10
The sum of 20 and 10 is 30.0
The subtraction of 20 and 10 is 10.0
The multiplication of 20 and 10 is 200.0
The division of 20 and 10 is 2.0
Write a program to find areas of 2D shapes
Area of Circle
π = 3.14
r = float(input("Enter the radius of circle: "))
area = π * r * r
print (" The area of circle is: ", area)
//Output in compiler
Enter the radius of circle: 2
The area of circle is: 12.56
Area of Square
a = float(input("Enter side of square: "))
area = a * a
print (" The area of square is: ", area)
//Output in compiler
Enter side of square: 10
The area of square is: 100.0
Area of Rectangle
l = float(input("Enter length of rectangle: "))
b = float(input("Enter width of rectangle: "))
area = l * b
print (" The area of rectangle is: ", area)
//Output in compiler
Enter length of rectangle: 2
Enter width of rectangle: 4
The area of rectangle is 8.0
Area of Triangle
l = float(input("Enter length of triangle: "))
b = float(input("Enter width of triangle: "))
area = 0.5 * l * b
print (" The area of triangle is: ", area)
//Output in compiler
Enter length of triangle: 2
Enter width of triangle: 4
The area of triangle is: 4.0
Write a python program to check given year is a leap year or not
def CheckLeap(Year):
if((Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0)):
print("Given Year is a leap Year");
else:
print ("Given Year is not a leap Year")
Year = int(input("Enter the number: "))
CheckLeap(Year)
//Output in compiler
Enter the number: 2020
Given Year is a leap Year
Write a program to find the factorial of a number
num = int(input("Enter a number: "))
fact = 1
if num < 0:
print("For negative numbers factorial does not exist")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
fact = fact * i
print("The factorial of",num,"is",fact)
//Output in compiler
Enter a number: 10
The factorial of 10 is 3628800
Write a program to print the Fibonacci series
n = int(input("Enter the terms you want in series: "))
n1 = 0
n2 = 1
count = 0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci series upto",n,":")
print(n1)
else:
print("Fibonacci series:")
while count < n:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
//Output in compiler
Enter the terms you want in series: 10
Fibonacci series:
0
1
1
2
3
5
8
13
21
34
Write a program to check an Armstrong number
n = int(input("Enter any number: "))
sum = 0
temp = n
while temp > 0:
num = temp % 10
sum += num ** 3
temp //= 10
if n == sum:
print(n,"is an Armstrong number")
else:
print(n,"is not an Armstrong number")
//Output in compiler
Enter any number: 1
1 is an Armstrong number
Write a program to find the sum of natural numbers
num = int(input("Enter a positive number: "))
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
//Output in compiler
Enter a positive number: 10
The sum is 55
Write a program to find the ASCII value of the character/code
char = input("Enter a character: ")
print("The ASCII value of '" + char + "' is", ord(char))
//Output in compiler
Enter a character: R
The ASCII value of 'R' is 82
Write a program to make a simple calculator in python
def add(x, y):
return x + y
def sub(x, y):
return x - y
def mult(x, y):
return x * y
def div(x, y):
return x / y
print("Select operation from: ")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
try:
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if choice == '1':
print(n1, "+", n2, "=", add(n1, n2))
elif choice == '2':
print(n1, "-", n2, "=", sub(n1, n2))
elif choice == '3':
print(n1, "*", n2, "=", mult(n1, n2))
elif choice == '4':
print(n1, "/", n2, "=", div(n1, n2))
# check if user wants another calculation
# break the while loop if answer is no
next_calculation = input("Want to do next calculation? (yes/no): ")
if next_calculation == "no":
break
else:
print("Invalid Input")
//Output in compiler
Select operation from:
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 2
Enter first number: 5
Enter second number: 3
5.0 - 3.0 = 2.0
Want to do next calculation? (yes/no): no
Write a program for swapping two values
Using the third variable
A = int( input("Enter value of A: "))
B = int( input("Enter value of B: "))
C = A
A = B
B = C
print ("Value of A after swapping: ", A)
print ("Value of B after swapping: ", B)
//Output in compiler
Enter value of A: 8
Enter value of B: 10
Value of A after swapping: 10
Value of B after swapping: 8
Using comma operator
A = int( input("Enter value of A: "))
B = int( input("Enter value of B: "))
A,B = B,A
print ("Value of A after swapping: ", A)
print ("Value of B after swapping: ", B)
//Output in compiler
Enter value of A: 20
Enter value of B: 10
Value of A after swapping: 10
Value of B after swapping: 20
Using XOR method
A = int( input("Enter value of A: "))
B = int( input("Enter value of B: "))
A = A ^ B
B = A ^ B
A = A ^ B
print ("Value of A after swapping: ", A)
print ("Value of B after swapping: ", B)
//Output in compiler
Enter value of A: 10
Enter value of B: 15
Value of A after swapping: 15
Value of B after swapping: 10
Using arithmetic operations
A = int( input("Enter value of A: "))
B = int( input("Enter value of B: "))
A = A + B
B = A - B
A = A - B
print ("Value of A after swapping: ", A)
print ("Value of B after swapping: ", B)
//Output in compiler
Enter value of A: 10
Enter value of B: 20
Value of A after swapping: 20
Value of B after swapping: 10
Write a program to check whether the number is positive, negative, or 0.
Using if…elif…else
num = float(input("Enter a number: "))
if num > 0:
print("Given number is Positive")
elif num == 0:
print("Zero")
else:
print("Given number is Negative")
//Output in compiler
Enter a number: 3
Given number is Positive
Using Nested if
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Given number is Positive")
else:
print("Given number is Negative")
//Output in compiler
Enter a number: -5
Given number is Negative
How to run Python codes in Linux
First of all, you have to save that file with the (.py) extension and then follow the given options: –
- Open the terminal in that directory where the python file is saved
- Use python interactive mode
- Use IDE or text editor
Execution using terminal
Execution using Python interactive mode
Execution using IDE or text editor
IDE stands for Integrated Development Environment. Many IDEs are present here but the best is the pycharm community. You can download it from here (https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=linux&code=PCC)
If you have any queries regarding the above content, or you want to update anything in the content, then contact us with your queries. You can directly post your question in the group.
Connect with us on these platforms