Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

I. Learning to Program > 5. Asking Questions with if and else

Chapter 5. Asking Questions with if and else

In programming, we often ask yes or no questions, and decide to do something based on the answer. For example, we might ask, “Are you older than 20?” and if the answer is yes, respond with “You are too old!”

These sorts of questions are called conditions, and we combine these conditions and the responses into if statements. Conditions can be more complicated than a single question, and if statements can also be combined with multiple questions and different responses based on the answer to each question.

In this chapter, you’ll learn how to use if statements to build programs.

if Statements

An if statement might be written in Python like this:

>>> age = 13
>>> if age > 20:
        print('You are too old!')

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint