├── .gitignore ├── LICENSE ├── README.md ├── function.py ├── json.py ├── read_file.py ├── web_page_retrieve.py └── write_file.py /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/launch.json 2 | .vscode/settings.json 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 WebBreacher 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repository is meant to help teach people the basics of Python3 coding. 2 | 3 | It is not by any means complete but it demonstrates many of the basic pieces of code that people need in their scripts. 4 | -------------------------------------------------------------------------------- /function.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Script to demonstrate how to use a function in Python3 3 | ''' 4 | 5 | # Function 1 does some action 6 | def my_first_function(): 7 | print('\nIt worked!') 8 | 9 | # Function 2 accepts a variable 10 | def my_second_function(name): 11 | print('\nMy name is {}'.format(name)) 12 | 13 | # Function 3 accepts 2 variables and performs some math 14 | def my_third_function(number1, number2): 15 | print("\nLet's do some math on {0} and {1}".format(number1, number2)) 16 | print('Addition: ' + str(number1 + number2)) 17 | print() 18 | 19 | # Set up our variables 20 | # The input() statement prompts a user for input 21 | name = input("\nWhat is your name? ") 22 | num1 = 4 23 | num2 = 99 24 | 25 | # Executing the functions 26 | my_first_function() 27 | my_second_function(name) 28 | my_third_function(num1, num2) -------------------------------------------------------------------------------- /json.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Script to demonstrate how to retrieve a web page 3 | using the requests library and parsing with json 4 | ''' 5 | 6 | # Import the contents of the requests library 7 | import requests 8 | 9 | # URL to make the request to 10 | url = 'http://en.gravatar.com/fyodor.json' 11 | 12 | # Make the request, don't check for HTTPS certificate issues and store response in 'response' 13 | print('Making a request to {}'.format(url)) 14 | response = requests.get(url, timeout=30, verify=False) 15 | json_out = response.json() 16 | 17 | # The Gravatar output is in JSON format so we do not need to convert. 18 | # We do need to correctly parse each element whether dictionary or list. 19 | # Here we look in the 'entry' element, then take the first item from the 20 | # list (element 0) and, within that, look for a dictionary entry with 'id' 21 | # as the key. 22 | print('The ID is: {0}'.format(json_out["entry"][0]["id"])) -------------------------------------------------------------------------------- /read_file.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Script to demonstrate how to read files locally in Python3 3 | ''' 4 | 5 | # Create a file handler (fh) and read in a certain file 6 | fh = open('read_file.py', 'r') 7 | 8 | # Create a variable for a counter and assign a value 9 | counter = 1 10 | 11 | # Start a for loop to iterate over all the contents of the fh 12 | for line in fh.readlines(): 13 | 14 | # For each line, we 'strip()' off the end return characters 15 | # then print it to the console using the value of the 16 | # counter variable as a line number 17 | print('{0} : {1}'.format(counter, line.strip())) 18 | 19 | # Increment the counter variable by 1 20 | counter += 1 21 | fh.close() 22 | -------------------------------------------------------------------------------- /web_page_retrieve.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Script to demonstrate how to retrieve a web page 3 | using the requests library 4 | ''' 5 | 6 | # Import the contents of the requests library 7 | import requests 8 | 9 | # URL to make the request to 10 | url = 'http://whatu.info' 11 | 12 | # Make the request, don't check for HTTPS certificate issues and store response in 'response' 13 | response = requests.get(url, timeout=30, verify=False) 14 | 15 | print(response.text) -------------------------------------------------------------------------------- /write_file.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Script to demonstrate how to write files locally in Python3 3 | ''' 4 | 5 | # Create a file handler (fh) and open it for appending 6 | fh = open('output1', 'a') 7 | 8 | # Create a variable for a counter and assign a value 9 | counter = 1 10 | 11 | # Start a loop to cycle through numbers 12 | while counter < 10: 13 | 14 | fh.write('{0}\n'.format(str(counter))) 15 | counter += 1 16 | 17 | fh.close() --------------------------------------------------------------------------------