example programs
example programs
__________________________________________________________________________________
________
Program 2:
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Output
The sum of 1.5 and 6.3 is 7.8
__________________________________________________________________________________
________
Program 3:
# Add Two Numbers With User Input
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
Output
Enter first number: 1.5
Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8
__________________________________________________________________________________
________
Program 4:
#Python program to find the area of a triangle
Mathematical formula:
Area of a triangle = (s*(s-a)*(s-b)*(s-c))-1/2
Here is the semi-perimeter and a, b and c are three sides of the triangle.
Let's understand the following example.
__________________________________________________________________________________
________
Program 5:
#Python Program to Generate a Random Number
#Importing the random module
import random
# Printing Results
print("Random Number between 0 to 1:", n)
print("Random Number between 0 to 1:", random.random())
print("Random Number between 0 to 1:", random.random())
Output:
Random Number between 0 to 1: 0.11835380687392505
Random Number between 0 to 1: 0.2563622979831378
Random Number between 0 to 1: 0.4344921246881883
__________________________________________________________________________________
________
Program 6:
# Generating a random integer between 0 to 50 using the randint() function
# Importing the random module
import random
# Printing Results
print("Random Number between 0 to 50:", n)
print("Random Number between 0 to 50:", random.randint(0, 50))
print("Random Number between 0 to 50:", random.randint(0, 50))
Output:
Random Number between 0 to 50: 12
Random Number between 0 to 50: 18
Random Number between 0 to 50: 8
__________________________________________________________________________________
________
Program 7:
# Python program to display calendar
import calendar and then apply the syntax
(calendar.month(yy,mm))
# Import calendar
import calendar
# Enter the month and year
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
Odd or Even
Odd and Even numbers:
If you divide a number by 2 and it gives a remainder of 0 then it is known as even number, otherwise
an odd number.
Even number examples: 2, 4, 6, 8, 10, etc.
Odd number examples:1, 3, 5, 7, 9 etc.
PauseNext
Mute
Current Time 0:00
/
Duration 18:10
Loaded: 0.37%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
ADVERTISEMENT
ADVERTISEMENT
See this example:
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even number".format(num))
else:
print("{0} is Odd number".format(num))
Output:
# class attribute
name = ""
age = 0
# access attributes
print(f"{parrot1.name} is {parrot1.age} years old")
print(f"{parrot2.name} is {parrot2.age} years old")
Run Code
Output
Blu is 10 years old
Woo is 15 years old
def eat(self):
print( "I can eat!")
def sleep(self):
print("I can sleep!")
# derived class
class Dog(Animal):
def bark(self):
print("I can bark! Woof woof!!")
Encapsulation
class Computer:
def __init__(self):
self.__maxprice = 900
def sell(self):
print("Selling Price: {}".format(self.__maxprice))
c = Computer()
c.sell()
# change the price
c.__maxprice = 1000
c.sell()
class Square(Polygon):
# renders Square
def render(self):
print("Rendering Square...")
class Circle(Polygon):
# renders circle
def render(self):
print("Rendering Circle...")
Lambda
# lambda call
greet_user('Delilah')
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
Try it Yourself »
Python program to print "Hello Python"
Python program to do arithmetical operations
Python program to find the area of a triangle
Python program to solve quadratic equation
Python program to swap two variables
Python program to generate a random number
Python program to convert kilometers to miles
Python program to convert Celsius to Fahrenheit
Python program to display calendar
Python Program to Check if a Number is Positive, Negative or Zero
Python Program to Check if a Number is Odd or Even
Python Program to Check Leap Year
Python Program to Check Prime Number
Python Program to Print all Prime Numbers in an Interval
Python Program to Find the Factorial of a Number
Python Program to Display the multiplication Table
Python Program to Print the Fibonacci sequence
Python Program to Check Armstrong Number
Python Program to Find Armstrong Number in an Interval
Python Program to Find the Sum of Natural Numbers
Python Program to Print Reverse of a String
Python Program to Print Sum of First Ten Natural Numbers
What is an Array?
An array, one of the most important elements, is used in computer science as a fundamental data
structure in which a collection of elements is stored in the contagious memory locations and all these
data are usually of one type. The arrays give the chance to grouped and access data in a consecutive
chunk of memory space. They can be accessed through their indices or positions in the array.
Let's see the list of programs based on an arrays below:
Example program:
1. num = int (input ("Enter the numerator: "))
2. den = int (input ("Enter the denominator: "))
3. try:
4. res = num/den
5. except ZeroDivisionError:
6. print ("The denominator cannot be zero")
7. else:
8. print ("The result of the division:", res)
Output:
#Sample output-1-No exception rose
Enter the numerator: 3
Enter the denominator: 4
The result of the division: 0.75
1. try:
2. array = [2,3,5,6]
3. print (array)
4. for i in range (0,5):
5. print ("The element in",i,"index is:",array[i])
6. except:
7. print ("Tried to access an out of bound index-check it")
8. else:
9. print("No exceptions. Well done")
10. finally:
11. print("You are now looking at the code in the finally block")
Output:
[2, 3, 5, 6]
The element in 0 index is: 2
The element in 1 index is: 3
The element in 2 index is: 5
The element in 3 index is: 6
Tried to access an out of bound index-check it
You are now looking at the code in the finally block