diff --git a/README.md b/README.md index b6bbd52..cb102d0 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,27 @@ # Playground repo for Python projects -This repo has multiple branches and each branch represent different projects/proof of concepts for playing with Python technologies/frameworks. - -_P.S. `master` branch is empty and has only this `README.md` file_ +This is a learning repo with different projects/proof of concepts for playing with Python technologies/frameworks. ## Table of Contents - [Status Updates](#status-updates) + - [Day 1](#day-1) + - [Day 2](#day-2) + - [Day 3](#day-3) + - [Day 4](#day-4) + - [Day 5](#day-5) + - [Day 6](#day-6) + - [Day 7](#day-7) + - [Day 8](#day-8) + - [Day 10](#day-10) + - [Day 12](#day-12) + - [Day 13](#day-13) - [Installation](#installation) - [Resources](#resources) ## Status Updates -- [learn/pipenv-pipfile - Pipenv and Pipfile](https://github.com/TechPrimers/python-playground/tree/learn/pipenv-pipfile) - - Day 1 - March 25th, 2020 - ## Day 1 +As part of 21 days of lockdown, I started learning python from March 25th, 2020. + +### Day 1 +- `cd pipenv` - `pipenv install` - Similar to `npm install`. Creates 2 files `Pipfile` and `Pipfile.lock` similar to `package-lock.json` in the javascript world with dependencies based on `requirements.txt` - `pipenv shell` - Activates the virtual environment. Much simpler than `virtualenv` - Once the shell is activated, run `python3 hello.py` to bring the Flask app UP!! @@ -34,19 +44,41 @@ _P.S. `master` branch is empty and has only this `README.md` file_ - If a package has `__init__.py`, it becomes a module - `learn` is a module which is used in `modules.py` +### Day 2 + - cd `functions` + - Started using callable functions inside a class + +### Day 3 + - Started using Functions and String manipulation + +### Day 4 + - `cd tuples` + - Started using `tuples` + +### Day 5 + - `cd dictionaries` + - Started using `dictionaries` + - Started using `set` + +### Day 6 + - Started with class usage and best practices + +### Day 7 + - class methods + - static methods + +### Day 8 + - class equals and repr + - Data Class usage + +### Day 10 + - Files and Directories + +### Day 12 + - Lambda -- [learn/functions - Functions](https://github.com/TechPrimers/python-playground/tree/learn/functions) - - Day 2 - - Started using callable functions inside a class - - Day 3 - - Started using Functions and String manipulation -- [learn/tuples - Tuples](https://github.com/TechPrimers/python-playground/tree/learn/tuples) - - Day 4 - - Started using `tuples` -- [learn/dictionaries - Dictionaries](https://github.com/TechPrimers/python-playground/tree/learn/dictionaries) - - Day 5 - - Started using `dictionaries` - - Started using `set` +### Day 13 + - Solving LeetCode program - [Problem](https://leetcode.com/problems/two-sum/) ## Installation Command used in Mac for installing supporting tools @@ -55,3 +87,4 @@ Command used in Mac for installing supporting tools ## Resources - [Introducing Python - Oreilly](https://learning.oreilly.com/library/view/introducing-python-2nd) - Most exhaustive and amazing book..! +- [Projects to build after learning pythong](https://medium.com/javarevisited/8-projects-you-can-buil-to-learn-python-in-2020-251dd5350d56) diff --git a/classes/ClassMethod.py b/classes/ClassMethod.py new file mode 100644 index 0000000..f9e30d5 --- /dev/null +++ b/classes/ClassMethod.py @@ -0,0 +1,15 @@ +class ClassMethod: + count = 0 + def __init__(self): + ClassMethod.count += 1 + + @classmethod + def print(cls): + print(f"Number of objects: {cls.count}") + + @staticmethod + def printLog(): + print("Static Method: ", ClassMethod.count) + + def normal(self): + print("normal") \ No newline at end of file diff --git a/classes/DataClass.py b/classes/DataClass.py new file mode 100644 index 0000000..2ff49af --- /dev/null +++ b/classes/DataClass.py @@ -0,0 +1,6 @@ +from dataclasses import dataclass + +@dataclass +class DataClass: + name: str + age: int = 0 \ No newline at end of file diff --git a/classes/User.py b/classes/User.py new file mode 100644 index 0000000..ffccd98 --- /dev/null +++ b/classes/User.py @@ -0,0 +1,13 @@ +class User: + + def __init__(self, input_name): + self.private_name = input_name + + def get_name(self): + return self.private_name; + + def set_name(self, input_name): + self.private_name = input_name + + name = property(get_name, set_name) + diff --git a/classes/UserAnnotation.py b/classes/UserAnnotation.py new file mode 100644 index 0000000..c9d83c4 --- /dev/null +++ b/classes/UserAnnotation.py @@ -0,0 +1,13 @@ +class UserAnnotation: + + def __init__(self, input_name): + self.private_name = input_name + + @property + def name(self): + return self.private_name; + + @name.setter + def name(self, input_name): + self.private_name = input_name + diff --git a/classes/Word.py b/classes/Word.py new file mode 100644 index 0000000..59ae6c2 --- /dev/null +++ b/classes/Word.py @@ -0,0 +1,10 @@ +class Word: + + def __init__(self, text): + self.text = text + + def __eq__(self, new_word): + return self.text.lower() == new_word.text.lower() + + def __repr__(self): + return f"(Word: {self.text})" \ No newline at end of file diff --git a/classes/class_main.py b/classes/class_main.py new file mode 100644 index 0000000..73efab2 --- /dev/null +++ b/classes/class_main.py @@ -0,0 +1,27 @@ +from User import User +from UserAnnotation import UserAnnotation +from ClassMethod import ClassMethod + +user = User("ajay") +user_annotation = UserAnnotation("ajay") +print(user.get_name()) +user.set_name("raj") + +print(user.private_name) +# Since property() is set via name. we can use name instead of private_name +user.name = "jaga" +print(user.name) +print("User annotation:") +print(user_annotation.private_name) +print(user_annotation.name) +user_annotation.name = "jaga" +print(user_annotation.name) + +ClassMethod.printLog() + +cm = ClassMethod() +cm_a = ClassMethod() +cm_b = ClassMethod() +cm.print() +cm.normal() +ClassMethod.printLog() \ No newline at end of file diff --git a/classes/dataclass_main.py b/classes/dataclass_main.py new file mode 100644 index 0000000..faff4be --- /dev/null +++ b/classes/dataclass_main.py @@ -0,0 +1,6 @@ +from DataClass import DataClass + +data_class = DataClass("ajay", 19) + +print("Name: ", data_class.name) +print("Age: ", data_class.age) \ No newline at end of file diff --git a/classes/word_main.py b/classes/word_main.py new file mode 100644 index 0000000..8650fac --- /dev/null +++ b/classes/word_main.py @@ -0,0 +1,9 @@ +from Word import Word + +word_a = Word("a") +word_b = Word("b") +word_c = Word("b") + +print("A equals B: ", word_a == word_b) +print("B equals C: ", word_b == word_c) +print("A: ", word_a) \ No newline at end of file diff --git a/dictionaries/dictionaries.py b/dictionaries/dictionaries.py new file mode 100644 index 0000000..ceca68c --- /dev/null +++ b/dictionaries/dictionaries.py @@ -0,0 +1,22 @@ +# Order doesn't matter like HashMap +dictionary = {"name": "ajay", "location": "chennai"} + +for item in dictionary: + print(f"key[{item}] value [{dictionary[item]}]in dictionary") + print(f"Another representation: key[{item}] value [{dictionary.get(item)}]in dictionary") + +print("Only keys: ", dictionary.keys()) + +dictionary_keys = list(dictionary.keys()) +print("Keys: ", dictionary_keys) +print("Keys (type): ", type(dictionary_keys)) + + +dictionary_values = list(dictionary.values()) +print("Values: ", dictionary_values) +print("Values (type): ", type(dictionary_values)) + + +dictionary_list_items = list(dictionary.items()) +print("Items: ", dictionary_list_items) +print("Items (type): ", type(dictionary_list_items)) \ No newline at end of file diff --git a/dictionaries/sets.py b/dictionaries/sets.py new file mode 100644 index 0000000..fc305d4 --- /dev/null +++ b/dictionaries/sets.py @@ -0,0 +1,12 @@ +# Set has similar notation to dictionaries with {} however they have only keys +# elements in a set are unique compared to lists +sets = {"z","a", "b", "c"} +print("Type: ", type(sets)) +print("Pop: ", sets.pop()) +print("Sorted Set(list): ", sorted(sets)) + +list_var = ["a", "a", "z", "d", "c", "z"] +print("Sorted List: ", sorted(list_var)) +print("Sorted in reverse: ", sorted(list_var, reverse = True)) + +print("Remove duplicates from list: ", sorted(list(set(list_var)))) diff --git a/files/files.py b/files/files.py new file mode 100644 index 0000000..a955a4f --- /dev/null +++ b/files/files.py @@ -0,0 +1,31 @@ +import os + +file_obj = open("files.txt", "wt") +print("This is a temp file", file=file_obj) +file_obj.write("Second line") +file_obj.close() + +try: + fout = open('files.txt', 'xt') + fout.write('added') +except FileExistsError: + print('files.txt already exists!') + +file_obj = open('files.txt', 'rt') +while True: + line = file_obj.readline(); + if not line: + break; + print("Line: ", line) +file_obj.close() + +file_obj = open('files.txt', 'rt') +lines = file_obj.readlines(); +print("Lines: ", lines) +file_obj.close() +for line in lines: + print("Line...", line) + +print(os.path.isfile('files.txt')) +print(os.path.isfile('files.temp')) +print(os.path.exists('files.txt')) \ No newline at end of file diff --git a/files/files.txt b/files/files.txt new file mode 100644 index 0000000..6c3bf22 --- /dev/null +++ b/files/files.txt @@ -0,0 +1,2 @@ +This is a temp file +Second line \ No newline at end of file diff --git a/functions/functions.py b/functions/functions.py new file mode 100644 index 0000000..e1c6c91 --- /dev/null +++ b/functions/functions.py @@ -0,0 +1,9 @@ +class Functions: + + def __init__(self): + self._cache = [] + + def __call__(self, name): + if name not in self._cache: + self._cache.append(name) + return self._cache \ No newline at end of file diff --git a/functions/functions_main.py b/functions/functions_main.py new file mode 100644 index 0000000..28f8973 --- /dev/null +++ b/functions/functions_main.py @@ -0,0 +1,4 @@ +from functions import Functions +func = Functions() +print(func("ajay")) +print(func("jaga")) \ No newline at end of file diff --git a/functions/listComprehension b/functions/listComprehension new file mode 100644 index 0000000..6c75ccf --- /dev/null +++ b/functions/listComprehension @@ -0,0 +1,35 @@ +input_list = [1, 2, 3, 4, 4, 5, 6, 7, 7] + +output_list = [] + +# Using loop for constructing output list +for var in input_list: + if var % 2 == 0: + output_list.append(var) + +print("Output List using for loop:", output_list) + + + +input_list = [1, 2, 3, 4, 4, 5, 6, 7, 7] + + +list_using_comp = [var for var in input_list if var % 2 == 0] + +print("Output List using list comprehensions:", + list_using_comp) + + + +output_list = [] +for var in range(1, 10): + output_list.append(var ** 2) + +print("Output List using for loop:", output_list) + + + +list_using_comp = [var**2 for var in range(1, 10)] + +print("Output List using list comprehension:", + list_using_comp) diff --git a/functions/strings.py b/functions/strings.py new file mode 100644 index 0000000..bc5627b --- /dev/null +++ b/functions/strings.py @@ -0,0 +1,38 @@ +# Strings are immutable. None of the changes affect the original variable unless assigned +import string + +extractor="Hello World" +print("Full String: ", extractor[:]) + +print("Offset from 3: ", extractor[3:]) +print("Offset from :3 ", extractor[:3]) +print("Offset from 3:8 ", extractor[3:8]) + +print("Last 3 characters: ", extractor[-3:]) +print("Last 3 characters: ", extractor[4:-3]) + +print("By space: ", extractor.split(" ")) + +strip_variable = "This has punctuations ..!!" +print("Strip Punctuations: ", strip_variable.strip(string.punctuation)) +print("Strip White spaces: ", strip_variable.strip(string.whitespace)) +print("Strip Punctuations and White spaces: ", strip_variable.strip(string.whitespace + string.punctuation)) + +print("Find the: ", strip_variable.find("the")) +print("rFind the: ", strip_variable.rfind("the")) +print("Find has: ", strip_variable.find("has")) + +# Returns substring not found error unlike -1 in find +# print("Index the: ", strip_variable.index("the")) +# print("rIndex the: ", strip_variable.rindex("the")) + + +count_variable = "This has repeated texts for counting texts" +print("Count: ", count_variable.count("text")) + +alpha_variable = "Thishasnumbersandtexts2" +print("alpha_variable isAlphanumberic: ", alpha_variable.isalnum()) +print("count_variable isAlphanumberic: ", count_variable.isalnum()) + +title = "This is a title" +print("Title: ", title.title()) diff --git a/lambda/lambda_main.py b/lambda/lambda_main.py new file mode 100644 index 0000000..cbd9216 --- /dev/null +++ b/lambda/lambda_main.py @@ -0,0 +1,2 @@ +name_lambda = lambda name: name.split()[-1] +print("Name: ", name_lambda("ajay")) diff --git a/learn/__pycache__/__init__.cpython-37.pyc b/learn/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 4e2b0ba..0000000 Binary files a/learn/__pycache__/__init__.cpython-37.pyc and /dev/null differ diff --git a/learn/__pycache__/learn.cpython-37.pyc b/learn/__pycache__/learn.cpython-37.pyc deleted file mode 100644 index 062fe9d..0000000 Binary files a/learn/__pycache__/learn.cpython-37.pyc and /dev/null differ diff --git a/leetcode/Solution.py b/leetcode/Solution.py new file mode 100644 index 0000000..831268d --- /dev/null +++ b/leetcode/Solution.py @@ -0,0 +1,17 @@ +from typing import List +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for j in range(0, len(nums)): + for i in range(0, len(nums)): + if (i!=j): + sumOf=nums[j] + nums[i] + if (sumOf == target): + return j, i +# Enumeration is faster than this +# def twoSum(self, nums, target): +# a ={} +# for i, num in enumerate(nums): +# if target-num in a: +# return [a[target - num], i] +# else: +# a[num] = i \ No newline at end of file diff --git a/leetcode/twosum.py b/leetcode/twosum.py new file mode 100644 index 0000000..afeac06 --- /dev/null +++ b/leetcode/twosum.py @@ -0,0 +1,4 @@ +from Solution import Solution + +sol = Solution() +print(sol.twoSum([11, 7,22, 2], 9)) diff --git a/Pipfile b/pipenv/Pipfile similarity index 100% rename from Pipfile rename to pipenv/Pipfile diff --git a/Pipfile.lock b/pipenv/Pipfile.lock similarity index 100% rename from Pipfile.lock rename to pipenv/Pipfile.lock diff --git a/basics.py b/pipenv/basics.py similarity index 100% rename from basics.py rename to pipenv/basics.py diff --git a/example.pdf b/pipenv/example.pdf similarity index 100% rename from example.pdf rename to pipenv/example.pdf diff --git a/hello.py b/pipenv/hello.py similarity index 100% rename from hello.py rename to pipenv/hello.py diff --git a/learn/__init__.py b/pipenv/learn/__init__.py similarity index 100% rename from learn/__init__.py rename to pipenv/learn/__init__.py diff --git a/learn/learn.py b/pipenv/learn/learn.py similarity index 100% rename from learn/learn.py rename to pipenv/learn/learn.py diff --git a/modules.py b/pipenv/modules.py similarity index 100% rename from modules.py rename to pipenv/modules.py diff --git a/pdf_reader.py b/pipenv/pdf_reader.py similarity index 100% rename from pdf_reader.py rename to pipenv/pdf_reader.py diff --git a/requirements.txt b/pipenv/requirements.txt similarity index 100% rename from requirements.txt rename to pipenv/requirements.txt diff --git a/tuples/tuples.py b/tuples/tuples.py new file mode 100644 index 0000000..5e272cd --- /dev/null +++ b/tuples/tuples.py @@ -0,0 +1,38 @@ +tuple_variable = () +print("Empty tuple: ", tuple_variable) + +single_tuple = ("single",) +print("Single tuple: ", single_tuple) +print("Single tuple(type): ", type(single_tuple)) + + +str_tuple = ("single") # Without comma, tuples become str +print("Not a tuple: ", str_tuple) +print("Not a tuple(type): ", type(str_tuple)) + + +declar_tuple = ("one", "two", "three") +print("Declaring tuples: ", declar_tuple) + +declar_tuple = "one", "two", "three" # Another way of declaring tuples +print("Declaring tuples: ", declar_tuple) + +assign_tuple1, assign_tuple2, assign_tuple3 = ("one", "two", "three") +print("Assigned tuple1: ", assign_tuple1) +print("Assigned tuple2: ", assign_tuple2) +print("Assigned tuple3: ", assign_tuple3) + +#Flip variables using tuples without using a temporary variable +assign_tuple1, assign_tuple2 = assign_tuple2, assign_tuple1 +print("Swapped tuple1: ", assign_tuple1) +print("Swapped tuple2: ", assign_tuple2) + +#List to tuple +list_tuple = tuple(["one", "two", "three"]) +print("List tuple: ", list_tuple) +print("List tuple(type): ", type(list_tuple)) + +for tup in list_tuple: + print("items: ", tup) + +print("List: ", list("test")) \ No newline at end of file