Keyboard navigation- Python prerequisite
Python
…
Prerequisites
…
Keyboard navigation
…
NOTE… to be certain that you are viewing the code with proper syntax and block structure you should open your browser to desktop site. If you are using Chrome browser on Android then tap the three dots at the top right corner in the drop-down menu it will say desktop site second from the bottom tap that to get a proper view of the code and its structure.
I will make every effort to present the code and block structure so that it is appropriate in mobile view but there may be times when that is impossible so always double-check with desktop view and also double check by simply referring to your textbook
“Automate the boring stuff”
Refer to the textbook whenever you are in doubt about weather a piece of code snippet is input or returned.
It is good practice to transcribe the return but it is not a requirement especially with long returns
To avoid your confusion take notice of the map keys that I am now going to give you for all of the homework assignments.
…
Bold text indicates the original code snippet from the textbook that you are to transcribe
…
Normal text preceded by a hash mark in the code is the return you will get from the code.
For example
>>>print(‘how goes it friend’) #code
# ‘how goes it friend’ ………. #return
…
Transcribe your code to Google Docs in normal text like the text you see here. Separate each copy of code transcription by a series of dots so that you can keep track of where you are working
…
The first space under each code snippet will be provided for you to enter your transcription and will be followed by two lines of dots two separate that code snippet from the next one as below
…
_Transcribe your code here –
…
…
#
Normal Text that has a hash mark for header and footer is information from the textbook to explain the code you are working onlike what you see here…
#
…
…
Two dots indicates a break in the subject matter like what you see directly above this line of text.
…
…
Assignment 1
#
The objective of this lesson assignment is to strengthen your keyboard skills and navigations.
You do not need to worry too much about what this code does at this time these are being pulled from multiple chapters in The Textbook because they are hard to type but we will go over these code Snippets in detail and in context as we go through the books.
#
…
Assignment instructions
…
transcribe each code snipet at least 4 times.
…
…
#
From Ch. 8
Backslash on Windows and Forward Slash on OS X and Linux
#
…
>>> import os
>>> os.path.join(‘usr’, ‘bin’, ‘spam’)
‘usr\\bin\\spam’
…
…
…
#
Find the multiple file types
#
…
>>> myFiles = [‘accounts.txt’, ‘details.csv’, ‘invite.docx’]
>>> for filename in myFiles:
print(os.path.join(‘C:\\Users\\asweigart’, filename))
# C:\Users\asweigart\accounts.txt
C:\Users\asweigart\details.csv
C:\Users\asweigart\invite.docx
…
…
…
>>> import os
>>> os.getcwd()
‘C:\\Python34’
…
…
…
>>>os.chdir(‘C:\\Windows\\System32’)
>>> os.getcwd()
‘C:\\Windows\\System32’
…
…
…
#
PyPDF2. To install it, run
pip install PyPDF2 from the command line. This module name is case sensitive, so make sure the y is lowercase and everything else is uppercase.
#
…
…
#
From Ch. 13
#
>>> import PyPDF2
>>> pdfFileObj = open(‘meetingminutes.pdf’, ‘rb’)
>>> pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
>>> pdfReader.numPages
# 1 -19
>>> pageObj = pdfReader.getPage(0)
>>> pageObj.extractText()
# 2 – ‘OOFFFFIICCIIAALL BBOOAARRDD MMIINNUUTTEESS Meeting of March 7, 2015
\n The Board of Elementary and Secondary Education shall provide leadership…
….
…
…
#
here I did the first transcription for you. take note of the # 1 and #2 …that would be the return which I did not type because it’s would be the return value when you type the code in the interactive shell.
#
>>>importPyPDF
>>>pdfFileObj = open(‘meetingminutes.pdf’, ‘rb’)
>>>pdfReader =PyPDF2.pdfFileReader(pdfFileObj)
>>>pdfReader.numPages
# 1
>>>pageObj = pdfReader.getPage(0)
>>>pageObj.extractText()
# 2
…
…
>>> import PyPDF2
>>> pdfReader = PyPDF2.PdfFileReader(open(‘encrypted.pdf’, ‘rb’))
>>> pdfReader.isEncrypted
# 1 – True
>>> pdfReader.getPage(0)
# 2 -Traceback (most recent call last):
File “<pyshell#173>”, line 1, in <module>
pdfReader.getPage()
–snip–
File “C:\Python34\lib\site-packages\PyPDF2\pdf.py”, line 1173, in getObject
raise utils.PdfReadError(“file has not been decrypted”)
PyPDF2.utils.PdfReadError: file has not been decrypted
>>> pdfReader.decrypt(‘rosebud’)
# 3 -1
>>> pageObj = pdfReader.getPage(0)
# 4 -returns page content
…
…
…
From Ch. 12
Writing Excel Documents
…
Creating and Saving Excel Documents
…
>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> wb.get_sheet_names()
#1 [‘Sheet’]
>>> sheet = wb.get_active_sheet()
>>> sheet.title
#2 ‘Sheet’
>>> sheet.title = ‘Spam Bacon Eggs Sheet’
>>> wb.get_sheet_names()
#3 [‘Spam Bacon Eggs Sheet’]
…
…
…
Writing Values to Cells
…
>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> sheet = wb.get_sheet_by_name(‘Sheet’)
>>> sheet[‘A1’] = ‘Hello world!’
>>> sheet[‘A1’].value
# ‘Hello world!’
…
…
…
From Ch 11
-Finding Elements on the web Page-
…
Not and duplicate block structure
….
…
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(‘http://inventwithpython.com‘)
try:
elem = browser.find_element_by_class_name(‘bookcover’)
print(‘Found <%s> element with that class name!’ % (elem.tag_name))
except:
print(‘Was not able to find an element with that name.’)
…
…
…
From Ch 9
Compressing Files with the zipfile Module
…
>> import zipfile, os
>>> os.chdir(‘C:\\’) # move to the folder with example.zip
>>> exampleZip = zipfile.ZipFile(‘example.zip’)
>>> exampleZip.namelist()
# [‘spam.txt’, ‘cats/’, ‘cats/catnames.txt’, ‘cats/zophie.jpg’]
…
…
…
From invent games with python
…
#
Do not duplicate the numbers on the Left margin they are not part of the code
#
Game Code ( outline )
TELLING JOKES
…
print(‘What do you get when you cross a snowman with a vampire?’)
2. input()
3. print(‘Frostbite!’)
4. print()
5. print(‘What do dentists call a astronaut’s cavity?’)
6. input()
7. print(‘A black hole!’)
8. print()
9. print(‘Knock knock.’)
10. input()
11. print(“Who’s there?”)
12. input()
13. print(‘Interrupting cow.’)
14. input()
15. print(‘Interrupting cow wh’, end=”)
16. print(‘-MOO!’)
…
…
…
From ch 3
Magic 8-ball
For loops
# duplicate the block structure it is part of the sentax.
…
import random
def getAnswer(answerNumber):
if answerNumber == 1:
return ‘It is certain’
elif answerNumber == 2:
return ‘It is decidedly so’
elif answerNumber == 3:
return ‘Yes’
elif answerNumber == 4:
return ‘Reply hazy try again’
elif answerNumber == 5:
return ‘Ask again later’
elif answerNumber == 6:
return ‘Concentrate and ask again’
elif answerNumber == 7:
return ‘My reply is no’
elif answerNumber == 8:
return ‘Outlook not so good’
elif answerNumber == 9:
return ‘Very doubtful’
r = random.randint(1, 9)
fortune = getAnswer(r)
print(fortune)
…
…
…
From Ch 4
Converting Types with the list() and tuple() Functions
…
>>> tuple([‘cat’, ‘dog’, 5])
(‘cat’, ‘dog’, 5)
…
…
…
>>> list((‘cat’, ‘dog’, 5))
[‘cat’, ‘dog’, 5]
…
…
…
>>> list(‘hello’)
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
…
…
…
From Ch 5
Building dictionaries
…
birthdays = {‘Alice’: ‘Apr 1’, ‘Bob’: ‘Dec 12’, ‘Carol’: ‘Mar 4’}
while True:
print(‘Enter a name: (blank to quit)’)
name = input()
if name == ”:
break
if name in birthdays:
print(birthdays[name] + ‘ is the birthday of ‘ + name)
else:
print(‘I do not have birthday information for ‘ + name)
print(‘What is their birthday?’)
bday = input()
birthdays[name] = bday
print(‘Birthday database updated.’)
…
…
…
The Get method ()
…
>>> picnicItems = {‘apples’: 5, ‘cups’: 2}
>>> ‘I am bringing ‘ + str(picnicItems.get(‘cups’, 0)) + ‘ cups.’
# ‘I am bringing 2 cups.’
>>> ‘I am bringing ‘ + str(picnicItems.get(‘eggs’, 0)) + ‘ eggs.’
# ‘I am bringing 0 eggs.’
…
…
…
…
#
Okay folks that’s it for this lesson normally we will try to do one full chapter out of the book every week but for now I think this will have you pretty well occupied for about a week and pulling your hair out and hating life sorry about that. But if we get the really nasty heavy lifting out of the way up front everything else will be much easier trust me until next week
…keep coding…
2 COMMENTS
I ? python
It’s making me a ? software developer?…?
Seriously though this comment box is for you guys to use it’s not often that I can check the comments because my time is highly occupied already but when you put in comments they will be here for other people to see and your questions and answers will help other people in the community so please use this comment box liberally. But if you have questions and want immediate answers my best advice is to go to your textbook… Not because I’m being evasive but because as you know I am learning python for the first time just like you so any answers I could give you came from the textbook and you could probably find them faster if you go to the textbook and search