diff --git a/advanced/recursion_fibonacci_memoization.py b/advanced/recursion_fibonacci_memoization.py index d489eb6..65f322f 100644 --- a/advanced/recursion_fibonacci_memoization.py +++ b/advanced/recursion_fibonacci_memoization.py @@ -31,15 +31,12 @@ def fibonacci(n): raise TypeError("n must be a positive int") if n < 0: raise ValueError("n must be a positive int or zero") - + # Compute the n-th term if n == 0: # Position 0 return 0 - if n == 1: - return 1 - else: - return fibonacci(n - 1) + fibonacci(n - 2) + return 1 if n == 1 else fibonacci(n - 1) + fibonacci(n - 2) # Uncomment the lines below to test example @@ -101,23 +98,18 @@ def fibonacci_(n): """ @lru_cache(maxsize = 1000) -# Now we will use the lru_cache from functools module. LRU means Least Recently Used Cache -# Look the following link for higher values of maxsize, https://stackoverflow.com/questions/5061582/setting-stacksize-in-a-python-script/16248113#16248113 def fibonacci_lru(n): # Check that the input is a positive integer if (type(n) != int): raise TypeError("n must be a positive int") if n < 0: raise ValueError("n must be a positive int or zero") - + # Compute the n-th term if n == 0: # Position 0 return 0 - if n == 1: - return 1 - else: - return fibonacci_lru(n - 1) + fibonacci_lru(n - 2) + return 1 if n == 1 else fibonacci_lru(n - 1) + fibonacci_lru(n - 2) # Uncomment the lines below to test example """ diff --git a/basics/arithmetic.py b/basics/arithmetic.py index a25d484..4174624 100644 --- a/basics/arithmetic.py +++ b/basics/arithmetic.py @@ -8,6 +8,7 @@ Based on Socratica videos ''' + # Operations: + - * / x = 28 @@ -19,7 +20,7 @@ Z = 2.00 print(type(Z)) -Z = Z + 0.32j +Z += 0.32j print(type(Z)) # The error below occurs because floats are narrower than complex numbers diff --git a/basics/if_then_else.py b/basics/if_then_else.py index b26d81b..61745a4 100644 --- a/basics/if_then_else.py +++ b/basics/if_then_else.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """""" + ''' File: if_then_else.py Project: basics @@ -41,8 +42,8 @@ if (a != b) and (b != c) and (a != c): - print("This is a scalene triangle.") -elif (a == b) and (b == c): + print("This is a scalene triangle.") +elif a == b == c: print("This is an equilateral triangle.") else: print("This is an isosceles triangle.") diff --git a/basics/lists_.py b/basics/lists_.py index 3ced9e2..bae7235 100644 --- a/basics/lists_.py +++ b/basics/lists_.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """""" + ''' File: lists_.py Project: basics @@ -19,7 +20,7 @@ ''' """""" # A simple list definition -example = list() +example = [] print(example) print(type(example)) # Another simple definition @@ -67,9 +68,7 @@ letters.sort() print(letters) -# To add more items -primes.append(17) -primes.append(19) +primes.extend((17, 19)) print(primes) # To remove items diff --git a/basics/sets_.py b/basics/sets_.py index 94ae8b5..0a0fdfe 100644 --- a/basics/sets_.py +++ b/basics/sets_.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """""" + ''' File: sets_.py Project: basics @@ -49,7 +50,7 @@ example.discard(42) print(example) # We can start a set starts from a list -example2 = set([1, True, 'Europe', 2.71828]) +example2 = {1, True, 'Europe', 2.71828} print(len(example2)) # Let's clear our set @@ -58,11 +59,11 @@ # If we have 2 sets, the combination of all elements from the two sets is denoted with Union # The intersection is the set of elements inside both A and B sets -odds = set([1, 3, 5, 7, 9]) -evens = set([2, 4, 6, 8, 10]) -primes = set([2, 3, 5, 7]) +odds = {1, 3, 5, 7, 9} +evens = {2, 4, 6, 8, 10} +primes = {2, 3, 5, 7} # composite integers - the integers which can be factored -composites = set([4, 6, 8, 9, 10]) +composites = {4, 6, 8, 9, 10} print(odds.union(evens)) print(evens.union(odds)) diff --git a/basics/text_files.py b/basics/text_files.py index 676535e..7ba94f1 100644 --- a/basics/text_files.py +++ b/basics/text_files.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """""" + ''' File: text_files.py Project: basics @@ -17,16 +18,8 @@ ---------- --- --------------------------------------------------------- ''' """""" -# There are two types of files: -# 1 - Text files: plain text, xml, json, souce code, etc. -# 2 - Binary files: compiled code, app data, media files(audio, video) -# print(help(open)) - -# The simple way to open a file. It's a dangerous way. -f = open("guido_bio.txt") -text = f.read() -f.close() - +with open("guido_bio.txt") as f: + text = f.read() # The code below is more security than above. When we uses open method # we don't need to close the file. with open("guido_bio.txt") as fobj: diff --git a/basics/tuples_.py b/basics/tuples_.py index b7ed480..af11ba4 100644 --- a/basics/tuples_.py +++ b/basics/tuples_.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """""" + ''' File: tuples_.py Project: basics @@ -35,17 +36,8 @@ TEST_SIZE = 100 -# Create a random tuple -list_int = [] -tuple_int = [random.randint(0, 1) for i in range(TEST_SIZE)] -# After this point the tuple can not be modify becayse it is immutable. -# Lists are mutable - -# Create a random list. -for i in range(TEST_SIZE): - list_int.append(random.randint(0, 1)) - # After this point we can modify the list. Lists are mutable. - +tuple_int = [random.randint(0, 1) for _ in range(TEST_SIZE)] +list_int = [random.randint(0, 1) for _ in range(TEST_SIZE)] print("LIST\t\tTUPLE") for i in range(TEST_SIZE): # We can iterate lists and tuples with a simple for @@ -121,7 +113,7 @@ # Let's try slicing a tuple my_tuple = ('m', 'y', 'n', 'a', 'm', 'e', 'i', 's') -print(my_tuple[0:4]) # The last is not inclusive +print(my_tuple[:4]) print(my_tuple[:]) # Begin to end print(my_tuple[:5]) # Begin to 5-1 print(my_tuple[:-2]) # Begin to end-2 diff --git a/intermediate/exceptions_.py b/intermediate/exceptions_.py index 1ae0a6e..88e8fba 100644 --- a/intermediate/exceptions_.py +++ b/intermediate/exceptions_.py @@ -43,8 +43,7 @@ def read_file_timed(path): start_time = time.time() try: f = open(path, mode="rb") - data = f.read() - return data + return f.read() except FileNotFoundError as err: logger.error(err) raise diff --git a/intermediate/random_numbers.py b/intermediate/random_numbers.py index a85705e..9749a30 100644 --- a/intermediate/random_numbers.py +++ b/intermediate/random_numbers.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """""" + ''' File: random_numbers.py Project: intermediate @@ -27,7 +28,7 @@ print(help(random.random)) # Display 10 random numbers between 0 and 1 -for i in range(10): +for _ in range(10): print(random.random()) # Generate random numbers from interval 3-7. Way #0 @@ -39,26 +40,26 @@ def my_random(): # Print 10 random numbers from call function my_random() -for i in range(10): +for _ in range(10): print(my_random()) # Generate random numbers from interval 3-7. Way #1 (uniform function) print(help(random.uniform)) -for i in range(10): +for _ in range(10): print(random.uniform(3, 7)) - + # Generate random numbers from a Gaussian Distribution print(help(random.normalvariate)) -for i in range(10): +for _ in range(10): print(random.normalvariate(0, 10)) # Generante random int numbers print(help(random.randint)) -for i in range(10): +for _ in range(10): print(random.randint(0, 9)) # Generate from a random from a list outcomes = ['rock', 'paper', 'scissor'] -for i in range(10): +for _ in range(10): print(random.choice(outcomes)) diff --git a/objects_and_class/0_basics_.py b/objects_and_class/0_basics_.py index e508dbf..69f5088 100644 --- a/objects_and_class/0_basics_.py +++ b/objects_and_class/0_basics_.py @@ -30,10 +30,10 @@ def __init__(self, name, age=0): # instance method def sing(self, song): - return "{} sings {}".format(self.name, song) + return f"{self.name} sings {song}" def dance(self): - return "{} is now dancing".format(self.name) + return f"{self.name} is now dancing" def main(): @@ -42,12 +42,12 @@ def main(): woo = Parrot("Woo", 15) # access the class attributes - print("Blu is a {}".format(blu.__class__.species)) - print("Woo is also a {}".format(woo.__class__.species)) + print(f"Blu is a {blu.__class__.species}") + print(f"Woo is also a {woo.__class__.species}") # access the instance attributes - print("{} is {} years old".format(blu.name, blu.age)) - print("{} is {} years old".format(woo.name, woo.age)) + print(f"{blu.name} is {blu.age} years old") + print(f"{woo.name} is {woo.age} years old") # call our instance methods print(blu.sing("'Happy'")) diff --git a/objects_and_class/1_encapsulation.py b/objects_and_class/1_encapsulation.py index 47332ae..cd553a1 100644 --- a/objects_and_class/1_encapsulation.py +++ b/objects_and_class/1_encapsulation.py @@ -25,7 +25,7 @@ def __init__(self): self.__maxprice = 900 def sell(self): - print("Selling Price: {}".format(self.__maxprice)) + print(f"Selling Price: {self.__maxprice}") def setMaxPrice(self, price): self.__maxprice = price diff --git a/objects_and_class/5_inheritance_advanced.py b/objects_and_class/5_inheritance_advanced.py index 9bb1533..4f9ea1f 100644 --- a/objects_and_class/5_inheritance_advanced.py +++ b/objects_and_class/5_inheritance_advanced.py @@ -33,11 +33,10 @@ class DerivedClass(BaseClass): class Polygon: def __init__(self, no_of_sides): self.n = no_of_sides - self.sides = [0 for i in range(no_of_sides)] + self.sides = [0 for _ in range(no_of_sides)] def inputSides(self): - self.sides = [float(input("Enter side "+str(i+1)+" : ")) - for i in range(self.n)] + self.sides = [float(input(f"Enter side {str(i+1)} : ")) for i in range(self.n)] def dispSides(self): for i in range(self.n):