Programming

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 Farenheight (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.pl”

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

5. Run the program with the command “python temperature.pl”. 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.

Comments about “Python”

  1. Nit_Picker says:

    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.

  2. PythonCobra says:

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

    Maybe I should change my nick to PythonHeckler.

  3. PythonCobra says:

    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.

  4. Comment says:

    Where is the Run command

  5. PythonCobra says:

    Ok. Thanks.

  6. MB Team Agent J says:

    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.

  7. lame_brain says:

    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.

  8. PythonCobra says:

    What about 2?

  9. PythonCobra says:

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

Write a comment about “Python”

Nickname:

Type your comment:

  • 1. Safety. Do the following:
      a. Show your counselor your current, up-to-date Cyber Chip.
      b. Discuss first aid and prevention for the types of injuries or illnesses that could occur during programming activities, including repetitive stress injuries and eyestrain.

    2. History. Do the following:
      a. Give a brief history of programming, including at least three milestones related to the advancement or development of programming.
      b. Describe the evolution of programming methods and how they have improved over time.

    3. General knowledge. Do the following:
      a. Create a list of 10 popular programming languages in use today and describe which industry or industries they are primarily used in and why.
      b. Describe three different programmed devices you rely on every day.

    4. Intellectual property. Do the following:
      a. Explain how software patents and copyrights protect a programmer.
      b. Describe the difference between licensing and owning software.
      c. Describe the differences between freeware, open source, and commercial software, and why it is important to respect the terms of use of each.

    5. Projects. Do the following:
      a. With your counselor’s approval, choose a sample program. Then, as a minimum, modify the code or add a function or subprogram to it. Debug and demonstrate the modified program to your counselor.
      b. With your counselor’s approval, choose a second programming language and development environment, different from those used for requirement 5a and in a different industry from 5a. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      c. With your counselor’s approval, choose a third programming language and development environment, different from those used for requirements 5a and 5b and in a different industry from 5a or 5b. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      d. Explain how the programs you wrote for requirements 5a, 5b, and 5c process inputs, how they make decisions based on those inputs, and how they provide outputs based on the decision making.

    6. Careers. Find out about three career opportunities in programming. Pick one and find out the education, training, and experience required. Discuss this with your counselor and explain why this career might be of interest to you.

  • Earning the Cyber Chip can help you learn how to stay safe while you are online and using social networks or the latest electronic gadgets.

    Topics include cell phone use, texting, blogging, gaming, cyberbullying, and identity theft. Find out more about the Cyber Chip at www.scouting.org/cyberchip.