Python 005
Python 005
(Autonomous)
B.Sc-III,Sem-VI
Skill Enhancement Course
on
Python Programming
Mahesh S Wavare,
April 13, 2020
Contents
1 Print Odd Numbers With in a Given Range 3
1.1 Problem Description . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Program Explanation . . . . . . . . . . . . . . . . . . . . . . . 5
1
4.2 Python Functions . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.2.1 Syntax of Function . . . . . . . . . . . . . . . . . . . . 12
4.2.2 How to call a function in python? . . . . . . . . . . . . 13
4.2.3 Docstrings . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2.4 The return statement . . . . . . . . . . . . . . . . . . . 13
4.2.5 Types of Functions . . . . . . . . . . . . . . . . . . . . 13
4.3 Python Program . . . . . . . . . . . . . . . . . . . . . . . . . 14
2
1 Print Odd Numbers With in a Given Range
1.1 Problem Description
The program takes the upper and lower limit and prints all odd numbers
within a given range provided by user.
1.2 Solution
1. Take in the upper range limit and the lower range limit and store it in
separate variables.
2. Use a for-loop ranging from the lower range to the upper range limit.
3. Then use an if statement if check whether the number is odd or not and
print the number.
4. Exit.
1.3 Program
Here is the source code of the Python Program to print odd numbers within
a given range. The program output is also shown below. Output of this is
3
Figure 2: Output of program for finding odd numbers in a range
4
1.4 Program Explanation
1. User must enter the upper range limit and the lower range limit.
2. The for loop ranges from the lower range limit to the upper range limit.
3. The expression within the if-statement checks if the remainder obtained
when the number divided by 2 is one or not (using the % operator).
4. If the remainder isnt equal to 0, the number is odd and hence the number
is printed.
5
2 Program to find factorial of given natural
number
2.1 Problem Description
The program takes a number and finds the factorial of that number without
using recursion.
2.2 Solution
1. Take a number from the user.
2. Initialize a factorial variable to 1.
3. Use a while loop to multiply the number to the factorial variable and then
decrement the number.
4. Continue this till the value of the number is greater than 0.
5. Print the factorial of the number.
6. Exit.
2.3 Program
6
Figure 4: Output of factorial calculation program
7
2.4 Program Explanation
1. User must enter a natural number.
2. A factorial variable is initialized to 1.
3. A while loop is used to multiply the number to the factorial variable and
then the number is decremented each time.
4. This continues till the value of the number is greater than 0.
5. The factorial of the number is printed.
8
3 Python program to check whether a num-
ber is Prime or not
3.1 Prime Number
A prime number is a natural number greater than 1 that has no positive
divisors other than 1 and itself. The first few prime numbers are 2, 3, 5, 7,
11, .....
3.3 Program
In this program, variable num is checked if it’s prime or not. Numbers less
than or equal to 1 are not prime numbers. Hence, we only proceed if the
num is greater than 1.
We check if num is exactly divisible by any number from 2 to num - 1. If
we find a factor in that range, the number is not prime. Else the number is
prime.
9
Figure 5: Program to decide whether number is prime or not
10
Output
11
4 Program to create simple calculator
4.1 Problem Description
Here we want to make use of python functions to create a simple calculator
.first upon user has to define all functions used in simple arithmetic i.e.
addition substraction,multiplication and division.We will discus here how to
define python functions here.
12
4.2.2 How to call a function in python?
Once we have defined a function, we can call it from another function, pro-
gram or even the Python prompt. To call a function we simply type the
function name with appropriate parameters.
4.2.3 Docstrings
The first string after the function header is called the docstring and is short
for documentation string. It is briefly used to explain what a function
does.Although optional, documentation is a good programming practice. Un-
less you can remember what you had for dinner last week, always document
your code.
13
4.3 Python Program
14
Output
15