{"id":19789459,"url":"https://github.com/jdeepd/python-4-beginners","last_synced_at":"2025-05-01T01:31:33.088Z","repository":{"id":43484519,"uuid":"280180903","full_name":"JDeepD/Python-4-Beginners","owner":"JDeepD","description":"Add your valuable , short python snippets here .","archived":false,"fork":false,"pushed_at":"2022-02-28T09:16:54.000Z","size":107,"stargazers_count":2,"open_issues_count":1,"forks_count":6,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-06T06:41:59.633Z","etag":null,"topics":["beginner-code","beginner-friendly","hacktoberfest","python","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JDeepD.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-16T14:50:44.000Z","updated_at":"2023-05-27T16:27:18.000Z","dependencies_parsed_at":"2022-08-23T11:21:39.748Z","dependency_job_id":null,"html_url":"https://github.com/JDeepD/Python-4-Beginners","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JDeepD%2FPython-4-Beginners","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JDeepD%2FPython-4-Beginners/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JDeepD%2FPython-4-Beginners/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JDeepD%2FPython-4-Beginners/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JDeepD","download_url":"https://codeload.github.com/JDeepD/Python-4-Beginners/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251808504,"owners_count":21647298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["beginner-code","beginner-friendly","hacktoberfest","python","python3"],"created_at":"2024-11-12T06:31:58.186Z","updated_at":"2025-05-01T01:31:32.698Z","avatar_url":"https://github.com/JDeepD.png","language":"Python","readme":"## Welcome to Python4Beginners.This repository will have codes on Homework problems in computer science. It will mainly be based on Python 3.6+.\n![GitHub](https://img.shields.io/github/license/Nova-Striker/Python-4-Beginners)\n![GitHub contributors](https://img.shields.io/github/contributors/Nova-Striker/Python-4-Beginners)\n![GitHub last commit](https://img.shields.io/github/last-commit/Nova-Striker/Python-4-Beginners)\n![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed/Nova-Striker/Python-4-Beginners)\n![GitHub issues](https://img.shields.io/github/issues/Nova-Striker/Python-4-Beginners)\n\n\n\n\n## How to become a better Pythonista : A handy documentation of python features\n\n##### 1. List comprehensions \n\n* These are probably the most useful yet under rated features of python. They help you populate a list from  `loops` with optional `conditionals`\n##### Syntax :\n```python3\nlst = [ \u003cloop\u003e \u003ccondition\u003e ]\n```\n##### Example\n```python3\nlst = [i for i in range(10) ] \nprint(lst)\n#Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n# With condtitions\n\nlst = [ i for i in range(20) if i%2 == 0 ]    #populates only those numbers between 1 and 20 which is divisible by 2.\nprint(lst)\n#Output : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n\n ```\n --------------------------\n### 2. Taking an unlimited number of parameters : Enters `*args` and `**kwargs` [![PyPI pyversions](https://img.shields.io/pypi/pyversions/ansicolortags.svg)](https://pypi.python.org/pypi/ansicolortags/)\n[Official Documentation](https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments)\n#### A bit of background : `*args` and `**kwargs` basically stands for Arguments and Keyword Arguments respectively.\n#### Usecase : Remember a scenario when you want to pass an unknown number of arguments into a defined function. One could just make an arbitrary number of default parameters using `k1=None , k2=None` and so on. But that is tedious.\n\n##### Enters `*args` and `**kwargs`\n\nSuppose a case Scenario where you just want to input numbers in a function which returns the square of the number.\nA general solution would be :\n```python3\ndef sqr(n):\n    print(sqr**2)\n```\nWhile this function works , it can only take one parameter at one time.\nExample :\n```python3--\n\u003e\u003e\u003esqr(3)\n\u003e\u003e\u003e9\n```\nBut if we give two arguments :\n```python3\n\u003e\u003e\u003esqr(3,4)\n\u003e\u003e\u003eTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: sqr() takes 1 positional argument but 2 were given\n```\nWe get an error which prompts us that we can't give more than one arguments\n\nLets see how `*args` simplifies this for us.\n\n```python3\ndef sqr(*args):\n    for i in args:\n      print(i**2)  \n```\n\nNow , we can give an arbitrary number of arguments in the function , and it will always print out the square of those numbers\n```python3\n\u003e\u003e\u003esqr(2)\n4\n\u003e\u003e\u003esqr(2,3,4)\n4\n9\n16\n```\n**Side notes : It is not mandatory that you pass the parameter as `*args` only. For example , you can put `*op` also instead of `*args` in the previous function , and it would work the same. What's important is the `*` single asterisk  that you give before the parameter name which tells python that its an optional argument in the form of a tuple**\n\n### Now , suppose a scenario where you want to input the details of person and those details are printed out. You can use `*args` for this scenario also but it would not give us meaningful results.\n\n#### Example :\n```python3\ndef take_details(*args):\n    for i in args:\n        print(i)\n```\n```python3\n\u003e\u003e\u003etake_details(\"XYZ\" , 21 , \"male\" , \"India\")\nXYZ\n21\nmale\nIndia\n```\nBut this detail is not meaningful as we really dont know what XYZ or 21 or male means . Does it mean the person's parent's name is XYZ or person's name is XYZ. What is 21 ? Is it the person's age or roll number in a school?\n\n#### Enters : `**kwargs`\n\nWith `**kwargs` , you can not only pass parameters , you can also pass a key which defines that parameter.\n#### Example :\n```python3\ndef take_details(**kwargs):\n    for i , j in kwargs.items():\n        print(i , j)\n```\n```python3\n\u003e\u003e\u003etake_details(name=\"XYZ\" , age=\"21\" , gender=\"male\" , Nationality=\"India\")\nname XYZ\nage 21\ngender male\nNationality India\n```\nAnd thus we get pretty neatly the required arguments along with their keys.\n\n#### Sidenotes : `**kwargs` are taken in as dictionaries. So many dictionary functions like `dict.items()` work with `kwargs` too.\n----------------------------------\n### 3. Data Structures : Something that stores something more than mere data ! [![PyPI pyversions](https://img.shields.io/pypi/pyversions/ansicolortags.svg)](https://pypi.python.org/pypi/ansicolortags/)\n\n##### When we talk about data structures , what comes into out mind are lists , tuples  , dictionaries etc. And we think that these are meant to store only abstract values like : integers, strings , booleans , etc. But they can store function calls too ! \n\n##### Take this for example :\n```python3\ndef sqr(n):\n    print(n**2)\n\ndef cube(n):\n    print(n**3)\n    \ndef quad(n):\n    print(n**4)\n\n func_dic = {           #Remember not to give function calls in parenthesis or quotes. Otherwise , it would throw error. \n'square' : sqr,\n'cube' : cube,\n'quadrapule' : quad \n}\n\n#We can call these funtions from the dictionary itself !\n\u003e\u003e\u003efunc_dic['square'](4)\n16\n\u003e\u003e\u003efunc_dic['cube'](3)\n27\n\u003e\u003e\u003efunc_dic['quadrapule'](2)\n16\n```\n#### This can be done with not only dictionaries but with lists and tuples too !\n\n##### Example :\n\n```python3\ndef sqr(n):\n    print(n**2)\n\ndef cube(n):\n    print(n**3)\n    \ndef quad(n):\n    print(n**4)\n    \nfunc_ls = [sqr , cube , quad]\n```\n```python3\n\u003e\u003e\u003efunc_ls[0](4)\n16\n\u003e\u003e\u003efunc_ls[1](3)\n27\n\u003e\u003e\u003efunc_ls[2](2)\n16\n```\n-----------------------------------\n### 4. Python String Formatting [![PyPI pyversions](https://img.shields.io/pypi/pyversions/ansicolortags.svg)](https://pypi.python.org/pypi/ansicolortags/)\n\n##### Sometimes , when formatting strings , a minor mistake like a missing `,` (comma) or `\"` can cause the entire script to break and at times , its very difficult when implementing such string formats in a big project.\n\n#### i) Newbie Method (Sometimes it is the best method to use when other methods just make the code longer)\n\n```python3\ni = 10\nk = 20\nr = 30\nst = \"i is equal to\" + str(i) + \"k is equal to\" + str(k) + \"r is equal to\" + str(r)\nprint(st)\n```\n```python3\n\u003e\u003e\u003ei is equal to 10 k is equal to 20 r is equal to 30\n```\n#### ii) Format Method (Its a good way to format strings too. The best thing about this is that its supported in python 2.6+ also)\n\n```python3\ni = 10\nk = 20\nr = 30\nst = \"i is equal to %s k is equal to %s r is equal to %s\".format(i,k,r)\nprint(st)\n```\n```python3\n\u003e\u003e\u003ei is equal to 10 k is equal to 20 r is equal to 30\n```\n#### ii) Python 3.6+ String Interpolation (You will get addicted to it once you use it)\n\n```python3\ni = 10\nk = 20\nr = 30\nst = f\"i is equal to {i} k is equal to {k} r is equal to {r}\"    \n\n#Notice the `f` before the string. It tells the python to replace everything inside `{}` with user defined variables \nprint(st)\n```\n```python3\n\u003e\u003e\u003ei is equal to 10 k is equal to 20 r is equal to 30\n```\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdeepd%2Fpython-4-beginners","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdeepd%2Fpython-4-beginners","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdeepd%2Fpython-4-beginners/lists"}