Python 101
Michael Driscoll
This book is for sale athttp://leanpub.com/python_101 This version was published on 2016-10-27
This is aLeanpubbook. Leanpub empowers authors and publishers with the Lean Publishing process.Lean Publishingis the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
Also By
Michael Driscoll
Python 201Contents
Introduction . . . . 1
A Brief History of Python . . . 3
About the Author . . . 3
Conventions . . . 4
Requirements . . . 4
Reader Feedback . . . 4
Errata . . . 5
Part I - Learning the Basics . . . . 6
Chapter 1 - IDLE Programming . . . . 8
Using IDLE . . . 8
Other Tips . . . 12
Wrapping Up . . . 13
Chapter 2 - All About Strings . . . 14
How to Create a String . . . 14
String Concatenation . . . 16
String Methods . . . 16
String Slicing . . . 18
String Formatting . . . 19
Wrapping Up . . . 23
Chapter 3 - Lists, Tuples and Dictionaries . . . 24
Lists . . . 24
Tuples . . . 26
Dictionaries . . . 26
Wrapping Up . . . 28
Chapter 4 - Conditional Statements . . . 29
The if statement . . . 29
Boolean Operations . . . 31
Checking for Nothing . . . 32
Special Characters . . . 34
CONTENTS
Wrapping Up . . . 35
Chapter 5 - Loops . . . 36
The for Loop . . . 36
The while Loop . . . 38
What else is for in loops . . . 40
Wrapping Up . . . 41
Chapter 6 - Python Comprehensions . . . 42
List Comprehensions . . . 42
Dictionary Comprehensions . . . 43
Set Comprehensions . . . 44
Wrapping Up . . . 44
Chapter 7 - Exception Handling . . . 45
Common Exceptions . . . 45
How to Handle Exceptions . . . 46
The finally Statement . . . 48
try, except, or else! . . . 49
Wrapping Up . . . 50
Chapter 8 - Working with Files . . . 51
How to Read a File . . . 51
How To Read Files Piece by Piece . . . 52
How to Read a Binary File . . . 53
Writing Files in Python . . . 53
Using the with Operator . . . 54
Catching Errors . . . 54
Wrapping Up . . . 55
Chapter 9 - Importing . . . 56
import this . . . 56
Using from to import . . . 57
Importing Everything! . . . 57
Wrapping Up . . . 58
Chapter 10 - Functions . . . 59
An Empty Function (the stub) . . . 59
Passing Arguments to a Function . . . 59
Keyword Arguments . . . 60
*args and **kwargs . . . 62
A Note on Scope and Globals . . . 62
Coding Tips . . . 63
CONTENTS Chapter 11 - Classes . . . 65 Creating a Class . . . 65 What is self? . . . 67 Subclasses . . . 69 Wrapping Up . . . 70
Part II - Learning from the Library . . . 71
Chapter 12 - Introspection . . . 73
The Python Type . . . 73
The Python Dir . . . 74
Python Help! . . . 75
Wrapping Up . . . 75
Chapter 13 - The csv Module . . . 76
Reading a CSV File . . . 76
Writing a CSV File . . . 77
Wrapping Up . . . 80
Chapter 14 - configparser . . . 81
Creating a Config File . . . 81
How to Read, Update and Delete Options . . . 82
How to Use Interpolation . . . 85
Wrapping Up . . . 85
Chapter 15 - Logging . . . 86
Creating a Simple Logger . . . 86
How to log From Multiple Modules (and Formatting too!) . . . 87
Configuring Logs for Work and Pleasure . . . 90
Wrapping Up . . . 93
Chapter 16 - The os Module . . . 94
os.name . . . 94
os.environ, os.getenv() and os.putenv() . . . 95
os.chdir() and os.getcwd() . . . 97
os.mkdir() and os.makedirs() . . . 97
os.remove() and os.rmdir() . . . 98
os.rename(src, dst) . . . 98 os.startfile() . . . 99 os.walk() . . . 99 os.path . . . 100 os.path.basename . . . 100 os.path.dirname . . . 100 os.path.exists . . . 101
CONTENTS
os.path.isdir / os.path.isfile . . . 101
os.path.join . . . 102
os.path.split . . . 102
Wrapping Up . . . 103
Chapter 17 - The email / smtplib Module . . . 104
Email Basics - How to Send an Email with smtplib . . . 104
Sending Multiple Emails at Once . . . 107
Send email using the TO, CC and BCC lines . . . 109
Add an attachment / body using the email module . . . 110
Wrapping Up . . . 112
Chapter 18 - The sqlite Module . . . 113
How to Create a Database and INSERT Some Data . . . 113
Updating and Deleting Records . . . 114
Basic SQLite Queries . . . 115
Wrapping Up . . . 116
Chapter 19 - The subprocess Module . . . 117
The call function . . . 117
The Popen Class . . . 119
Learning to Communicate . . . 119
Wrapping Up . . . 121
Chapter 20 - The sys Module . . . 122
sys.argv . . . 122
sys.executable . . . 123
sys.exit . . . 123
sys.path . . . 125
sys.platform . . . 125
sys.stdin / stdout / stderr . . . 126
Wrapping Up . . . 126
Chapter 21 - The threading module . . . 127
Using Threads . . . 127
Writing a Threaded Downloader . . . 128
Using Queues . . . 131
Wrapping Up . . . 133
Chapter 22 - Working with Dates and Time . . . 135
The datetime Module . . . 135
datetime.date . . . 135
datetime.datetime . . . 136
CONTENTS
The time Module . . . 138
time.ctime . . . 138
time.sleep . . . 139
time.strftime . . . 139
time.time . . . 140
Wrapping Up . . . 140
Chapter 23 - The xml module . . . 141
Working with minidom . . . 141
Parsing with ElementTree . . . 146
How to Create XML with ElementTree . . . 146
How to Edit XML with ElementTree . . . 148
How to Parse XML with ElementTree . . . 150
Wrapping Up . . . 152
Part III - Intermediate Odds and Ends . . . 153
Chapter 24 - The Python Debugger . . . 155
How to Start the Debugger . . . 155
Stepping Through the Code . . . 157
Setting breakpoints . . . 158
Wrapping Up . . . 158
Chapter 25 - Decorators . . . 159
A Simple Function . . . 159
Creating a Logging Decorator . . . 161
Built-in Decorators . . . 162
@classmethod and @staticmethod . . . 163
Python Properties . . . 164
Replacing Setters and Getters with a Python property . . . 165
Wrapping Up . . . 169
Chapter 26 - The lambda . . . 170
Tkinter + lambda . . . 170
Wrapping Up . . . 171
Chapter 27 - Code Profiling . . . 173
Profiling Your Code with cProfile . . . 173
Wrapping Up . . . 176
Chapter 28 - An Intro to Testing . . . 177
Testing with doctest . . . 177
Running doctest via the Terminal . . . 177
Running doctest Inside a Module . . . 179
CONTENTS
Test Driven Development with unittest . . . 182
The First Test . . . 183
The Second Test . . . 185
The Third (and Final) Test . . . 187
Other Notes . . . 189
Wrapping Up . . . 189
Part IV - Tips, Tricks and Tutorials . . . 190
Chapter 29 - Installing Modules . . . 192
Installing from Source . . . 192
Using easy_install . . . 193 Using pip . . . 193 A Note on Dependencies . . . 194 Wrapping Up . . . 194 Chapter 30 - ConfigObj . . . 195 Getting Started . . . 195 Using a configspec . . . 196 Wrapping Up . . . 198
Chapter 31 - Parsing XML with lxml . . . 199
Parsing XML with lxml . . . 200
Parsing the Book Example . . . 201
Parsing XML with lxml.objectify . . . 202
Creating XML with lxml.objectify . . . 205
Wrapping Up . . . 208
Chapter 32 - Python Code Analysis . . . 209
Getting Started with pylint . . . 209
Analyzing Your Code . . . 209
Getting Started with pyflakes . . . 212
Wrapping Up . . . 213
Chapter 33 - The requests package . . . 214
Using requests . . . 214
How to Submit a Web Form . . . 215
Wrapping Up . . . 216
Chapter 34 - SQLAlchemy . . . 217
How to Create a Database . . . 217
How to Insert / Add Data to Your Tables . . . 220
How to Modify Records with SQLAlchemy . . . 222
How to Delete Records in SQLAlchemy . . . 223
CONTENTS
Wrapping Up . . . 225
Chapter 35 - virtualenv . . . 226
Installation . . . 226
Creating a Virtual Environment . . . 226
Wrapping Up . . . 227
Part V - Packaging and Distribution . . . 228
Chapter 36 - Creating Modules and Packages . . . 230
How to Create a Python Module . . . 230
How to Create a Python Package . . . 232
Wrapping Up . . . 234
Chapter 37 - How to Add Your Code to PyPI . . . 235
Creating a setup.py File . . . 235
Registering Packages . . . 236
Uploading Packages to PyPI . . . 237
Wrapping Up . . . 238
Chapter 38 - The Python egg . . . 239
Creating an egg . . . 239
Wrapping Up . . . 240
Chapter 39 - Python wheels . . . 241
Getting Started . . . 241
Creating a wheel . . . 241
Installing a Python wheel . . . 242
Wrapping Up . . . 243
Chapter 40 - py2exe . . . 244
Creating a Simple GUI . . . 244
The py2exe setup.py file . . . 246
Creating an Advanced setup.py File . . . 248
Wrapping Up . . . 249
Chapter 41 - bbfreeze . . . 250
Getting Started with bbfreeze . . . 250
Using bbfreeze’s Advanced Configuration . . . 252
Wrapping Up . . . 255
Chapter 42 - cx_Freeze . . . 256
Getting Started with cx_Freeze . . . 256
Advanced cx_Freeze - Using a setup.py File . . . 258
CONTENTS
Chapter 43 - PyInstaller . . . 261
Getting Started with PyInstaller . . . 261
PyInstaller and wxPython . . . 263
Wrapping Up . . . 266
Chapter 44 - Creating an Installer . . . 267
Getting Started with GUI2Exe . . . 268
Let’s Make an Installer! . . . 270
Wrapping Up . . . 273
Appendix A: Putting It All Together . . . 275
The Background . . . 275
The Specification . . . 275
Breaking the Specification Down . . . 276
Turning Chapters Into a Book . . . 276
Reading the Customer CSV File . . . 278
Emailing the PDF . . . 279
Putting it all Together . . . 281
Introduction
Welcome to Python 101! I wrote this book to help you learn Python 3. It is not meant to be an exhaustive reference book. Instead, the object is to get you acquainted with the building blocks of Python so that you can actually write something useful yourself. A lot of programming textbooks only teach you the language, but do not go much beyond that. I will endeavour to not only get you up to speed on the basics, but also to show you how to create useful programs. Now you may be wondering why just learning the basics isn’t enough. In my experience, when I get finished reading an introductory text, I want to then create something, but I don’t know how! I’ve got the learning, but not the glue to get from point A to point B. I think it’s important to not only teach you the basics, but also cover intermediate material.
Thus, this book will be split into five parts: • Part one will cover Python’s basics
• Part two will be on a small subset of Python’s Standard Library • Part three will be intermediate material
• Part four will be a series of small tutorials
• Part five will cover Python packaging and distribution |
Let me spend a few moments explaining what each part has to offer. In part one, we will cover the following:
• Python types (strings, lists, dicts, etc) • Conditional statements
• Loops
• List and dictionary comprehensions • Exception Handling
• File I/O
• Functions and Classes |
Part two will talk about some of Python’s standard library. The standard library is what comes pre-packaged with Python. It is made up of modules that you can import to get added functionality. For example, you can import the math module to gain some high level math functions. I will be cherry picking the modules I use the most as a day-to-day professional and explaining how they work. The reason I think this is a good idea is that they are common, every day modules that I think you will benefit knowing about at the beginning of your Python education. This section will also cover
Introduction 2 various ways to install 3rd party modules. Finally, I will cover how to create your own modules and packages and why you’d want to do that in the first place. Here are some of the modules we will be covering: • csv • ConfigParser • logging • os • smtplib / email • subprocess • sys • thread / queues • time / datetime |
Part three will cover intermediate odds and ends. These are topics that are handy to know, but not necessarily required to be able to program in Python. The topics covered are:
• the Python debugger (pdb) • decorators
• the lambda function • code profiling
• a testing introduction |
Part four will be made up of small tutorials that will help you to learn how to use Python in a practical way. In this way, you will learn how to create Python programs that can actually do something useful! You can take the knowledge in these tutorials to create your own scripts. Ideas for further enhancements to these mini-applications will be provided at the end of each tutorial so you will have something that you can try out on your own. Here are a few of the 3rd party packages that we’ll be covering:
• pip and easy_install • configobj • lxml • requests • virtualenv • pylint / pychecker • SQLAlchemy |
Part five is going to cover how to take your code and give it to your friends, family and the world! You will learn the following:
Introduction 3 • How to turn your reusable scripts into Python “eggs”, “wheels” and more
• How to upload your creation to the Python Package Index (PyPI)
• How to create binary executables so you can run your application without Python • How to create an installer for your application
|
The chapters and sections may not all be the same length. While every topic will be covered well, not every topic will require the same page count.
A Brief History of Python
I think it helps to know the background of the Python programming language. Python was created in the late1980s¹. Everyone agrees that its creator is Guido van Rossum when he wrote it as a successor to the ABC programming language that he was using. Guido named the language after one of his favorite comedy acts: Monty Python. The language wasn’t released until 1991 and it has grown a lot in terms of the number of included modules and packages included. At the time of this writing, there are two major versions of Python: the 2.x series and the 3.x (sometimes known as Python 3000) . The 3.x series is not backwards compatible with 2.x because the idea when creating 3.x was to get rid of some of the idiosyncrasies in the original. The current versions are 2.7.12 and 3.5.2. Most of the features in 3.x have been backported to 2.x; however, 3.x is getting the majority of Python’s current development, so it is the version of the future.
Some people think Python is just for writing little scripts to glue together “real” code, like C++ or Haskell. However you will find Python to be useful in almost any situation. Python is used by lots of big name companies such as Google, NASA, LinkedIn, Industrial Light & Magic, and many others. Python is used not only on the backend, but also on the front. In case you’re new to the computer science field, backend programming is the stuff that’s behind the scenes; things like database processing, document generation, etc. Frontend processing is the pretty stuff most users are familiar with, such as web pages or desktop user interfaces. For example, there are some really nice Python GUI toolkits such as wxPython, PySide, and Kivy. There are also several web frameworks like Django, Pyramid, and Flask. You might find it surprising to know that Django is used for Instagram and Pinterest. If you have used these or many other websites, then you have used something that’s powered by Python without even realizing it!
About the Author
You may be wondering about who I am and why I might be knowledgeable enough about Python to write about it, so I thought I’d give you a little information about myself. I started programming in Python in the Spring of 2006 for a job. My first assignment was to port Windows login scripts from
Introduction 4 Kixtart to Python. My second project was to port VBA code (basically a GUI on top of Microsoft Office products) to Python, which is how I first got started in wxPython. I’ve been using Python ever since, doing a variation of backend programming and desktop front end user interfaces. I realized that one way for me to remember how to do certain things in Python was to write about them and that’s how my Python blog came about: http://www.blog.pythonlibrary.org/. As I wrote, I would receive feedback from my readers and I ended up expanding the blog to include tips, tutorials, Python news, and Python book reviews. I work regularly with Packt Publishing as a technical reviewer, which means that I get to try to check for errors in the books before they’re published. I also have written for the Developer Zone (DZone) and i-programmer websites as well as the Python Software Foundation. In November 2013, DZone published The Essential Core Python Cheat Sheet that I co-authored.
Conventions
As with most technical books, this one includes a few conventions that you need to be aware of. New topics and terminology will be in bold. You will also see some examples that look like the following: 1 >>> myString = "Welcome to Python!"
The >>> is a Python prompt symbol. You will see this in the Python interpreter and in IDLE. You will learn more about each of these in the first chapter. Other code examples will be shown in a similar manner, but without the >>>.
Requirements
You will need a working Python 3 installation. The examples should work in either Python 2.x or 3.x unless specifically marked otherwise. Most Linux and Mac machines come with Python already installed. However, if you happen to find yourself without Python, you can go download a copy fromhttp://python.org/download/². There are up-to-date installation instructions on their website, so I won’t include any installation instructions in this book. Any additional requirements will be explained later on in the book.
Reader Feedback
I welcome feedback about my writings. If you’d like to let me know what you thought of the book, you can send comments to the following address:
[email protected] ²http://python.org/download/
Introduction 5
Errata
I try my best not to publish errors in my writings, but it happens from time to time. If you happen to see an error in this book, feel free to let me know by emailing me at the following:
Part I - Learning the Basics
In Part I, we will learn the basics of the Python programming language. This section of the book should get you ready to use all the building blocks of Python so that you will be ready to tackle the following sections confidently.
Let’s go over what we’ll be covering: • IDLE
• Strings
• Lists, Dictionaries and Tuples • Conditional statements • Loops
• Comprehensions • Exception Handling • File I/O
• Importing modules and packages • Functions
• Classes
The first chapter in this section will familiarize you with Python’s built-in development environment that is known as IDLE. The next couple of chapters will go over some of Python’s types, such as strings, lists, and dictionaries. After that we’ll look at conditional statements in Python and looping using Python’s for and while loops.
In the second half of this section, we will go into comprehensions, such as list and dictionary comprehensions. Then we’ll look at Python’s exception handling capabilities and how Python’s file operations work. Next up is learning how to import pre-made modules and packages. The last two chapters cover Python functions and classes.
Part I - Learning the Basics 7
Chapter 1 - IDLE Programming
Using IDLE
Python comes with its own code editor: IDLE. There is some unconfirmed lore that the name for IDLE comes from Eric Idle, an actor in Monty Python. I have no idea if that’s true or not, but it would make sense in this context as it appears to be a pun on the acronym IDE or Integrated Development Environment. An IDE is an editor for programmers that provides color highlighting of key words in the language, auto-complete, a debugger and lots of other fun things. You can find an IDE for most popular languages and a number of IDEs will work with multiple languages. IDLE is kind of a lite IDE, but it does have all those items mentioned. It allows the programmer to write Python and debug their code quite easily. The reason I call it “lite” is the debugger is very basic and it’s missing other features that programmers who have a background using products like Visual Studio will miss. You might also like to know that IDLE was created using Tkinter, a Python GUI toolkit that comes with Python.
To open up IDLE, you will need to find it and you’ll see something like this:
image
Yes, it’s a Python shell where you can type short scripts and see their output immediately and even interact with code in real time. There is no compiling of the code as Python is an interpretive language and runs in the Python interpreter. Let’s write your first program now. Type the following after the command prompt (>>>) in IDLE:
Chapter 1 - IDLE Programming 9 1 print("Hello from Python!")
You have just written your first program! All your program does is write a string to the screen, but you’ll find that very helpful later on. Please note that the print statement has changed in Python 3.x. In Python 2.x, you would have written the above like this:
1 print "Hello from Python!"
In Python 3, the print statement was turned into a print function, which is why parentheses are required. You will learn what functions are in chapter 10.
If you want to save your code into a file, go to the File menu and choose New Window (or press CTRL+N). Now you can type in your program and save it here. The primary benefit of using the Python shell is that you can experiment with small snippets to see how your code will behave before you put the code into a real program. The code editor screen looks a little different than the IDLE screenshot above:
image
Now we’ll spend a little time looking at IDLE’s other useful features.
Python comes with lots of modules and packages that you can import to add new features. For example, you can import the math module for all kinds of good math functions, like square roots, cosines, etcetera. In the File menu, you’ll find a Path Browser which is useful for figuring out where Python looks for module imports. You see, Python first looks in the same directory as the script that is running to see if the file it needs to import is there. Then it checks a predefined list of other locations. You can actually add and remove locations as well. The Path Browser will show you where these files are located on your hard drive, if you have imported anything. My Path Browser looks like this:
Chapter 1 - IDLE Programming 10
image
Next there’s a Class Browser that will help you navigate your code. This is actually something that won’t be very useful to you right now, but will be in the future. You’ll find it helpful when you have lots of lines of code in a single file as it will give you a “tree-like” interface for your code. Note that you won’t be able to load the Class Browser unless you have actually saved your program.
The Edit menu has your typical features, such as Copy, Cut, Paste, Undo, Redo and Select All. It also contains various ways to search your code and do a search and replace. Finally, the Edit menu has some menu items that will Show you various things, such as highlighting parentheses or displaying the auto-complete list.
The Format menu has lots of useful functionality. It has some helpful items for indenting and dedenting your code, as well as commenting out your code. I find that pretty helpful when I’m testing my code. Commenting out your code can be very helpful. One way it can be helpful is when you have a lot of code and you need to find out why it’s not working correctly. Commenting out portions of it and re-running the script can help you figure out where you went wrong. You just go along slowly uncommenting out stuff until you hit your bug. Which reminds me; you may have noticed that the main IDLE screen has a Debugger menu. That is nice for debugging, but only in the Shell window. Sadly you cannot use the debugger in your main editing menu. If you need a more versatile debugger, you should either find a different IDE or try Python’s debugger found in the pdb library.
Chapter 1 - IDLE Programming 11 A comment is a way to leave un-runnable code that documents what you are doing in your code. Every programming language uses a different symbol to demarcate where a comment starts and ends. What do comments look like in Python though? A comment is anything that begins with an octothorpe (i.e. a hash or pound sign). The following is an example of some comments in action:
1 # This is a comment before some code 2 print("Hello from Python!")
3 print("Winter is coming") # this is an in-line comment
You can write comments on a line all by themselves or following a statement, like the second print statement above. The Python interpreter ignores comments, so you can write anything you want in them. Most programmers I have met don’t use comments very much. However, I highly recommend using comments liberally not just for yourself, but for anyone else who might have to maintain or enhance your code in the future. I have found my own comments useful when I come back to a script that I wrote 6 months ago and I have found myself working with code that didn’t have comments and wishing that it did so I could figure it out faster.
Examples of good comments would include explanations about complex code state-ments, or adding an explanation for acronyms in your code. Sometimes you’ll need to leave a comment to explain why you did something a certain way because it’s just not obvious.
Now we need to get back to going over the menu options of IDLE:
The Run menu has a couple of handy options. You can use it to bring up the Python Shell, check your code for errors, or run your code. The Options menu doesn’t have very many items. It does have a Configure option that allows you to change the code highlighting colors, fonts and key shortcuts. Other than that, you get a Code Context option that is helpful in that it puts an overlay in the editing window which will show you which class or function you’re currently in. We will be explaining functions and classes near the end of Part I. You will find this feature is useful whenever you have a lot of code in a function and the name has scrolled off the top of the screen. With this option enabled, that doesn’t happen. Of course, if the function is too large to fit on one screen, then it may be getting too long and it could be time to break that function down into multiple functions. The Windows menu shows you a list of currently open Windows and allows you to switch between them.
Last but not least is the Help menu where you can learn about IDLE, get help with IDLE itself or load up a local copy of the Python documentation. The documentation will explain how each piece of Python works and is pretty exhaustive in its coverage. The Help menu is probably the most helpful in that you can get access to the docs even when you’re not connected to the internet. You can search
Chapter 1 - IDLE Programming 12 the documentation, find HOWTOs, read about any of the builtin libraries, and learn so much your head will probably start spinning.
Other Tips
When you see code examples in the following chapters, you can write and run them in IDLE. I wrote all my programs in IDLE for the first couple of years of my Python programming life and I was pretty happy with it. There are lots of free Python IDEs out there though and several IDEs that you have to pay for. If you want to go cheap, you might want to take a look at Eclipse+PyDev, Editra or even Notepad++. For a paid IDE, I would recommend WingWare’s IDE or possibly PyCharm. They have many more features such as integration with code repositories, better debuggers, refactoring help, etc.
In this book, we will be using IDLE in our examples because it comes with Python and will provide a common test bed. I still think IDLE has the best, most consistent code highlighting of any IDE I have used. Code highlighting is important in my mind in that it helps prevent me from using one of Python’s keywords (or built-ins) for a variable name. In case you’re wondering, here is a list of those key words:
1 and del from not while
2 as elif global or with
3 assert else if pass yield
4 break except import print
5 class exec in raise
6 continue finally is return
7 def for lambda try
Let’s see what happens as we type out a few things in Python:
Chapter 1 - IDLE Programming 13 As you can see, IDLE color coded everything. A key word is magenta, a string of text is in green, a comment is in red and the output from the print function is in blue.
Wrapping Up
In this chapter we learned how to use Python’s integrated development environment, IDLE. We also learned what comments are and how to use them. At this point, you should be familiar enough with IDLE to use it in the rest of this book. There are many other integrated development environments (IDEs) for Python. There are free ones like PyDev and Editra, and there are some others that you have to pay for, such as WingWare and PyCharm. There are also plug-ins for regular text editors that allow you to code in Python too. I think IDLE is a good place to start, but if you already have a favorite editor, feel free to continue using that.
At this point, we are ready to move on and start learning about Python’s various data types. We will start with Strings in the following chapter.
Chapter 2 - All About Strings
There are several data types in Python. The main data types that you’ll probably see the most are string, integer, float, list, dict and tuple. In this chapter, we’ll cover the string data type. You’ll be surprised how many things you can do with strings in Python right out of the box. There’s also a string module that you can import to access even more functionality, but we won’t be looking at that in this chapter. Instead, we will be covering the following topics:
• How to create strings • String concatenation • String methods • String slicing • String substitution
How to Create a String
Strings are usually created in one of three ways. You can use single, double or triple quotes. Let’s take a look!
1 >>> my_string = "Welcome to Python!"
2 >>> another_string = 'The bright red fox jumped the fence.' 3 >>> a_long_string = '''This is a
4 multi-line string. It covers more than 5 one line'''
The triple quoted line can be done with three single quotes or three double quotes. Either way, they allow the programmer to write strings over multiple lines. If you print it out, you will notice that the output retains the line breaks. If you need to use single quotes in your string, then wrap it in double quotes. See the following example.
1 >>> my_string = "I'm a Python programmer!"
2 >>> otherString = 'The word "python" usually refers to a snake'
3 >>> tripleString = """Here's another way to embed "quotes" in a string"""
The code above demonstrates how you could put single quotes or double quotes into a string. There’s actually one other way to create a string and that is by using the str method. Here’s how it works:
Chapter 2 - All About Strings 15 1 >>> my_number = 123
2 >>> my_string = str(my_number)
If you type the code above into your interpreter, you’ll find that you have transformed the integer value into a string and assigned the string to the variable my_string. This is known as casting. You can cast some data types into other data types, like numbers into strings. But you’ll also find that you can’t always do the reverse, such as casting a string like ‘ABC’ into an integer. If you do that, you’ll end up with an error like the one in the following example:
1 >>> int('ABC')
2 Traceback (most recent call last): 3 File "<string>", line 1, in <fragment>
4 ValueError: invalid literal for int() with base 10: 'ABC'
We will look at exception handling in a later chapter, but as you may have guessed from the message, this means that you cannot convert a literal into an integer. However, if you had done
1 >>> x = int("123")
then that would have worked fine.
It should be noted that a string is one of Python immutable types. What this means is that you cannot change a string’s content after creation. Let’s try to change one to see what happens: 1 >>> my_string = "abc"
2 >>> my_string[0] = "d"
3 Traceback (most recent call last): 4 File "<string>", line 1, in <fragment>
5 TypeError: 'str' object does not support item assignment
Here we try to change the first character from an “a” to a “d”; however this raises a TypeError that stops us from doing so. Now you may think that by assigning a new string to the same variable that you’ve changed the string. Let’s see if that’s true:
Chapter 2 - All About Strings 16 1 >>> my_string = "abc" 2 >>> id(my_string) 3 19397208 4 >>> my_string = "def" 5 >>> id(my_string) 6 25558288
7 >>> my_string = my_string + "ghi" 8 >>> id(my_string)
9 31345312
By checking the id of the object, we can determine that any time we assign a new value to the variable, its identity changes.
Note that in Python 2.x, strings can only contain ASCII characters. If you require unicode in Python 2.x, then you will need to precede your string with a u. Here’s an example:
1 my_unicode_string = u"This is unicode!"
The example above doesn’t actually contain any unicode, but it should give you the general idea. In Python 3.x, all strings are unicode.
String Concatenation
Concatenation is a big word that means to combine or add two things together. In this case, we want to know how to add two strings together. As you might suspect, this operation is very easy in Python:
1 >>> string_one = "My dog ate " 2 >>> string_two = "my homework!"
3 >>> string_three = string_one + string_two The ‘+’ operator concatenates the two strings into one.
String Methods
A string is an object in Python. In fact, everything in Python is an object. However, you’re not really ready for that. If you want to know more about how Python is an object oriented programming language, then you’ll need to skip to that chapter. In the meantime, it’s enough to know that strings have their very own methods built into them. For example, let’s say you have the following string:
Chapter 2 - All About Strings 17 1 >>> my_string = "This is a string!"
Now you want to cause this string to be entirely in uppercase. To do that, all you need to do is call its upper() method, like this:
1 >>> my_string.upper()
If you have your interpreter open, you can also do the same thing like this: 1 >>> "This is a string!".upper()
There are many other string methods. For example, if you wanted everything to be lowercase, you would use the lower() method. If you wanted to remove all the leading and trailing white space, you would use strip(). To get a list of all the string methods, type the following command into your interpreter:
1 >>> dir(my_string)
You should end up seeing something like the following:
[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__getnewargs__’, ‘__getslice__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mod__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__rmod__’, ‘__rmul__’, ‘__-setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘_formatter_field_name_split’, ‘_formatter_parser’, ‘capitalize’, ‘center’, ‘count’, ‘decode’, ‘encode’, ‘endswith’, ‘ex-pandtabs’, ‘find’, ‘format’, ‘index’, ‘isalnum’, ‘isalpha’, ‘isdigit’, ‘islower’, ‘isspace’, ‘istitle’, ‘isupper’, ‘join’, ‘ljust’, ‘lower’, ‘lstrip’, ‘partition’, ‘replace’, ‘rfind’, ‘rindex’, ‘rjust’, ‘rpartition’, ‘rsplit’, ‘rstrip’, ‘split’, ‘splitlines’, ‘startswith’, ‘strip’, ‘swapcase’, ‘title’, ‘translate’, ‘upper’, ‘zfill’]
You can safely ignore the methods that begin and end with double-underscores, such as __add__. They are not used in every day Python coding. Focus on the other ones instead. If you’d like to know what one of them does, just ask for help. For example, say you want to learn what capitalize is for. To find out, you would type
1 >>> help(my_string.capitalize)
Chapter 2 - All About Strings 18 Help on built-in function capitalize:
capitalize(…)
S.capitalize() -> string
Return a copy of the string S with only its first character capitalized.
You have just learned a little bit about a topic called introspection. Python allows easy introspection of all its objects, which makes it very easy to use. Basically, introspection allows you to ask Python about itself. In an earlier section, you learned about casting. You may have wondered how to tell what type the variable was (i.e. an int or a string). You can ask Python to tell you that!
1 >>> type(my_string) 2 <type 'str'>
As you can see, the my_string variable is of type str!
String Slicing
One subject that you’ll find yourself doing a lot of in the real world is string slicing. I have been surprised how often I have needed to know how to do this in my day-to-day job. Let’s take a look at how slicing works with the following string:
1 >>> my_string = "I like Python!"
Each character in a string can be accessed using slicing. For example, if I want to grab just the first character, I could do this:
1 >>> my_string[0:1]
This grabs the first character in the string up to, but not including, the 2nd character. Yes, Python is zero-based. It’s a little easier to understand if we map out each character’s position in a table: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 - - - – – – – I l i k e P y t h o n !
Thus we have a string that is 14 characters long, starting at zero and going through thirteen. Let’s do a few more examples to get these concepts into our heads better.
Chapter 2 - All About Strings 19 1 >>> my_string[:1] 2 'I' 3 >>> my_string[0:12] 4 'I like Pytho' 5 >>> my_string[0:13] 6 'I like Python' 7 >>> my_string[0:14] 8 'I like Python!' 9 >>> my_string[0:-5] 10 'I like Py' 11 >>> my_string[:] 12 'I like Python!' 13 >>> my_string[2:] 14 'like Python!'
As you can see from these examples, we can do a slice by just specifying the beginning of the slice (i.e. my_string[2:]), the ending of the slice (i.e. my_string[:1]) or both (i.e. my_string[0:13]). We can even use negative values that start at the end of the string. So the example where we did my_string[0:-5] starts at zero, but ends 5 characters before the end of the string.
You may be wondering where you would use this. I find myself using it for parsing fixed width records in files or occasionally for parsing complicated file names that follow a very specific naming convention. I have also used it in parsing out values from binary-type files. Any job where you need to do text file processing will be made easier if you understand slicing and how to use it effectively. You can also access individual characters in a string via indexing. Here is an example:
1 >>> print(my_string[0])
The code above will print out the first character in the string.
String Formatting
String formatting (AKA substitution) is the topic of substituting values into a base string. Most of the time, you will be inserting strings within strings; however you will also find yourself inserting integers and floats into strings quite often as well. There are two different ways to accomplish this task. We’ll start with the old way of doing things and then move on to the new.
Ye Olde Way of Substituting Strings
Chapter 2 - All About Strings 20 1 >>> my_string = "I like %s" % "Python"
2 >>> my_string 3 'I like Python' 4 >>> var = "cookies"
5 >>> newString = "I like %s" % var 6 >>> newString
7 'I like cookies'
8 >>> another_string = "I like %s and %s" % ("Python", var) 9 >>> another_string
10 'I like Python and cookies'
As you’ve probably guessed, the %s is the important piece in the code above. It tells Python that you may be inserting text soon. If you follow the string with a percent sign and another string or variable, then Python will attempt to insert it into the string. You can insert multiple strings by putting multiple instances of %s inside your string. You’ll see that in the last example. Just note that when you insert more than one string, you have to enclose the strings that you’re going to insert with parentheses.
Now let’s see what happens if we don’t insert enough strings: 1 >>> another_string = "I like %s and %s" % "Python" 2 Traceback (most recent call last):
3 File "<string>", line 1, in <fragment>
4 TypeError: not enough arguments for format string
Oops! We didn’t pass enough arguments to format the string! If you look carefully at the example above, you’ll notice it has two instances of %s, so to insert strings into it, you have to pass it the same number of strings! Now we’re ready to learn about inserting integers and floats. Let’s take a look!
1 >>> my_string = "%i + %i = %i" % (1,2,3) 2 >>> my_string 3 '1 + 2 = 3' 4 >>> float_string = "%f" % (1.23) 5 >>> float_string 6 '1.230000' 7 >>> float_string2 = "%.2f" % (1.23) 8 >>> float_string2 9 '1.23' 10 >>> float_string3 = "%.2f" % (1.237) 11 >>> float_string3 12 '1.24'
Chapter 2 - All About Strings 21 The first example above is pretty obvious. We create a string that accept three arguments and we pass them in. Just in case you hadn’t figured it out yet, no, Python isn’t actually doing any addition in that first example. For the second example, we pass in a float. Note that the output includes a lot of extra zeroes. We don’t want that, so we tell Python to limit it to two decimal places in the 3rd example (“%.2f”). The last example shows you that Python will do some rounding for you if you pass it a float that’s greater than two decimal places.
Now let’s see what happens if we pass it bad data: 1 >>> int_float_err = "%i + %f" % ("1", "2.00") 2 Traceback (most recent call last):
3 File "<string>", line 1, in <fragment>
4 TypeError: %d format: a number is required, not str
In this example, we pass it two strings instead of an integer and a float. This raises a TypeError and tells us that Python was expecting a number. This refers to not passing an integer, so let’s fix that and see if that fixes the issue:
1 >>> int_float_err = "%i + %f" % (1, "2.00") 2 Traceback (most recent call last):
3 File "<string>", line 1, in <fragment> 4 TypeError: float argument required, not str
Nope. We get the same error, but a different message that tells us we should have passed a float. As you can see, Python gives us pretty good information about what went wrong and how to fix it. If you fix the inputs appropriately, then you should be able to get this example to run.
Let’s move on to the new method of string formatting! Templates and the New String Formatting Methodology
This new method was actually added back in Python 2.4 as string templates, but was added as a regular string method via the format method in Python 2.6. So it’s not really a new method, just newer. Anyway, let’s start with templates!
1 >>> print("%(lang)s is fun!" % {"lang":"Python"}) 2 Python is fun!
This probably looks pretty weird, but basically we just changed our %s into %(lang)s, which is basically the %s with a variable inside it. The second part is actually called a Python dictionary that we will be studying in the next section. Basically it’s a key:value pair, so when Python sees the key “lang” in the string AND in the key of the dictionary that is passed in, it replaces that key with its value. Let’s look at some more samples:
Chapter 2 - All About Strings 22 1 >>> print("%(value)s %(value)s %(value)s !" % {"value":"SPAM"})
2 SPAM SPAM SPAM !
3 >>> print("%(x)i + %(y)i = %(z)i" % {"x":1, "y":2}) 4 Traceback (most recent call last):
5 File "<string>", line 1, in <fragment> 6 KeyError: 'z'
7 >>> print("%(x)i + %(y)i = %(z)i" % {"x":1, "y":2, "z":3}) 8 1 + 2 = 3
In the first example, you’ll notice that we only passed in one value, but it was inserted 3 times! This is one of the advantages of using templates. The second example has an issue in that we forgot to pass in a key, namely the “z” key. The third example rectifies this issue and shows the result. Now let’s look at how we can do something similar with the string’s format method!
1 >>> "Python is as simple as {0}, {1}, {2}".format("a", "b", "c") 2 'Python is as simple as a, b, c'
3 >>> "Python is as simple as {1}, {0}, {2}".format("a", "b", "c") 4 'Python is as simple as b, a, c'
5 >>> xy = {"x":0, "y":10}
6 >>> print("Graph a point at where x={x} and y={y}".format(**xy)) 7 Graph a point at where x=0 and y=10
In the first two examples, you can see how we can pass items positionally. If we rearrange the order, we get a slightly different output. The last example uses a dictionary like we were using in the templates above. However, we have to extract the dictionary using the double asterisk to get it to work correctly here.
There are lots of other things you can do with strings, such as specifying a width, aligning the text, converting to different bases and much more. Be sure to take a look at some of the references below for more information.
• Python’s official documentation on the str type³ • String Formatting⁴
• More on String Formatting⁵
• Python 2.x documentation onunicode⁶ ³https://docs.python.org/3/library/functions.html#func-str
⁴https://docs.python.org/3/library/string.html#string-formatting ⁵https://docs.python.org/3/library/string.html#formatexamples ⁶http://docs.python.org/2/library/functions.html#unicode
Chapter 2 - All About Strings 23
Wrapping Up
We have covered a lot in this chapter. Let’s review:
First we learned how to create strings themselves, then we moved on to the topic of string concatenation. After that we looked at some of the methods that the string object gives us. Next we looked at string slicing and we finished up by learning about string substitution.
In the next chapter, we will look at three more of Python’s built-in data types: lists, tuples and dictionaries. Let’s get to it!
Chapter 3 - Lists, Tuples and
Dictionaries
Python has several other important data types that you’ll probably use every day. They are called lists, tuples and dictionaries. This chapter’s aim is to get you acquainted with each of these data types. They are not particularly complicated, so I expect that you will find learning how to use them very straight forward. Once you have mastered these three data types plus the string data type from the previous chapter, you will be quite a ways along in your education of Python. You’ll be using these four building blocks in 99% of all the applications you will write.
Lists
A Python list is similar to an array in other languages. In Python, an empty list can be created in the following ways.
1 >>> my_list = [] 2 >>> my_list = list()
As you can see, you can create the list using square brackets or by using the Python built-in, list. A list contains a list of elements, such as strings, integers, objects or a mixture of types. Let’s take a look at some examples:
1 >>> my_list = [1, 2, 3]
2 >>> my_list2 = ["a", "b", "c"] 3 >>> my_list3 = ["a", 1, "Python", 5]
The first list has 3 integers, the second has 3 strings and the third has a mixture. You can also create lists of lists like this:
1 >>> my_nested_list = [my_list, my_list2] 2 >>> my_nested_list
3 [[1, 2, 3], ['a', 'b', 'c']]
Chapter 3 - Lists, Tuples and Dictionaries 25 1 >>> combo_list = [] 2 >>> one_list = [4, 5] 3 >>> combo_list.extend(one_list) 4 >>> combo_list 5 [4, 5]
A slightly easier way is to just add two lists together. 1 >>> my_list = [1, 2, 3]
2 >>> my_list2 = ["a", "b", "c"] 3 >>> combo_list = my_list + my_list2 4 >>> combo_list
5 [1, 2, 3, 'a', 'b', 'c']
Yes, it really is that easy. You can also sort a list. Let’s spend a moment to see how to do that: 1 >>> alpha_list = [34, 23, 67, 100, 88, 2]
2 >>> alpha_list.sort() 3 >>> alpha_list
4 [2, 23, 34, 67, 88, 100]
Now there is a got-cha above. Can you see it? Let’s do one more example to make it obvious: 1 >>> alpha_list = [34, 23, 67, 100, 88, 2]
2 >>> sorted_list = alpha_list.sort() 3 >>> sorted_list
4 >>> print(sorted_list) 5 None
In this example, we try to assign the sorted list to a variable. However, when you call the sort() method on a list, it sorts the list in-place. So if you try to assign the result to another variable, then you’ll find out that you’ll get a None object, which is like a Null in other languages. Thus when you want to sort something, just remember that you sort them in-place and you cannot assign it to a different variable.
You can slice a list just like you do with a string: 1 >>> alpha_list[0:3]
2 [2, 23, 34]
Chapter 3 - Lists, Tuples and Dictionaries 26
Tuples
A tuple is similar to a list, but you create them with parentheses instead of square brackets. You can also use the tuple built-in. The main difference is that a tuple is immutable while the list is mutable. Let’s take a look at a few examples:
1 >>> my_tuple = (1, 2, 3, 4, 5) 2 >>> my_tuple[0:3]
3 (1, 2, 3)
4 >>> another_tuple = tuple() 5 >>> abc = tuple([1, 2, 3])
The code above demonstrates one way to create a tuple with five elements. It also shows that you can do tuple slicing. However, you cannot sort a tuple! The last two examples shows how to create tuples using the tuple keyword. The first one just creates an empty tuple whereas the second example has three elements inside it. Notice that it has a list inside it. This is an example of casting. We can change or cast an item from one data type to another. In this case, we cast a list into a tuple. If you want to turn the abc tuple back into a list, you can do the following:
1 >>> abc_list = list(abc)
To reiterate, the code above casts the tuple (abc) into a list using the list function.
Dictionaries
A Python dictionary is basically a hash table or a hash mapping. In some languages, they might be referred to as associative memories or associative arrays. They are indexed with keys, which can be any immutable type. For example, a string or number can be a key. You need to be aware that a dictionary is an unordered set of key:value pairs and the keys must be unique. You can get a list of keys by calling a dictionary instance’s keys method. To check if a dictionary has a key, you can use Python’s in keyword. In some of the older versions of Python (2.3 and older to be specific), you will see the has_key keyword used for testing if a key is in a dictionary. This keyword is deprecated in Python 2.x and removed entirely from Python 3.x.
Chapter 3 - Lists, Tuples and Dictionaries 27 1 >>> my_dict = {}
2 >>> another_dict = dict()
3 >>> my_other_dict = {"one":1, "two":2, "three":3} 4 >>> my_other_dict
5 {'three': 3, 'two': 2, 'one': 1}
The first two examples show how to create an empty dictionary. All dictionaries are enclosed with curly braces. The last line is printed out so you can see how unordered a dictionary is. Now it’s time to find out how to access a value in a dictionary.
1 >>> my_other_dict["one"] 2 1
3 >>> my_dict = {"name":"Mike", "address":"123 Happy Way"} 4 >>> my_dict["name"]
5 'Mike'
In the first example, we use the dictionary from the previous example and pull out the value associated with the key called “one”. The second example shows how to acquire the value for the “name” key. Now let’s see how to tell if a key is in a dictionary or not:
1 >>> "name" in my_dict 2 True
3 >>> "state" in my_dict 4 False
So, if the key is in the dictionary, Python returns a Boolean True. Otherwise it returns a Boolean False. If you need to get a listing of all the keys in a dictionary, then you do this:
1 >>> my_dict.keys()
2 dict_keys(['name', 'address'])
In Python 2, the keys method returns a list. But in Python 3, it returns a view object. This gives the developer the ability to update the dictionary and the view will automatically update too. Also note that when using the in keyword for dictionary membership testing, it is better to do it against the dictionary instead of the list returned from the keys method. See below:
1 >>> "name" in my_dict # this is good
2 >>> "name" in my_dict.keys() # this works too, but is slower
While this probably won’t matter much to you right now, in a real job situation, seconds matter. When you have thousands of files to process, these little tricks can save you a lot of time in the long run!
Chapter 3 - Lists, Tuples and Dictionaries 28
Wrapping Up
In this chapter you just learned how to construct a Python list, tuple and dictionary. Make sure you understand everything in this section before moving on. These concepts will assist you in designing your programs. You will be building complex data structures using these building blocks every day if you choose to pursue employment as a Python programmer. Each of these data types can be nested inside the others. For example, you can have a nested dictionary, a dictionary of tuples, a tuple made up of several dictionaries, and on and on.
Chapter 4 - Conditional Statements
Every computer language I have ever used has had at least one conditional statement. Most of the time that statement is the if/elif/else structure. This is what Python has. Other languages also include the case/switch statement which I personally enjoy, however Python does not include it. You can make your own if you really want to, but this book is focused on learning Python fundamentals, so we’re going to be only focusing on what’s included with Python in this chapter.
The conditional statement checks to see if a statement is True or False. That’s really all it does. However we will also be looking at the following Boolean operations: and, or, and not. These operations can change the behaviour of the conditional in simple and complex ways, depending on your project.
The if statement
Python’s if statement is pretty easy to use. Let’s spend a few minutes looking at some examples to better acquaint ourselves with this construct.
1 >>> if 2 > 1:
2 print("This is a True statement!") 3 This is a True Statement!
This conditional tests the “truthfulness” of the following statement: 2 > 1. Since this statement evaluates to True, it will cause the last line in the example to print to the screen or standard out (stdout).
Python Cares About Space
The Python language cares a lot about space. You will notice that in our conditional statement above, we indented the code inside the if statement four spaces. This is very important! If you do not indent your blocks of code properly, the code will not execute properly. It may not even run at all.
Also, do not mix tabs and spaces. IDLE will complain that there is an issue with your file and you will have trouble figuring out what the issue is. The recommended number of spaces to indent a block of code is four. You can actually indent your code any number of spaces as long as you are consistent. However, the 4-space rule is one that is recommended by the Python Style Guide and is the rule that is followed by the Python code developers.
Chapter 4 - Conditional Statements 30
Let’s look at another example: 1 >>> var1 = 1
2 >>> var2 = 3
3 >>> if var1 > var2:
4 print("This is also True")
In this one, we compare two variables that translate to the question: Is 1 > 3? Obviously one is not greater than three, so it doesn’t print anything. But what is we wanted it to print something? That’s where the else statement comes in. Let’s modify the conditional to add that piece:
1 if var1 > var2:
2 print("This is also True") 3 else:
4 print("That was False!")
If you run this code, it will print the string that follows the else statement. Let’s change gears here and get some information from the user to make this more interesting. In Python 2.x, you can get information using a built-in called raw_input. If you are using Python 3.x, then you’ll find that raw_input no longer exists. It was renamed to just input. They function in the same way though. To confuse matters, Python 2.x actually has a built-in called input as well; however it tries to execute what is entered as a Python expression whereas raw_input returns a string. Anyway, we’ll be using Python 2.x’s raw_input for this example to get the user’s age.
1 # Python 2.x code
2 value = raw_input("How much is that doggy in the window? ") 3 value = int(value)
4
5 if value < 10:
6 print("That's a great deal!") 7 elif 10 <= value <= 20:
8 print("I'd still pay that...") 9 else:
10 print("Wow! That's too much!") |
Let’s break this down a bit. The first line asks the user for an amount. In the next line, it converts the user’s input into an integer. So if you happen to type a floating point number like 1.23, it will get truncated to just 1. If you happen to type something other than a number, then you’ll receive an
Chapter 4 - Conditional Statements 31 exception. We’ll be looking at how to handle exceptions in a later chapter, so for now, just enter an integer.
In the next few lines, you can see how we check for 3 different cases: less than 10, greater than or equal to 10 but less than or equal to 20 or something else. For each of these cases, a different string is printed out. Try putting this code into IDLE and save it. Then run it a few times with different inputs to see how it works.
You can add multiple elif statements to your entire conditional. The else is optional, but makes a good default.
Boolean Operations
Now we’re ready to learn about Boolean operations (and, or, not). According to the Python documentation, their order of priority is first or, then and, then not. Here’s how they work:
• or means that if any conditional that is “ored” together is True, then the following statement runs
• and means that all statements must be True for the following statement to run • not means that if the conditional evaluates to False, it is True. This is the most
confusing, in my opinion.
Let’s take a look at some examples of each of these. We will start with or. 1 x = 10
2 y = 20 3
4 if x < 10 or y > 15:
5 print("This statement was True!")
Here we create a couple of variables and test if one is less than ten or if the other is greater than 15. Because the latter is greater than 15, the print statement executes. As you can see, if one or both of the statements are True, it will execute the statement. Let’s take a look at how and works:
1 x = 10 2 y = 10
3 if x == 10 and y == 15:
4 print("This statement was True") 5 else:
Chapter 4 - Conditional Statements 32 If you run the code above, you will see that first statement does not get run. Instead, the statement under the else is executed. Why is that? Well, it is because what we are testing is both x and y are 10 and 15 respectively. In this case, they are not, so we drop to the else. Thus, when you and two statements together, both statements have to evaluate to True for it to execute the following code. Also note that to test equality in Python, you have to use a double equal sign. A single equals sign is known as the assignment operator and is only for assigning a value to a variable. If you had tried to run the code above with one of those statement only having one equals sign, you would have received a message about invalid syntax.
Note that you can also or and and more than two statements together. However, I do not recommend that as the more statements that you do that too, the harder it can be to understand and debug. Now we’re ready to take a look at the not operation.
1 my_list = [1, 2, 3, 4] 2 x = 10
3 if x not in my_list:
4 print("'x' is not in the list, so this is True!")
In this example, we create a list that contains four integers. Then we write a test that asks if “x” is not in that list. Because “x” equals 10, the statement evaluates to True and the message is printed to the screen. Another way to test for not is by using the exclamation point, like this:
1 x = 10 2 if x != 11:
3 print("x is not equal to 11!")
If you want to, you can combine the not operation with the other two to create more complex conditional statements. Here is a simple example:
1 my_list = [1, 2, 3, 4] 2 x = 10
3 z = 11
4 if x not in my_list and z != 10: 5 print("This is True!")
Checking for Nothing
Because we are talking about statements that evaluate to True, we probably need to cover what evaluates to False. Python has the keyword False which I’ve mentioned a few times. However an empty string, tuple or list also evaluates to False. There is also another keyword that basically evaluates to False which is called None. The None value is used to represent the absence of value. It’s kind of analogous to Null, which you find in databases. Let’s take a look at some code to help us better understand how this all works:
Chapter 4 - Conditional Statements 33 1 empty_list = [] 2 empty_tuple = () 3 empty_string = "" 4 nothing = None 5 6 if empty_list == []:
7 print("It's an empty list!") 8
9 if empty_tuple:
10 print("It's not an empty tuple!") 11
12 if not empty_string:
13 print("This is an empty string!") 14
15 if not nothing:
16 print("Then it's nothing!")
The first four lines set up four variables. Next we create four conditionals to test them. The first one checks to see if the empty_list is really empty. The second conditional checks to see if the empty_tuple has something in it. Yes, you read that right, The second conditional only evaluates to True if the tuple is not empty! The last two conditionals are doing the opposite of the second. The third is checking if the string is empty and the fourth is checking if the nothing variable is really None.
The not operator means that we are checking for the opposite meaning. In other words, we are checking if the value is NOT True. So in the third example, we check if the empty string is REALLY empty. Here’s another way to write the same thing:
1 if empty_string == "":
2 print("This is an empty string!")
To really nail this down, let’s set the empty_string variable to actually contain something: 1 >>> empty_string = "something"
2 >>> if empty_string == "":
3 print("This is an empty string!")
If you run this, you will find that nothing is printed as we will only print something if the variable is an empty string.
Please note that none of these variables equals the other. They just evaluate the same way. To prove this, we’ll take a look at a couple of quick examples:
Chapter 4 - Conditional Statements 34 1 >>> empty_list == empty_string
2 False
3 >>> empty_string == nothing 4 False
As you can see, they do not equal each other. You will find yourself checking your data structures for data a lot in the real world. Some programmers actually like to just wrap their structures in an exception handler and if they happen to be empty, they’ll catch the exception. Others like to use the strategy mentioned above where you actually test the data structure to see if it has something in it. Both strategies are valid.
Personally, I find the not operator a little confusing and don’t use it that much. But you will find it useful from time to time.
Special Characters
Strings can contain special characters, like tabs or new lines. We need to be aware of those as they can sometimes crop up and cause problems. For example, the new line character is defined as “n”, while the tab character is defined as “t”. Let’s see a couple of examples so you will better understand what these do:
1 >>> print("I have a \n new line in the middle") 2 I have a
3 new line in the middle
4 >>> print("This sentence is \ttabbed!") 5 This sentence is tabbed!
Was the output as you expected? In the first example, we have a “n” in the middle of the sentence, which forces it to print out a new line. Because we have a space after the new line character, the second line is indented by a space. The second example shows what happens when we have a tab character inside of a sentence.
Sometimes you will want to use escape characters in a string, such as a backslash. To use escape characters, you have to actually use a backslash, so in the case of a backslash, you would actually type two backslashes. Let’s take a look:
Chapter 4 - Conditional Statements 35 1 >>> print("This is a backslash \")
2 Traceback (most recent call last): 3 File "<string>", line 1, in <fragment>
4 EOL while scanning string literal: <string>, line 1, pos 30 5 >>> print("This is a backslash \\")
6 This is a backslash \
You will notice that the first example didn’t work so well. Python thought we were escaping the double-quote, so it couldn’t tell where the end of the line (EOL) was and it threw an error. The second example has the backslash appropriately escaped.
if __name__ == “__main__”
You will see a very common conditional statement used in many Python examples. This is what it looks like:
1 if __name__ == "__main__": 2 # do something!
You will see this at the end of a file. This tells Python that you only want to run the following code if this program is executed as a standalone file. I use this construct a lot to test that my code works in the way I expect it to. We will be discussing this later in the book, but whenever you create a Python script, you create a Python module. If you write it well, you might want to import it into another module. When you do import a module, it will not run the code that’s under the conditional because __name__ will no longer equal “__main__“. We will look at this again in Chapter 11 when we talk about classes.
Wrapping Up
We’ve covered a fair bit of ground in this chapter. You have learned how to use conditional statements in several different ways. We have also spent some time getting acquainted with Boolean operators. As you have probably guessed, each of these chapters will get slightly more complex as we’ll be using each building block that we learn to build more complicated pieces of code. In the next chapter, we will continue that tradition by learning about Python’s support of loops!