You are on page 1of 8

…you can always join us @ faculty of Education, TOPMOST floor(EDB317).

We offer MTH201/202, MEE205/206,


MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)

CSC201-COMMON PYTHON PROGRAMS


COMPILED BY: ENGR MARVIE(08160054997)
NOTE: ‘#’ means comment and explanation of the program. You don’t need to write that. Study and share… The joy is that everyone of us excel.
SUCCESS!!!!!!!!! Check for more https://www.geeksforgeeks.org/output-python-programs https://www.sanfoundry.com/python

LEAP YEAR

A leap year is exactly divisible by 4 except for


century years (years ending with 00). The else:
century year is a leap year only if it is
print("{0} is a leap year".format(year))
perfectly divisible by 400. For example,
else:

2017 is not a leap year print("{0} is not a leap year".format(year))

1900 is a not leap year

2012 is a leap year

2000 is a leap year


MULTIPLICATION TABLE
''' Python program to find the

Source Code multiplication table (from 1 to 10)'''

# Python program to check if the input year is a leap


year or not num = 12
year = 2000

# To get year (integer input) from the user # To take input from the user

# year = int(input("Enter a year: ")) # num = int(input("Display


multiplication table of? "))
if (year % 4) == 0:

if (year % 100) == 0:
# use for loop to iterate 10 times
if (year % 400) == 0:
for i in range(1, 11):
print("{0} is a leap year".format(year)) print(num,'x',i,'=',num*i)
else:

print("{0} is not a leap year".format(year))

…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
ADD TWO NUMBERS

# This program adds two numbers

num1 = 1
The factorial of a number is the product of all
num2 = 6 the integers from 1 to that number.

# Add two numbers


For example, the factorial of 6 (denoted as 6!)
is 1*2*3*4*5*6 = 720. Factorial is not defined
sum = float(num1) + float(num2)
for negative numbers and the factorial of zero
# Display the sum is one, 0! = 1.

print(sum)
Source Code
# Python program to find the factorial of a
PRIME NUMBER
number provided by the user.
Here is source code of the Python Program to check # change the value for a different result
if a number is a prime number. The program output num = 7
is also shown below. # uncomment to take input from the user
a=int(input("Enter number: ")) #num = int(input("Enter a number: "))
k=0
for i in range(2,a//2+1): factorial = 1
if(a%i==0):
k=k+1 # check if the number is negative, positive
if(k<=0): or zero
print("Number is prime") if num < 0:
else: print("Sorry, factorial does not exist
print("Number isn't prime") 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)

EVEN AND ODD NUMBER

# Python program to check if the input


number is odd or even.
# A number is even if division by 2
give a remainder of 0.
# If remainder is 1, it is odd number.

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")
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)

A Fibonacci sequence is the integer


sequence of 0, 1, 1, 2, 3, 5, 8....

The first two terms are 0 and 1. All other


