Recent Comments

Python

Python is a scripting language that is unique from other languages in that it uses indentation of the code to form the if-then-else blocks. Other languages will use an explicit start and stop construction, so this shorthand makes the code feel less cluttered. Like Perl, it is supported on all computing platforms (Windows, Unix, Linux, Mac, and mobile devices) and executes immediately without the need for a compile step. The simplicity and self-documenting nature of the Python language make it a favorite language for script writers.

Additional libraries can be called from a Python program which gives it access to many pre-built objects such as network connections, databases and graphics.

Download software: http://www.python.org/

Cost: Free

Instructions to setup software: For Windows, just run the MSI installer available at python.org/download. For Mac OS X, it is already installed on your computer.

Hardware requirements: Any PC or laptop computer

Operating systems supported: Win, Mac OS X, Linux

Difficulty level: 2 out of 5


TRY IT!

1. Install the Python compiler and tools on your computer or laptop. This is available in a single download file from python.org. For Mac OS X and some Linux computers, Python is already installed.

2. Open a text editor (notepad, textedit, or your favorite) and copy/paste the following program into a file. If you installed Python into a Windows computer, it included a program called IDLE, which can edit and run Python programs:


#!/usr/bin/env python3

#...initialize looping variable, assume 'yes' as the first answer
continueYN = "y"

while continueYN == "y":
   #...get temperature input from the user
   sDegreeF = input("Enter next temperature in degrees Fahrenheit (F):";)

   #...convert text entry to number value that can be used in equations
   nDegreeF = int(sDegreeF)

   #...convert temperature from F to Celsius
   nDegreeC = (nDegreeF - 32) * 5 / 9

   print ("Temperature in degrees C is:", nDegreeC)

   #...check for temperature below freezing..
   if nDegreeC < 0:
      print ("Pack long underwear!")

   #...check for it being a hot day...
   if nDegreeF > 100:
      print ("Remember to hydrate!")

   continueYN = input("Input another?")

#exit the program

3. Save it as “temperature.py”

4. Open a command window (cmd.exe, or Unix shell) on your computer and navigate the default directory to the place where you stored “temperature.py”. If you are editing in IDLE, then you can skip this step.

5. Run the program with the command “python temperature.py”. If you are running IDLE, then just select the menu option Run -> Run Module.


TRY THIS

1. Change the messages to have different advice for extreme temperatures.

2. Change the trigger temperatures (hot or cold) to something different.

3. Add another temperature trigger check for very extreme temps.

4. Collect the person’s name before the loop starts (store in a variable) and add this to the output messages.

5. (Advanced) Change the whole program into a currency converter. Input amount in US dollars and convert to Euros.


LEARN MORE

www.python.org/doc — The official documentation site for Python along with links to other tutorial and sample sites.

Learnpython.org -– A step by step tutorial site. Starts with a “Hello World” program and extends into advanced language features.

Google.com -– Search for other code and samples.


RESOURCES, TIPS, TRICKS AND HINTS

Scripting languages do not need a compiler, so you can quickly write and test programs. The Windows installation includes an environment called IDLE, which runs the Python command line in a second window while you make changes in a primary window. For Mac and Linux, this can also be done using Textpad to edit a file in one window and running the command line in a second window.

This allows you to make small changes to the file, save it, and then run it in the other windows. You can grow a simple program into a complex one in this manner.

24 Comments on Python

  1. There seems to be a bug in this python example program. It throws a syntax error until the semi colon is removed from the line below:
    sDegreeF = input(“Enter next temperature in degrees Fahrenheit (F):”;)

    I don’t believe that semicolon was present when I originally downloaded this file in January 2017.

  2. CodeCodeCode // January 27, 2019 at 11:06 am // Reply

    That’s incredible!

  3. juniorRubyist // April 4, 2018 at 6:48 pm // Reply

    Python isn’t typically used a compiled langauge, it is interpereted by an interpreter (the `python` command or IDLE)

  4. SquidVSPython // March 29, 2017 at 4:05 pm // Reply

    How do I run Python from IDLE on my Mac?

  5. Is it just me or do I not feel comfortable downloading this or other programs? I know I wouldn’t get a virus, but I’m still not comfortable with it. Plus, it doesn’t let me on Windows 10.

  6. If you are using Python 2.7, be sure to use raw_input() rather than input(). Funky things may happen otherwise.

  7. I tried this in Python 2.7.5. All I got was the numeral 2.

  8. Just a small nit, but the example uses “.pl” at the end of the program file name (e.g., temperature.pl), aka the file name extension, but I think it should use “.py” instead for consistency. “.pl” indicates it should be a Perl script, not Python (I suspect this example was converted from a Perl example), while “.py” is used for naming Python scripts. The file name extension is sometimes used to determine which language should be used to execute a given script file, although the “#!/usr/bin/env python3” in the first line of the script will take care of that, in this case.

  9. And the input code has a 3 indent! It’s supposed to be 4!

    Maybe I should change my nick to PythonHeckler.

  10. I’m using the workbook, and I just came across requirement 5a, which tells me to put in the language (done), the environment (done), the industry (done), and the program itself. The last one confuses me. Do I put in the actual program, or do I put in the synopsis of it?

    Thanks.

  11. Where is the Run command

    • If you want to run your program you can press F5

      • scoutchorton // January 15, 2019 at 4:40 pm //

        That’s only if you’re in IDLE. IDLE comes with Python depending on the operating system you’re in (like Windows or macOS, where Linux might not have it installed with it). You’d most likely have to use the command line (Command Prompt for Windows or Terminal for macOS or Linux) to navigate to and run the file.

  12. Ok. Thanks.

  13. MB Team Agent J // August 19, 2013 at 4:51 pm // Reply

    The release of Python that a scout uses is between him and his MB counselor. Tehy should use whatever release makes them happy. We are not going to get into the doctrinal disputes of the acolytes of various programming languages.

  14. IDK if the Boy Scouts want to get into the python 2 vs python 3 debate since the merit badge required that you code in 3 distinct languages. Besides, while python 2 is more commonly used, python 3 is the future.

  15. PythonCobra // July 25, 2013 at 7:00 pm // Reply

    What about 2?

  16. PythonCobra // July 25, 2013 at 6:12 pm // Reply

    What about Python 2? Why isn’t that anyplace?

    • I'd_Rather_Be_Perling // June 3, 2017 at 1:09 pm // Reply

      I was reading about that, actually. Python 2 is still a good version, and it’s completely fine to use it, but Python 3 is now considered the innovative version and the future of python. I would recommend learning Python 3, because if you learn that, you should get Python 2 just fine.

Leave a Comment

Please don't use your real name.