diff --git a/README.md b/README.md index b20c87d..194e955 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,19 @@

+ :information_source:  This repo contains questions and exercises to learn and practice Python :bar_chart:  There are currently **231** exercises and questions +- [Free App for Learning Python](#free-app-for-learning-python) - [Exercises](#exercises) - [Hello World](#hello-world) - - [Objects & Data Types](#objects--data-types) + - [Objects \& Data Types](#objects--data-types) - [Variables](#variables) - [Booleans](#booleans) - [Strings](#strings) - [Numbers](#numbers) - - [Lists & Tuples](#lists--tuples) + - [Lists \& Tuples](#lists--tuples) - [Dictionaries](#dictionaries) - [Loops](#loops) - [Functions](#functions) @@ -26,12 +28,41 @@ - [Improve the Code](#improve-the-code) - [Type Hinting](#type-hinting) - [Misc](#misc) + - [Lambda](#lambda) - [Questions](#questions) - [Hello World](#hello-world-1) + - [Conditionals](#conditionals) + - [Loops](#loops-1) - [Classes](#classes-1) - [Strings](#strings-1) - [Lists](#lists) - + - [Python - OOP](#python---oop) + - [Python - Exceptions](#python---exceptions) + - [Python Built-in functions](#python-built-in-functions) + - [Properties](#properties) + - [Python - Lists](#python---lists) + - [Python - Dictionaries](#python---dictionaries) + - [Common Algorithms Implementation](#common-algorithms-implementation) + - [Python Files](#python-files) + - [Python OS](#python-os) + - [Python Regex](#python-regex) + - [Python Strings](#python-strings) + - [Python Iterators](#python-iterators) + - [Python Misc](#python-misc) + - [Python - Slicing](#python---slicing) + - [Python Debugging](#python-debugging) + - [Python - Linked List](#python---linked-list) + - [Python - Stack](#python---stack) + - [Python Testing](#python-testing) + - [Flask](#flask) + +# Free App for Learning Python + +

+ + + +

# Exercises @@ -1613,333 +1644,4 @@ def home(): return 'main website' app.add_url_rule('/', view_func=home) -``` - - - -
-What is a blueprint in Flask?
-
- -
-What is a template?
-
- -#### zip - -
-Given x = [1, 2, 3], what is the result of list(zip(x))?
- -``` -[(1,), (2,), (3,)] -``` -
- -
-What is the result of each of the following: - -``` -list(zip(range(5), range(50), range(50))) -list(zip(range(5), range(50), range(-2))) -``` -
- -``` -[(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)] -[] -``` -
- -#### Python Descriptors - -
-Explain Descriptors
- -Read about descriptors [here](https://docs.python.org/3/howto/descriptor.html) -
- -
-What would be the result of running a.num2 assuming the following code - -``` -class B: - def __get__(self, obj, objtype=None): - reuturn 10 - -class A: - num1 = 2 - num2 = Five() -``` -
-10 -
- -
-What would be the result of running some_car = Car("Red", 4) assuming the following code - -``` -class Print: - - def __get__(self, obj, objtype=None): - value = obj._color - print("Color was set to {}".format(valie)) - return value - - def __set__(self, obj, value): - print("The color of the car is {}".format(value)) - obj._color = value - -class Car: - - color = Print() - - def __ini__(self, color, age): - self.color = color - self.age = age -``` -
-An instance of Car class will be created and the following will be printed: "The color of the car is Red" -
- -#### Python Misc - -
-How can you spawn multiple processes with Python?
-
- -
-Implement simple calculator for two numbers
- -``` -def add(num1, num2): - return num1 + num2 - - -def sub(num1, num2): - return num1 - num2 - - -def mul(num1, num2): - return num1*num2 - - -def div(num1, num2): - return num1 / num2 - -operators = { - '+': add, - '-': sub, - '*': mul, - '/': div -} - -if __name__ == '__main__': - operator = str(input("Operator: ")) - num1 = int(input("1st number: ")) - num2 = int(input("2nd number: ")) - print(operators[operator](num1, num2)) -``` -
- -
-What data types are you familiar with that are not Python built-in types but still provided by modules which are part of the standard library?
- -This is a good reference https://docs.python.org/3/library/datatypes.html -
- -
-Explain what is a decorator
- -In python, everything is an object, even functions themselves. Therefore you could pass functions as arguments -for another function eg; - -``` -def wee(word): - return word - -def oh(f): - return f + "Ohh" - ->>> oh(wee("Wee")) -<<< Wee Ohh -``` - -This allows us to control the before execution of any given function and if we added another function as wrapper, -(a function receiving another function that receives a function as parameter) we could also control the after execution. - -Sometimes we want to control the before-after execution of many functions and it would get tedious to write - - f = function(function_1()) - f = function(function_1(function_2(*args))) - -every time, that's what decorators do, they introduce syntax to write all of this on the go, using the keyword '@'. - -
- -
-Can you show how to write and use decorators?
- - -These two decorators (ntimes and timer) are usually used to display decorators functionalities, you can find them in lots of -tutorials/reviews. I first saw these examples two years ago in pyData 2017. https://www.youtube.com/watch?v=7lmCu8wz8ro&t=3731s - -``` -Simple decorator: - -def deco(f): - print(f"Hi I am the {f.__name__}() function!") - return f - -@deco -def hello_world(): - return "Hi, I'm in!" - -a = hello_world() -print(a) - ->>> Hi I am the hello_world() function! - Hi, I'm in! -``` - -This is the simplest decorator version, it basically saves us from writting a = deco(hello_world()). -But at this point we can only control the before execution, let's take on the after: - -``` -def deco(f): - def wrapper(*args, **kwargs): - print("Rick Sanchez!") - func = f(*args, **kwargs) - print("I'm in!") - return func - return wrapper - -@deco -def f(word): - print(word) - -a = f("************") ->>> Rick Sanchez! - ************ - I'm in! -``` - -deco receives a function -> f -wrapper receives the arguments -> *args, **kwargs - -wrapper returns the function plus the arguments -> f(*args, **kwargs) -deco returns wrapper. - -As you can see we conveniently do things before and after the execution of a given function. - -For example, we could write a decorator that calculates the execution time of a function. - -``` -import time -def deco(f): - def wrapper(*args, **kwargs): - before = time.time() - func = f(*args, **kwargs) - after = time.time() - print(after-before) - return func - return wrapper - -@deco -def f(): - time.sleep(2) - print("************") - -a = f() ->>> 2.0008859634399414 -``` - -Or create a decorator that executes a function n times. - -``` -def n_times(n): - def wrapper(f): - def inner(*args, **kwargs): - for _ in range(n): - func = f(*args, **kwargs) - return func - return inner - return wrapper - -@n_times(4) -def f(): - print("************") - -a = f() - ->>>************ - ************ - ************ - ************ -``` - -
- -
-Write a decorator that calculates the execution time of a function
-
- -
-Write a script which will determine if a given host is accessible on a given port
-
- -
-Are you familiar with Dataclasses? Can you explain what are they used for?
-
- -
-You wrote a class to represent a car. How would you compare two cars instances if two cars are equal if they have the same model and color?
- -``` -class Car: - def __init__(self, model, color): - self.model = model - self.color = color - - def __eq__(self, other): - if not isinstance(other, Car): - return NotImplemented - return self.model == other.model and self.color == other.color - ->> a = Car('model_1', 'red') ->> b = Car('model_2', 'green') ->> c = Car('model_1', 'red') ->> a == b -False ->> a == c -True -``` -
- -
-Explain Context Manager
-
- -
-Tell me everything you know about concurrency in Python
-
- -
-Explain the Buffer Protocol
-
- -
-Do you have experience with web scraping? Can you describe what have you used and for what?
-
- -
-Can you implement Linux's tail command in Python? Bonus: implement head as well
-
- -
-You have created a web page where a user can upload a document. But the function which reads the uploaded files, runs for a long time, based on the document size and user has to wait for the read operation to complete before he/she can continue using the web site. How can you overcome this?
-
- -
-How yield works exactly?
-
- +``` \ No newline at end of file diff --git a/images/python_hero.png b/images/python_hero.png new file mode 100644 index 0000000..16e3511 Binary files /dev/null and b/images/python_hero.png differ