diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..93216055 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\Tyler\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe" +} \ No newline at end of file diff --git a/README b/README deleted file mode 100644 index e69de29b..00000000 diff --git a/Requests.md b/Requests.md new file mode 100644 index 00000000..6a080ba8 --- /dev/null +++ b/Requests.md @@ -0,0 +1,76 @@ +# Python3 Requests Module Examples +This file contains examples for different actions using the Requests III module. All examples are Python3. + +## https://3.python-requests.org/user/quickstart + +### Download Images +```python3 +r = requests.get('https://imgs.xkcd.com/comics/python.png') + +with open('img.jpg', 'wb') as f: + for chunk in r.iter_content(chunk_size=128): + f.write(chunk) +``` + +### Save Responses in JSON Dictionary +```python3 +payload = {'username': 'corey', 'password': 'testing'} +r = requests.post('https://httpbin.org/post', data=payload) + +r_dict = r.json() #Saves JSON response as a dictionary + +print(r_dict['form']) #Retrieve particular value +``` + +### Timeout in Seconds - Should be used for production code +```python3 +requests.get('https://github.com/', timeout=0.001) +``` + +### Errors and Exceptions + +In the event of a network problem (e.g. DNS failure, refused connection, etc), Requests will raise a **```ConnectionError```** exception. +**```Response.raise_for_status()```** will raise an **```HTTPError```** if the HTTP request returned an unsuccessful status code. +If a request times out, a **```Timeout```** exception is raised. +If a request exceeds the configured number of maximum redirections, a **```TooManyRedirects```** exception is raised. + +### Web Scraper with Request and BeautifulSoup4 +```python3 +from bs4 import BeautifulSoup +import requests +import csv + +source = requests.get('http://coreyms.com').text + +soup = BeautifulSoup(source, 'lxml') + +csv_file = open('cms_scrape.csv', 'w') + +csv_writer = csv.writer(csv_file) +csv_writer.writerow(['headline', 'summary', 'video_link']) + +for article in soup.find_all('article'): + headline = article.h2.a.text + print(headline) + + summary = article.find('div', class_='entry-content').p.text + print(summary) + + try: + vid_src = article.find('iframe', class_='youtube-player')['src'] + + vid_id = vid_src.split('/')[4] + vid_id = vid_id.split('?')[0] + + yt_link = f'https://youtube.com/watch?v={vid_id}' + except Exception as e: + yt_link = None + + print(yt_link) + + print() + + csv_writer.writerow([headline, summary, yt_link]) + +csv_file.close() +``` diff --git a/challenge1.py b/challenge1.py new file mode 100644 index 00000000..b4363007 --- /dev/null +++ b/challenge1.py @@ -0,0 +1,19 @@ +# Question 1 +# Level 1 + +# Question: +# Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, +# between 2000 and 3200 (both included). +# The numbers obtained should be printed in a comma-separated sequence on a single line. + + +for i in range(2000, 32001): + if i%7 == 0 and i%5 != 0: + print(i, end=',') + + + + + + + diff --git a/challenge2.py b/challenge2.py new file mode 100644 index 00000000..ee98b421 --- /dev/null +++ b/challenge2.py @@ -0,0 +1,26 @@ +# Question 2 +# Level 1 + +# Question: +# Write a program which can compute the factorial of a given numbers. +# The results should be printed in a comma-separated sequence on a single line. +# Suppose the following input is supplied to the program: +# 8 +# Then, the output should be: +# 40320 + +number = int(input("What number? ")) +total = number +for i in range((number-1), 1, -1): + total *= i + +print('Total = ' + str(total)) + +#other solution +def fact(x): + if x == 0: + return 1 + return x * fact(x - 1) + +x=int(input()) +print (fact(x)) \ No newline at end of file diff --git a/challenge3.py b/challenge3.py new file mode 100644 index 00000000..6924e227 --- /dev/null +++ b/challenge3.py @@ -0,0 +1,20 @@ +# Question 3 +# Level 1 + +# Question: +# With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary. +# Suppose the following input is supplied to the program: +# 8 +# Then, the output should be: +# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64} + +a = {} + +def todict(x): + for i in range(1, (x+1)): + a[i] = i*i + print (a) + +number = int(input("Number: ")) + +todict(number) diff --git a/challenge4.py b/challenge4.py new file mode 100644 index 00000000..82f498fa --- /dev/null +++ b/challenge4.py @@ -0,0 +1,21 @@ +# Question 4 +# Level 1 + +# Question: +# Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. +# Suppose the following input is supplied to the program: +# 34,67,55,33,12,98 +# Then, the output should be: +# ['34', '67', '55', '33', '12', '98'] +# ('34', '67', '55', '33', '12', '98') + +txt = input('Enter string: ') + +x = txt.split(',') +y = tuple(x) + +print(x[:2]) +print(x[2:]) +print(x[:-2]) +print(x[0:2]) +print(y) \ No newline at end of file diff --git a/python contents.docx b/python contents.docx deleted file mode 100644 index 184715d8..00000000 Binary files a/python contents.docx and /dev/null differ diff --git a/python contents.txt b/python contents.txt deleted file mode 100644 index db6c7e86..00000000 --- a/python contents.txt +++ /dev/null @@ -1,188 +0,0 @@ -Python -The below table largely covers the TOC for 5 popular books. Learning Python (Fourth Edition) has a more in-depth look at concepts than any of the other books. However this book also does not essentially cover some aspects that are covered in other books. -No. Diving into Python The Python Standard Library by Example Python Essential Reference (4th edition) The Quick Python Book Learning Python -Introductory Concepts covering installation on different OS, version history, interpreter. This section also covers questions like Why, Who, What and Where on Python. -1 1. Installing Python -2. Which Python is right for you ? -3. Python & your OS -4. Interactive Shell -5. Summary 1. Introduction (Text) 1. Tutorial Introduction -2. Lexical Conventions and Syntax 1. About Python -2. Getting Started 1. Python Q & A Session -1. Why do people use Python ? -2. Downside of using it -3. Who uses Python Today ? -4. What Can I do with Python ? -5. Python vs Language X -6. Test your Knowledge -2. How Python runs programs -1. Python Interpreter -2. Program Execution -1. Programmer View -2. Python View -3. Execution Model Variations -1. Implementation Alternatives -2. Execution Optimization Tools -3. Frozen Binaries -3. How you run programs -1. Interactive prompt -2. Your first script - -Python Object Types, Numeric Types, Data Structures, Control Structures, Scopes and Arguments -2 1. Your first program -2. Declaring Functions -3. Python Data types vs Other Languages -4. Documenting Functions -5. Everything is an Object -6. The Import Search Path -7. What is an Object ? -8. Indenting Code -9. Testing Modules -10. Native Datatypes -1. Dictionaries -2. List -3. Tuples -11. Variables & referencing 1. Data Structures 1. Types and Objects -2. Operators and Expressions -3. Program Structure and Control Flow -4. Functions and Functional Programming -5. Classes and Object Oriented Programming -6. Modules, Packages and Distribution -7. Input and Output -8. Execution Environment -9. Testing, Debugging, Profiling and Tuning - -Data Structures, Algorithms & Code simplification -String & Text Handling 1. Python Overview -1. Built-in Data types -2. Control Structures -3. Module -4. OOPs -2. Basics -1. Lists -2. Dictionaries -3. Tuple -4. Sets -5. Strings -6. Control Flow -3. Functions -4. Modules and Scoping Rules -5. Python Programs 1. Introducing Python Object Types -1. Why use built-in Types ? -2. Core data types -3. Numbers, Lists, Dictionaries, Tuples, Files, Other Core Types -4. User Defined Classes -2. Numeric Types -1. Literals, Built-in tools, expression operators -2. Formats, Comparisons, Division, Precision -3. Complex Numbers -4. Hexadecimal, Octal & Binary -5. Bitwise Operations -6. Decimal, Fraction, Sets, Booleans - -1. Statements & Syntax -2. Assignments, Expressions & Syntax -3. If Tests & Syntax Rules -4. Scopes -5. Arguments -Built-in functions, Function Design, Recursive Functions, Introspection, Annotations, Lambda, Filter and Reduce -3 1. Power of Introspection -1. Optional and Named Arguments -2. type, str, dir and other built-in functions -3. Object References with getattr -4. Filtering Lists -5. Lambda Functions -6. Real world Lambda functions - None 1. Built-in functions -2. Python run-time services None Built-in functions are covered as part of the topic above but from a numeric perspective -1. Advanced Function Topics -1. Function Design -2. Recursive Functions -3. Attributes and Annotation -4. Lambda -5. Mapping Functions over sequences -6. Filter and Reduce - -Special Class Attributes -Display Tool -OOPS, Modules -4 1. Objects and Object Orientation -1. Importing Modules -2. Defining Classes -3. Initializing and Coding Classes -4. Self & __init__ -5. Instantiating Classes -6. Garbage Collection -7. Wrapper Classes -8. Special Class Methods -9. Advanced Class Methods -10. Class Attributes -11. Private Functions None Covered partially section 2 1. Packages -2. Data Types and Objects -3. Advanced Object Oriented Features 1. Modules -1. Why use Modules ? -2. Program Architecture -3. Module Search Path -4. Module Creation & Usage -5. Namespaces -6. Reloading Modules -7. Packages -2. Advanced Module Topics -1. Data Hiding in Modules -2. as Extension for import and from -3. Modules are Objects: Metaprograms -4. Transitive Module Reloads -5. Module Design Concepts -6. Module Gotchas -3. OOP -1. Why use classes ? -2. Classes & Instances -3. Attribute Inheritance Search -4. Class Method Calls -5. Class Trees -6. Class Objects & Default Behavior -7. Instance Objects are Concrete Items -8. Intercepting Python Operators -9. Classes Vs. Dictionaries -10. Class customization by Inheritance -11. Operator Overloading -12. Subclasses -13. Polymorphism in Action -14. Designing with Classes -15. Mix-in Classes -Advanced Class Topics -5 None None None None 1. Advanced Class Topics -1. Extending Types by Embedding -2. Extending Types by Subclassing -3. Static and Class Methods -4. Decorators and Metaclasses -5. Class Gotchas -Exceptions -6 1. Exceptions and File Handling -1. Handling Exceptions -2. Using exceptions for other purposes 1. Exceptions 1. Exceptions Basics -1. Why use Exceptions ? -2. Default Exception Handler -3. User-Defined Exceptions -4. Class Based Exceptions -5. Designing with Exceptions -XML, HTTP, SOAP, Network Programming, I18N, Unicode -7 1. Regular Expressions -2. Parsing / Processing Mark-up languages (HTML, XML) -1. Unicode -3. HTTP Web Services -1. Headers -2. Debugging -4. SOAP Web Services 1. Networking -2. Internet -3. Email -4. Internationalization and Localization 1. Network Programming and Sockets -2. Internet Application Programming -3. Web Programming -4. Internet Data Handling & Encoding 1. Network, web programming 1. Unicode and Bytes Strings -Miscellaneous -8 None 1. Algorithms -2. Cryptography -3. Data compression and archiving -4. Processes and Threads -5. Data persistence & exchange 1. Extending & Embedding Python 1. GUI None