terms are obtained by adding the preceding
# Python program to find the largest number
among the three input numbers
two terms. This means to say the nth term is
the sum of (n-1)th and (n-2)th term.
# change the values of num1, num2 and num3
# for a different result
num1 = 10
num2 = 14
Source Code
num3 = 12 # Program to display the Fibonacci sequence
up to n-th term where n is provided by the
# uncomment following lines to take three user
numbers from user
#num1 = float(input("Enter first number: ")) # change this value for a different result
#num2 = float(input("Enter second number: nterms = 10
"))
#num3 = float(input("Enter third number: ")) # uncomment to take input from the user
#nterms = int(input("How many terms? "))
if (num1 >= num2) and (num1 >= num3):
largest = num1 # first two terms
elif (num2 >= num1) and (num2 >= num3): n1 = 0
largest = num2 n2 = 1
else: count = 0
largest = num3
# check if the number of terms is valid
print("The largest number if nterms <= 0:
between",num1,",",num2,"and",num3,"is",large print("Please enter a positive integer")
st) elif nterms == 1:
print("Fibonacci sequence
upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence
# Python program to find the largest number upto",nterms,":")
among the three input numbers while count < nterms:
print(n1,end=' , ')
# change the values of num1, num2 and num3 nth = n1 + n2
# for a different result # update values
num1 = 10
num2 = 14
num3 = 12

# uncomment following lines to take three Python Program to Display Calendar


numbers from user
#num1 = float(input("Enter first number: ")) Python has a built-in function, calendar to
#num2 = float(input("Enter second number: work with date related tasks. You will learn to
")) display the calendar of a given date in this
#num3 = float(input("Enter third number: ")) example.
if (num1 >= num2) and (num1 >= num3): # Python program to display calendar of
largest = num1 given month of the year
elif (num2 >= num1) and (num2 >= num3):
largest = num2 # import module
else: import calendar
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
yy = 2014
mm = 11

# To ask month and year from the user


# yy = int(input("Enter year: "))
# mm = int(input("Enter month: "))
# Python program to find the sum of
# display the calendar
natural numbers up to n where n is
print(calendar.month(yy, mm))
provided by user

# change this value for a different


result
num = 16
Python Program to Swap Two Variables

In this example, you will learn to swap two


# uncomment to take input from the
variables by using a temporary variable and, user
without using temporary variable.
#num = int(input("Enter a number: "))

# Python program to swap two variables if num < 0:


print("Enter a positive number")
# To take input from the user else:
# x = input('Enter value of x: ') sum = 0
# y = input('Enter value of y: ')
# use while loop to iterate un till
zero
x = 5 while(num > 0):
y = 10 sum += num
num -= 1
# create a temporary variable and swap print("The sum is",sum)
the values
temp = x
x = y
y = temp

print('The value of x after swapping:


{}'.format(x))
print('The value of y after swapping:
{}'.format(y))

…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
celsius * 1.8 = fahrenheit - 32

If a, b and c are three sides of a triangle.


Then,
# Python Program to convert
temperature in celsius to fahrenheit
s = (a+b+c)/2

# change this value for a different


area = √(s(s-a)*(s-b)*(s-c)) result
celsius = 37.5

# Python Program to find the area of


triangle # calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
a = 5 print('%0.1f degree Celsius is equal
b = 6 to %0.1f degree Fahrenheit'
%(celsius,fahrenheit))
c = 7

# Uncomment below to take inputs from


the user
# a = float(input('Enter first side:
'))
# b = float(input('Enter second side:
'))
# c = float(input('Enter third side:
'))

# calculate the semi-perimeter


s = (a + b + c) / 2

# calculate the area


area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is
%0.2f' %area)

In the program below, we take temperature in


degree Celsius and convert it into degree
Fahrenheit. They are related by the formula:
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
a = 1
b = 5
c = 6
In the program below, we have used the for
loop to display the multiplication table of 12.
# To take coefficient input from the
users
# a = float(input('Enter a: '))
''' Python program to find the
# b = float(input('Enter b: '))
multiplication table (from 1 to 10)'''
# c = float(input('Enter c: '))

num = 12
# calculate the discriminant
d = (b**2) - (4*a*c)
# To take input from the user
# num = int(input("Display
multiplication table of? ")) # find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)

# use for loop to iterate 10 times sol2 = (-b+cmath.sqrt(d))/(2*a)

for i in range(1, 11):


print(num,'x',i,'=',num*i) print('The solution are {0} and
{1}'.format(sol1,sol2))

The standard form of a quadratic equation is:

Output
ax2 + bx + c = 0, where

Enter a: 1
a, b and c are real numbers and

Enter b: 5
a ≠ 0

Enter c: 6
# Solve the quadratic equation ax**2 +
bx + c = 0 The solutions are (-3+0j) and (-2+0j)

# import complex math module


import cmath

…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
0x158 in hexadecimal.

Decimal system is the most widely used

Program :
number system. But computer only
understands binary. Binary, octal and
hexadecimal number systems are closely
related and we may require to convert MEAN/AVERAGE
decimal into these systems. Decimal system
is base 10 (ten symbols, 0-9, are used to
represent a number) and similarly, binary is
base 2, octal is base 8 and hexadecimal is num = int(input('How many
base 16. numbers: '))
total_sum = 0
for n in range(num):
A number with the prefix '0b' is considered
numbers = float(input('Enter
binary, '0o' is considered octal and '0x' as
number : '))
hexadecimal. For example:
total_sum += numbers
avg = total_sum/num
print('Average of ', num, '
60 = 0b11100 = 0o74 = 0x3c numbers is :', avg)

# Python program to convert decimal


number into binary, octal and
hexadecimal number system

# Change this line for a different


result
MEDIAN
dec = 344
Code #1 : Working
# Python code to demonstrate the
# working of median() function.
print("The decimal value
of",dec,"is:") # importing statistics module
import statistics
print(bin(dec),"in binary.")
print(oct(dec),"in octal.") # unsorted list of random integers
data1 = [2, -2, 3, 6, 9, 4, 5, -1]
print(hex(dec),"in hexadecimal.")

# Printing median of the


# random data-set
print("Median of data-set is : % s "
% (statistics.median(data1)))
Output
Output :
Median of data-set is : 3.5
The decimal value of 344 is:

0b101011000 in binary.
Code #2 :
# Python code to demonstrate the
0o530 in octal # working of median() on various
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)
# range of data-sets

# importing the statistics module


from statistics import median

# Importing fractions module as fr


from fractions import Fraction as fr

# tuple of positive integer numbers


data1 = (2, 3, 4, 5, 7, 9, 11)

# tuple of floating point values


data2 = (2.4, 5.1, 6.7, 8.9)

# tuple of fractional numbers


data3 = (fr(1, 2), fr(44, 12),
fr(10, 3), fr(2, 3))

# tuple of a set of negative integers


data4 = (-5, -1, -12, -19, -3)

# tuple of set of positive


# and negative integers
data5 = (-1, -2, -3, -4, 4, 3, 2, 1)

# Printing the median of above datsets


print("Median of data-set 1 is % s" % (median(data1)))
print("Median of data-set 2 is % s" % (median(data2)))
print("Median of data-set 3 is % s" % (median(data3)))
print("Median of data-set 4 is % s" % (median(data4)))
print("Median of data-set 5 is % s" % (median(data5)))

Output :
Median of data-set 1 is 5

Median of data-set 2 is 5.9

Median of data-set 3 is 2

Median of data-set 4 is -5

Median of data-set 5 is 0.0

…you can always join us @ faculty of Education, TOPMOST floor(EDB317). We offer MTH201/202, MEE205/206,
MEE203/204, CSC201, EEE201/203, part 1 courses and programing tutorials…Contact Engr marvie for more info.
(08160054997)

You might also like