You are on page 1of 7

Practices for Lesson 1:

Basics of Python
Programming

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
1 Practices for Lesson 1: Basics of Python Programming
Practices for Lesson 1
Overview
In these practices for this lesson, you will install Python IDLE, configure pip, create python
applications using various python programming features along with modules and packages.

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
2 Practices for Lesson 1: Basics of Python Programming
Practice of 1-4: Implementing User Defined Function by using
Modules and Packages
Overview
In this practice, you will create user defined functions and call these functions using modules and
packages.

Assumptions: You should have completed Practices for 1-3


Tasks:

1. Double click on Python IDLE shortcut on your local Desktop to start the Python Shell.
2. Click File from the drop down select New File.
3. Save the file by clicking File Menu from the drop down select Save Menu option.
4. Save the file as myfunctions1.py on your local file system.
5. Copy and paste the below code in myfunctions1.py file. The code consists of functions
to swap two numbers and to find the Fibonacci number for nth digit respectively as shown
below.

def swap(a,b):
a,b=b,a
return a,b

def fibo(n):
if n==0:
return 0
if n==1:
return 1
return fibo(n-1)+fibo(n-2)

6. Save myfunctions1.py file.

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
3 Practices for Lesson 1: Basics of Python Programming
7. Create a new file and save it as myfunctions2.py by navigating to the File menu in
Python Shell.
8. Copy and paste the below code in myfunctions2.py file. The code consists of functions
to check whether a string is palindrome or not and to check the given number is prime
number or not respectively as shown below.
def ispalindrome(s):
if s==s[::-1]:
return True
return False

def isprime(n):
for i in range(2,2//2+1):
if n%i==0:
return False
return True

9. Save myfunctions2.py file


10. Create another new file in Python Shell and save the file as Module_Function.py.
11. Copy and paste the below code in Module_Function.py file. In this Python code, the main
module is defined and to call the module myfunctions1.py and myfunctions2.py is done as
shown below.
import myfunctions1
from myfunctions2 import ispalindrome
def armstrong(num):
result=0
n=len(str(num))
while(num!=0):
digit=num%10
result=result+(digit**n)
num=num//10
return result

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
4 Practices for Lesson 1: Basics of Python Programming
while(1):
print("Select any one from the below choice:")
print("1.Armstrong")
print("2.Generate Fibonacci numbers")
print("3.Check whether a number is prime or not")
choice=int(input("Enter your choice : "))
if choice==1:
num=int(input("Enter the number to find armstrong number
: "))
if num==armstrong(num):
print("It is an armstrong number")
else:
print("It is not an armstrong number")

elif choice==2:
print("Fibonacci of 10 numbers are: ")
print(myfunctions1.fibo(10))

elif choice==3:
print(ispalindrome('abcba'))

else:
print("Invalid choice")
print(50*"_")

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
5 Practices for Lesson 1: Basics of Python Programming
12. Save Module_Function.py file and Click Run Menu from the drop down and select Run
Module option to run the program
13. On successful execution, verify the output as shown below.

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
6 Practices for Lesson 1: Basics of Python Programming
Description of the output:
Choice 1: Checking whether a number is Armstrong or not. Here 153 is an Armstrong
number
Choice 2: Here Fibonacci of 10th number is obtained
Choice 3: Checking whether a string is palindrome or not
Choice 4: Invalid choice

14. Close the file and close Python Shell, by clicking on ‘X’ icon in the Python Shell window.

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
7 Practices for Lesson 1: Basics of Python Programming

You might also like