GithubHelp home page GithubHelp logo

18f / an_introduction_to_python Goto Github PK

View Code? Open in Web Editor NEW
10.0 7.0 8.0 104 KB

An introduction to Python - https://www.digitalgov.gov/event/online-intro-to-python/

Jupyter Notebook 94.65% Python 5.35%

an_introduction_to_python's Introduction

Introduction

Welcome to introduction to Python!

This three week class will prepare you to read and write basic python scripts. No prior programming experience required!

Help with the homework?

Email [email protected]

Administrivia

Some homework assignments are from hackerRank. There are a few reasons for this:

  1. Teaches you how to write code that will pass tests.
  2. No installation required - you don't need Python locally in order to write code here.
  3. Solve real problems.

Course Breakdown

Week 1

  • Introduction to Python - Class 1 (class time 30 minutes) - CLASS ONE NOTES

    • writing your first program
    • understanding Python data types
    • if/else statements in Python
    • functions in python
    • printing to the screen
    • string processing basics - the .format operator.
  • Standing on the shoulders of giants - Class 2 (class time 45 minutes) - CLASS TWO NOTES

    • reading and writing files
    • introduction to import statements
    • introduction to the os module

Homework:

Assignment for class 1:

  1. https://www.hackerrank.com/challenges/py-hello-world
  2. https://www.hackerrank.com/challenges/python-raw-input
  3. https://www.hackerrank.com/challenges/py-if-else
  4. https://www.hackerrank.com/challenges/python-arithmetic-operators
  5. https://www.hackerrank.com/challenges/python-division
  6. https://www.hackerrank.com/challenges/write-a-function
  7. https://www.hackerrank.com/challenges/python-print

Assignment for class 2:

For this assignment you will be either creating an account on https://www.pythonanywhere.com or downloading and installing Python locally. If at all possible, it is better to install python locally.

But if you are restricted from doing so, PythonAnywhere works.

  1. Write a Python program called create_file.py

The program should create a file called practing_file_writing.txt. The file should contain the following text:

Hello! You've successfully created this file with a program! Congradulations!!!!

  1. Write a Python program called edit_file.py

The program should open practicing_file_writing.txt and then add the following line to the end of the file:

Sincerely, Python

So the whole file should now look like:

Hello! You've successfully created this file with a program! Congradulations!!!! Sincerely, Python

The file should then be writen back out as practicing_file_updating.txt.

  1. Create a directory called to_traverse and put the file two files created above in the directory. Then from the directory above to_traverse put a python program called traverse_and_open.py. This python program should change directories, read both the files into memory and then check if the two files contents are equal.

Week 2

  • Introduction to Python Data Structures - Class 1 (class time 30 minutes)

    • lists
    • while loops
    • for loops
    • dictionaries
    • sets
  • More file processing - Class 2 (class time 45 minutes)

    • string processing
    • using for loops to read a bunch of text from a file
    • using for loops to write a bunch of text to a file
    • using for loops to move from one directory to the next
    • working with os.walk

Assignment for class 3:

  1. https://www.hackerrank.com/challenges/python-lists
  2. https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list
  3. https://www.hackerrank.com/challenges/finding-the-percentage
  4. https://www.hackerrank.com/challenges/py-introduction-to-sets
  5. https://www.hackerrank.com/challenges/symmetric-difference
  6. https://www.hackerrank.com/challenges/python-loops

Assignment for class 4:

  1. Write a program called traversal.py - This file will traverse the entire file system, starting at the first directory you give it.

  2. Write a new program called print_traversal.py - this file will traverse the entire file system, starting at the first directory you give it and print out the full paths of every filename it traverses.

  3. Write a new program called print_analyze_traversal.py - this file traverse the entire file system, starting at the first directory you give it and print out the full paths of every filename it traverses. Also it will record the number of times the file is a python file and the number of times it is not. At the end of the program, the program should print out how many python files were found and how many non-python files were found. It should print this out in absolute terms and relative terms. Example:

total number of python files found: 5 total number of non-python files found: 500 percentage of python files: 1% percentage of non-python files: 99%

Week 3

  • Introduction to methods - Class 1 (class time 30 minutes)

    • methods on integers
    • methods on strings
    • methods on lists
    • working with dictionaries
  • Making use of methods on strings with files (class time 45 minutes)

    • advanced string processing
    • reading files and processing them as strings in Python
    • traversing directories to look for patterns
    • introduction to very basic regular expressions

Assignment for class 5:

Assignment for class 6:

Now that you have an understanding of how to iterate over a set of things, let's make use of that to really do something interesting!

  1. write a program called find.py. This program will start at whatever directory you give it and traverse until it finds a specific string. In this case, the string will be Hello there! If the string is never found, after looking through all subfolders and files, the program terminates with, couldn't find the string.

How you'll call the file: python find.py dir_name

  1. Now you'll be creating a new program called find_replace.py. This program will do the same traversal as the last program, except, it will also open any files with Hello there! edit that line to say Hi instead of Hello there! and close the file, writing it back out to the file system.

  2. Now you'll be going even further - now instead of looking for a simple find and replace, you'll be adding descriptive statistics to any files you find. The descriptive statistics you'll be adding are:

  3. the number of lines in the file

  4. the five most commonly used words in the file

  5. if the file is a program, what language is it written in

