0% found this document useful (0 votes)
6 views

Python 005

excellent

Uploaded by

Keerthi bolimera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python 005

excellent

Uploaded by

Keerthi bolimera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Rjarshi Shahu Mahavidyalaya,Latur

(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

2 Program to find factorial of given natural number 6


2.1 Problem Description . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Program Explanation . . . . . . . . . . . . . . . . . . . . . . . 8

3 Python program to check whether a number is Prime or not 9


3.1 Prime Number . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Problem Description . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

4 Program to create simple calculator 12


4.1 Problem Description . . . . . . . . . . . . . . . . . . . . . . . 12

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

Figure 1: Program for finding odd numbers in a range

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

Figure 3: Factorial of Natural number

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.2 Problem Description


The idea to solve this problem is to iterate through all the numbers starting
from 2 to (N/2) using a for loop and for every number check if it divides N.
If we find any number that divides, we return false. If we did not find any
number between 2 and N/2 which divides N then it means that N is prime
and we will return True.

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

Figure 6: Output of a program whether number is prime or not

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.

4.2 Python Functions


In this section, you’ll learn about functions, what a function is, the syntax,
components, and types of functions. Also, you’ll learn to create a function
in Python.
In Python, a function is a group of related statements that performs a specific
task.Functions help break our program into smaller and modular chunks. As
our program grows larger and larger, functions make it more organized and
manageable.Furthermore, it avoids repetition and makes the code reusable.

4.2.1 Syntax of Function


def function name(parameters):
”””docstring”””
statement(s)
Above shown is a function definition that consists of the following compo-
nents.

1. Keyword def that marks the start of the function header.


2. A function name to uniquely identify the function. Function naming fol-
lows the same rules of writing identifiers in Python.
Parameters (arguments) through which we pass values to a function. They
are optional. 3. A colon (:) to mark the end of the function header.
4. Optional documentation string (docstring) to describe what the function
does.
5. One or more valid python statements that make up the function body.
6.Statements must have the same indentation level (usually 4 spaces).
7. An optional return statement to return a value from the function.

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.

4.2.4 The return statement


The return statement is used to exit a function and go back to the place from
where it was called.
Syntax of return
return [expression list]
This statement can contain an expression that gets evaluated and the value
is returned. If there is no expression in the statement or the return statement
itself is not present inside a function, then the function will return the None
object.

4.2.5 Types of Functions


Basically, we can divide functions into the following two types:
1. Built-in functions - Functions that are built into Python.
2. User-defined functions - Functions defined by the users themselves.
Now using by defining functions one can make simple Python Calculator
which is given in following program

13
4.3 Python Program

Figure 7: Python Program to create simple Calculator

14
Output

Figure 8: Output of Python simple Calculator

15

You might also like