Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Essentials

You're reading from   Python Essentials Modernize existing Python code and plan code migrations to Python using this definitive guide

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher Packt
ISBN-13 9781784390341
Length 298 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Steven F. Lott Steven F. Lott
Author Profile Icon Steven F. Lott
Steven F. Lott
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting Started FREE CHAPTER 2. Simple Data Types 3. Expressions and Output 4. Variables, Assignment and Scoping Rules 5. Logic, Comparisons, and Conditions 6. More Complex Data Types 7. Basic Function Definitions 8. More Advanced Functions 9. Exceptions 10. Files, Databases, Networks, and Contexts 11. Class Definitions 12. Scripts, Modules, Packages, Libraries, and Applications 13. Metaprogramming and Decorators 14. Fit and Finish – Unit Testing, Packaging, and Documentation 15. Next Steps Index

The if-elif-else statement


Our central tool for conditional processing is the if statement. This is a compound statement which is built from a number of clauses. The initial clause starts with the if keyword. Any number of elif (short for "else if") clauses can be used. Each of these clauses has a conditional expression and an indented suite of statements. We can also add a single catch-all else clause at the end; this doesn't have a condition, but does have a suite of statements.

The minimal if statement, with a single clause, might look like this:

if abs(a-b) < ε:
    print("{a} \N{ALMOST EQUAL TO} {b}".format(a=a, b=b))

The if statement contains a single expression. If the expression is True, the suite of statements is executed. In this case, the suite is a single expression statement, using the print() function.

The else clause can be used in simple if statements.

if count == 0:
    print("Insufficient Data")
else:
    print("Mean = {0:.2f}".format(total/count))

In this case, we have two...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image