Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

pythonm

Uploaded by

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

pythonm

Uploaded by

rashmimaruthi2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

13.10.

2022

Computer science

Loops and graphics in python

Sometimes you may need to execute a statement or a group of


statements block more than once in a programming language
loops allow us to execute one or more statement as many as we
want by writing a loop statement only once.

Further the graphics by python module allow us to draw points


lines circles rectangles polygons and print text in a graphics
window with various features in this chapter we discuss various
looping and graphics statement in python

Loops-for and while

Python provides two types of loops=the for loop and the while
loop

For loop -The for loop is used to iterate over a numeric range or
over a collection of items.

For loop is used to iterate over a numeric range or over a


collection items

For loop over a numeric range. The range function is used to set
up for a loop has a following format;

Start end and increment are integer type constant or variables

The range()functions provide a sequence of numbers from start to


end-1;

In the third argument it is not specified by default it is not


specified

The general format for the for loop with a numeric range is:

For variable in range();


Statement();

If the increment is missing it is assumed to be 1; the next value


produced by the range() function is the current value
+increment .if the only one argument is mentioned in the range
start is taken to be zero and increment is taken to be one;

For loop using collection of items;

You can use a for loop with a string a list , a tuple or a dictionary
the general format for this usage :for item in collection statement
(s);

If the collection is a list the loop execute for this statement ;

If the collection is a string then the loop steps through each


character of the string. If the collection is a dictionary then the
loop steps through each key of the key-value pair.

Let us write the python program to display the list even numbers
from one to 10 using a for loop.

Let us first understand how does for loop executes in the


program.

Write a program in python to display it’s a factorial and accept it


from the user. The factorial of a positive number is the product of
all integer between one and the given integer.

While loop

The general format for the while loop is while expression


statement(s);

The statement (s); will be executed repeatedly as long as the


expression is true after executing the statement once the
program re-evaluates the expression if the expression is still true
it executes once again . the process of testing and execution
continues until the expression is false.
Infinite loop

A loop becomes infinite loop if the stop conditions never become


true. An infinite loop might be useful in client /server
programming where the server needs to run continuously so that
the client program scan communicate with it as and when
required .an example for the infinite loop is shown in the figure.

Jump statement = sometimes you may want to get out of the loop
or manipulate the loop variable even before the termination
condition becomes true. jump statements allow you to do
that .python offers three jump statement-break, continue and
pass.

Break statement terminates the loop prematurely and transfers


the execution to the statement immediately following the loop it
can be used to end an infinite loop for example the program
becomes shown in the figure come out of the loop when the value
for the variable is one .

Continue statement

The continue statement forces a loop to skip the rest of the a


statement in the current iteration rest it condition for the next
iteration .foe example in the following program

The continue statement has been used to skip numbers

Pass statement :

The pass statement executes nothing the execution simply


proceeds to the next statement of the loop .the pass statement is
generally used as a place holder for the future implementation of
the programming statements

Bugs and debugging

An error in a program is called a bug .the process of removing an


error from a program is called a debugging
Generally there are the three types of error:

Syntax error

Run time error

Logical error

Runtime error

Sometimes there is no syntax error


Characteristics of health compromising behaviors that undermine
or harm current or future health. Habitual in nature leading to
addiction in certain cases. Substantial contributors to global
burden of disease.
• Produce pleasurable effects: sensory pleasure – alliesthesia
(external stimulus perceived as pleasant if maintains or improves
internal homeostasis, perceived as unpleasant if threatens
internal homeostasis).
• Thrill-seeking behavior.
• Stress reduction, coping mechanisms: avoidance – negative
reinforcement.
• Acquired gradually over time: degree of engagement &
experimentation
• Developmental state seen as window of vulnerability, risk-taking
behaviour.
• Problematic family relationships.
Recogizing and avoiding of Additions
• Someone with an addiction will crave a substance or other
behavioral habits. They’ll often ignore other areas of life to fulfill
or support their desires.
• Lack of control, or inability to stay away from a substance or
behavior
• Decreased socialization, like abandoning commitments or
ignoring relationships
• Ignoring risk factors, like sharing needles despite potential
consequences
• Physical effects, like withdrawal symptoms or needing higher
dosage for effect
• Problems at school or work — frequently missing school or work,
a sudden disinterest in school activities or work, or a drop in
grades or work performance.
Syntax error : syntax s=error occur only when rules for writing a
statement is not allowed :for example:

Missing colon or comma , incorrect indention , incorrect use of a


keyword

Run time error

Sometimes there is no syntax error in the3 program but an error


occurs during the execution of the program this is called as a
runtime error.

For example:

Use of a variable which is not defined

Accessing an element of the list which does not exity

Logical error

This error occurs only when a program executes but does not
produce the required result this is because of the wrong logic
used in the program the python interpreter will not show any
error on the screen you have to find the error by executing the
program yourself using pen and paper(called a dry run) and
correct it.

Write a program to display a numbers from 1 to 10;

# to input a number and display its table

#from 1to 10

Num=int(input(“enter the number”))


For I in range(1,11):

Print(num , “x”,I,)

Write a program to input a number and display whether it is


prime or not

From math import sqrt

Flag=0;

Num=int (input(“enter the number”))

A=int(sqrt(num))

For I in range (2,a+1);

If num%i==0;

Flag=1;

Break;

If flag==0;

Print(“it is a prime number”);

Else

Print(“it is not a prime number”);

Graphics in python

You can draw in python using its graphics module the module
does not come with the python program by default .you have to
install the file graphics.py manually

Starting operating graphics window

To operate /open the graphics window type of the following


codeand save the program

From the graphics import


Win=graphwin()
the first line imports all the objects defined in the graphics.pr
file.the second line opens a graphics window and stores in a
variable win.

By default a new window is created with the title” graphics


window”. the default size of the window is 200*200.

A pixel is a small little square represented by x and y coordinates

The statement win=graph win(“triangle”640,480) will open in a


graphics with the title

Drawing a point

The python statement to draw on the graphics screen at the


coordinates() is point(x,y) Syntax error : syntax s=error occur
only when rules for writing a statement is not allowed :for
example:

Missing colon or comma , incorrect indention , incorrect use of a


keyword

Syntax error : syntax s=error occur only when rules for writing a
statement is not allowed :for example:

Missing colon or comma , incorrect indention , incorrect use of a


keyword

Syntax error : syntax s=error occur only when rules for writing a
statement is not allowed :for example:

Missing colon or comma , incorrect indention , incorrect use of a


keyword

Syntax error : syntax s=error occur only when rules for writing a
statement is not allowed :for example:

Missing colon or comma , incorrect indention , incorrect use of a


keyword
statement is not allowed :for example:

Missing colon or comma , incorrect indention , incorrect use of a


keyword

Drawing a line:

The python statement to draw a line from one point to another


other point

Drawing a circle

You might also like