0% found this document useful (0 votes)
13 views1 page

Python Crash Course - 026

Uploaded by

darkflux514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Python Crash Course - 026

Uploaded by

darkflux514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

The output shows that we do indeed have a string containing pi to

1,000,000 decimal places:

3.14159265358979323846264338327950288419716939937510...
1000002

Python has no inherent limit to how much data you can work with; you
can work with as much data as your system’s memory can handle.

NOTE To run this program (and many of the examples that follow), you’ll need to download
the resources available at https://ehmatthes.github.io/pcc_3e.

Is Your Birthday Contained in Pi?


I’ve always been curious to know if my birthday appears anywhere in the
digits of pi. Let’s use the program we just wrote to find out if someone’s
birthday appears anywhere in the first million digits of pi. We can do this
by expressing each birthday as a string of digits and seeing if that string
appears anywhere in pi_string:

pi_birthday.py --snip--
for line in lines:
pi_string += line.strip()

birthday = input("Enter your birthday, in the form mmddyy: ")


if birthday in pi_string:
print("Your birthday appears in the first million digits of pi!")
else:
print("Your birthday does not appear in the first million digits of pi.")

We first prompt for the user’s birthday, and then check if that string is
in pi_string. Let’s try it:

Enter your birthdate, in the form mmddyy: 120372


Your birthday appears in the first million digits of pi!

My birthday does appear in the digits of pi! Once you’ve read from a
file, you can analyze its contents in just about any way you can imagine.

TRY IT YOURSELF

10-1. Learning Python: Open a blank file in your text editor and write a few lines
summarizing what you’ve learned about Python so far. Start each line with the
phrase In Python you can. . . . Save the file as learning_python.txt in the same
directory as your exercises from this chapter. Write a program that reads the file
and prints what you wrote two times: print the contents once by reading in the
entire file, and once by storing the lines in a list and then looping over each line.
(continued)

Files and Exceptions 189

You might also like