<< Chapter < Page Chapter >> Page >

However, before you can use a module or the functions contained in a module, you must first import the module into memory. There are a variety of options available when importing modules in Python. You will see an example ofone way to import a module and to use its functions in the next section.

The if...else statement

The if statement shown in Listing 1 basically tells the program to test for a condition. If the condition is True, do something special and then continuebusiness as usual. If the condition is False, don't do anything special -- just continue business as usual.

Sometimes our decision processes are more complicated than that. Sometimes we need the program to test for a condition and if the condition is True, theprogram should do something special. However, if the condition is False, the program should do something different that is also special. After one or theother of the special things is done, the program should continue with business as usual. That is the purpose of the if...else statement.

The pseudo-code for a Python if...else statement is shown in Figure 4 .

Figure 4 . Pseudo-code for a Python if...else statement.
if expression is True: Execute statements at same indentation levelelse: Execute different statements at same indentation levelExecute next statement

As before, the expression must be one that will evaluate to either True or False.

If the expression evaluates to True, the indented statement(s) that follow the expression will be executed in sequence and the indented statement(s) thatfollow else: will be skipped.

If the expression evaluates to False, the indented statement(s) that follow the expression will be skipped and the indented statements that follow else: will be executed.

After that, the next statement following the if statement will be executed.

An example if...else statement is shown in Listing 2 .

Listing 2 . Example of if...else statement usage.
# Illustrates the if...else statement #------------------------------------import random weather = ["sunshine","rain"]rain = False sunshine = False# Loop until rain and sunshine are both true while (rain == False or sunshine == False):# Get today's weather weatherToday = weather[random.randint(0,1)]if weatherToday == "rain": rain = Trueprint("It's raining, visit the museum.") else:sunshine = True print("Sunshine, go to the beach.");print("Vacation is over, go home.")

The code in Listing 2 simulates a short vacation. You want to visit the museum at least once and go to the beach at least once before yougo home.

The code begins by importing the random module as described earlier . Then it creates and populates three working variables that will be used later.

A while loop is used is used to assure that you visit both the museum and the beach at least once before going home. Note the use of theequality operator ( == ) and the logical ( or ) operator in the conditional clause of the while loop.

The first statement inside the while loop uses the random number generator to get the weather for the day from the list named weather . The result will be either "sunshine" or "rain".

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask