Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.

Slides:



Advertisements
Similar presentations
Chapter 2.2 – More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld senior.
Advertisements

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
PYTHON FRUITFUL FUNCTIONS CHAPTER 6 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Debugging Introduction to Computing Science and Programming I.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Recitation 1 Programming for Engineers in Python.
An Introduction to Textual Programming
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
General Programming Introduction to Computing Science and Programming I.
A Level Computing#BristolMet Session ObjectivesU2#S10 MUST describe the difference between constants, local and global variables SHOULD explain why constants.
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
Python From the book “Think Python”
How to start Visual Studio 2008 or 2010 (command-line program)
22: Division and The Remainder Theorem © Christine Crisp “Teach A Level Maths” Vol. 1: AS Core Modules.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CS101 Computer Programming I Chapter 4 Extra Examples.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
CSCI-100 Introduction to Computing
Programming for GCSE 1.0 Beginning with Python T eaching L ondon C omputing Margaret Derrington KCL Easter 2014.
Decision Structures, String Comparison, Nested Structures
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Introduction to Eclipse Programming with an Integrated Development Environment.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Introduction to Computing Science and Programming I
what is computer programming?
14 Compilers, Interpreters and Debuggers
Line Continuation, Output Formatting, and Decision Structures
Loops BIS1523 – Lecture 10.
What to do when a test fails
7 - Programming 7P, Q, R - Testing.
IF statements.
Formatting Output.
If, else, elif.
Line Continuation, Output Formatting, and Decision Structures
Learning to Program in Python
Conditions and Ifs BIS1523 – Lecture 8.
Basketball Drill Programming Alternatives
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Types, Truth, and Expressions (Part 2)
COMPUTER PROGRAMMING PYTHON
CS 1111 Introduction to Programming Fall 2018
Control flow : if statements
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
A look at Python Programming Language 2018.
Python programming exercise
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Computer Science
Python While Loops.
Assignment Operators Topics Increment and Decrement Operators
CMPT 120 Lecture 24 – Unit 4 – Computer Vision
Presentation transcript:

Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then the main aspect of the program occurs ie referral to functions and use of if elif etc The last section of your program that i would like you to get used to is : "if name == ‘main’" in a Python program do?

Lets create an example to demonstrate how it works

Step 1 –create a python file named mathsconstants.py Step 2 –type the following code in it

Step 3 –create another python file called inaccuratemaths.py Step 4- type in the code below into inaccuratemaths.py

When we execute or run When this is executed using python inaccuratemaths.py, the value of PI is imported from our mathsconstants.py file and brought into the (namespace of)inaccuratemaths.py As you might expect, the result is: The circumference of a circle with radius 10 is

Carrying out testing on python module mathsconstants.py Now consider that we might want to carry out some testing in our Python module above called mathsconstants.py We might want to add some testing or debug code so that we assure ourselves that the ‘correct’ value is being computed for PI: Type in the following code

And when you run it……. Although we only intend our mathsconstants.py code to be imported into other programs, for testing purposes we can run it directly using python mathsconstants.py. When we run it, we get something like: All good in the hood. Our testing code shows that we’ve got the ‘correct’ value for Pi, and we can see that our mathsconstants.py file is working correctly

What will the impact be of that print() function that we’ve added for testing purposes? we have forgotten that the mathsconstants.py file is also being imported into inaccuratemaths.py program. When a Python program is imported into another one, it is effectively executed as it is imported. So the from mathsconstants import PI line, imports the mathsconstants.pyfile and executes it as it goes. Which means it will find the print() function that we added, and execute that too.

If we now run our inaccuratemaths.py program, we will get this output: You can see that the print() function from mathsconstants.py is being executed. Our testing/debug code is being run when we didn’t want it to be. What we need is a way to distinguish if our code is being executed as part of an import statement, or if it being executed as a standalone program. Python’s very cryptic solution is to check it’s internal name of the source code file. If it’s being executed as a standalone program, Python will set its __name__ reference to be equal to the string __main__

However, if the Python code is being imported, the __name__ reference will be set to the name of the source file without the “.py” ending - mathsconstants in our example. So to fix this, we can add the following code to our mathsconstants.py program so that our testing code is not run if the file is being imported, but is executed only if we run the program from the command line: now if we execute our inaccuratemaths.py code, we will no longer get the testing output from our mathsconstants.py code: Now save the maths constants program with the if_main now in it then run the inaccurate maths program

And finally……….. So what using if_main does is allow the function created in maths constant and enable it to be imported and used as many times as you want. what it will do is allow the reference to the data in the program but it will not execute the program and print the data. So in inaccuratemaths.py program result of running the program will be You will notice it doesn’t print out the print statement in the referral program mathsconstant.py It essentially enables an absolute reference point If you don’t use if_main what will happen is the program you have created called maths constant will print What you want is only reference to the part of the program called pi To do that you must use if_main otherwise when you import a python program it will assume you want to execute or run all of the code in the program