Note you should write functions for each of these sub commands. Here's what a typical output should look like:

This file has 10 lines The five most common words used are: print, if, else, import, and This file is written in Python

an_introduction_to_python's People

Contributors

ericschles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

an_introduction_to_python's Issues

Homework 2 creating .py files and writing to them

I opened up a “console” on pythonanywhere and wrote the following code: new_file = open(“create_file.py”, “w”). I know that this command will create a new file for me called create_file and that it will be stored in the current working directory. However, after hitting enter and getting a new line in the console, I’m not sure how I can write to create_file.py the code for a program that will create another file (in this case, practicing_writing_text.txt). What I did to get around this problem was to just go to my dashboard and click on the file that I just created, create_file.py, and then write the code needed to create practicing_writing_text.txt in that file. Is that how we were supposed to do this? I’m not sure if I’m missing something important here.

Questions on HW2

I’m working on the 3 week python class that just wrapped and I’m finishing homework 2. Unfortunately, I have to use pythonanywhere to do the work, since I don’t have administrative privileges to install software on my work computer. I have just a few questions.

I opened up a “console” on pythonanywhere (which I’m assuming is sort of a python-specific CLI?) and wrote the following code: new_file = open(“create_file.py”, “w”). I know that this command will create a new file for me called create_file and that it will be stored in the current working directory (which from what I can tell is home/jmacula for me). However, after hitting enter and getting a new line in the console, I’m not sure how I can write to create_file.py the code for a program that will create another¬ file (in this case, practicing_writing_text.txt). What I did to get around this problem was to just go to my dashboard and click on the file that I just created, create_file.py, and then write the code needed to create practicing_writing_text.txt in that file. Is that how we were supposed to do this? I’m not sure if I’m missing something important here.

Once I created the two .py files, the two .txt files (for edit.py and practicing_file_updating.txt I just followed the method described above), and the to_traverse/ directory, I’m getting stuck on moving the .txt files into the to_traverse directory using code. I tried using both the os.rename() and the shutil.move() methods as follows:

os.rename(home/jmacula/practicing_file_writing.txt, home/jmacula/to_traverse/practicing_file_writing.txt)
os.rename(“home/jmacula/practicing_file_writing.txt”, “home/jmacula/to_traverse/practicing_file_writing.txt”)
        
shutil.move(home/jmacula/practicing_file_writing.txt, home/jmacula/to_traverse/practicing_file_writing.txt)
shutil.move(“home/jmacula/practicing_file_writing.txt”, “home/jmacula/to_traverse/practicing_file_writing.txt”)

When I don’t wrap the file paths in quotes, python throws me the following error: NameError: name ‘practicing_file_writing’ is not defined. When I wrap them in quotes, python throws me a different error: FileNotFoundError: [Errno 2] No such file or directory ‘practicing_file_writing.txt’. Could you explain briefly (I know you’re busy and have limited time for newbies!) how you’re supposed to use these methods?

For the last bit of the homework, I’m assuming I should do the following: import os, then change the cwd to whatever directory is above to_traverse/ (which I’m assuming is jmacula), create a .py file there, then open that .py file and write the necessary code to change directories, read in the .txt files, assign the contents of those read commands to variables, then compare those variables to test for equality?

Sorry if this should all be super obvious; I looked through some SO questions/python documentation, but couldn’t get satisfactory answers to these questions.

Introduce yourself

Hello! 👋

Welcome to the Intro to Python class.

Please introduce yourself in a comment below. Tell us where you work, and why you are interested in learning python.

What is your home directory on PythonAnywhere?

I am a bit stuck on using PythonAnywhere. When I type in import os and print(os.getcwd()) to figure out my current working directory, the line “/home/alexbijak” is returned. I’m confused as to where “alexbijak” is exactly. Is this just on the PythonAnywhere server? I should not be looking for anything on my local drive called “alexbijak”, right?

How to post a question

This is a place to post questions around the Intro to Python class homework or ask technical questions relating to python.

How to post a question:

  1. Log in to GitHub
  2. Visit this page #1
  3. To ask a new question — create a " new issue" (green button)
  4. To respond on existing open question from someone else — use the comment box on the question page.

@EricSchles will be notified of any questions. You will be notified of any responses.

Homework 2 question about directories

I created the two .py files, the two .txt files and the to_traverse/ directory for this assignment, but I’m getting stuck on moving the .txt files into the to_traverse directory using code. I tried using both the os.rename() and the shutil.move() methods as follows:

os.rename(home/jmacula/practicing_file_writing.txt,home/jmacula/to_traverse/practicing_file_writing.txt)

os.rename(“home/jmacula/practicing_file_writing.txt”,“home/jmacula/to_traverse/practicing_file_writing.txt”)

shutil.move(home/jmacula/practicing_file_writing.txt,home/jmacula/to_traverse/practicing_file_writing.txt)

shutil.move(“home/jmacula/practicing_file_writing.txt”,“home/jmacula/to_traverse/practicing_file_writing.txt”)

When I don’t wrap the file paths in quotes, python throws me the following error: NameError: name ‘practicing_file_writing’ is not defined. When I wrap them in quotes, python throws me a different error: FileNotFoundError: [Errno 2] No such file or directory ‘practicing_file_writing.txt’. Could you explain briefly (I know you’re busy and have limited time for newbies!) how you’re supposed to use these methods?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.