Skillshare intro to Python/Unit 4

From OpenHatch wiki


Goal #1: using modules

(Estimated completion time: 15 - 30 minutes)

To get some more practice with the random module, we're going to read, run, and extend some Python scripts that use this module.

For each of the following Python scripts, please do the following:

  1. Download the script and save it to your Desktop. Be sure to save it as a .py file.
  2. Open the script in your text editor.
  3. Read through the script. Answer these questions:
    • Overall, what does this script do?
    • What variables and data types are used? Where are the strings, integers, and floats?
    • What functions from the random module are used, and why?
  4. Once you have a good sense of what the script does, open a terminal, navigate to the directory where you saved the script, and run it. Does it do what you expected?

Here are you scripts to read and run. Have fun with them!

For die.py, how would you change the program to simulate rolling 2 dices?

For password_generator.py, how would you generate a password that was, instead of a fixed 10 characters long, a random length between 10 and 14 characters?


Goal #2: reading from files

(Estimated completion time: 15 - 30 minutes)

To get some more practice with reading from files, we're going to read, run, and extend some Python scripts.

Countries example

Here are the country names text file and Python script from the lesson:

Please download both files and save them to your Desktop. Review countries.py, make sure you understand it, and then run it!

Then, add code to the file to answer the following questions:

  • What are all of the countries that end in "land"?
  • What is the shortest country name?


Wordplay example

Here are two new files. The first, sowpods.txt, is the official Scrabble wordlists we'll use for our Scrabble cheater. The second is a Python script that uses data from sowpods.txt to find weird and interesting words:

Please download both files and save them to your Desktop. Open wordplay.py, see if you can guess what it does, and then run it!

Then, using the existing code as a template, add code to the file to answer the following questions:

  • What are all of the words that contain "VV"?
  • What are all of the words that contain "Q" without a "U"?
  • What are all of the words that contain all of the vowels and Y, ("A", "E", "I", "O", "U", "Y") in any order?

What other weird words can you find?


Success!

You have mastered modules and files in Python. Keep practicing!