{"id":21260478,"url":"https://github.com/tanmayvaij/python-syntax","last_synced_at":"2025-06-17T02:35:57.632Z","repository":{"id":153628646,"uuid":"554626940","full_name":"tanmayvaij/python-syntax","owner":"tanmayvaij","description":"Overview of the syntax of Python language ","archived":false,"fork":false,"pushed_at":"2022-10-20T15:26:05.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T06:44:42.323Z","etag":null,"topics":["python","python-syntax","python3","syntax"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tanmayvaij.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-20T05:46:02.000Z","updated_at":"2022-10-20T12:20:32.000Z","dependencies_parsed_at":"2023-05-28T19:00:17.487Z","dependency_job_id":null,"html_url":"https://github.com/tanmayvaij/python-syntax","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tanmayvaij/python-syntax","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanmayvaij%2Fpython-syntax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanmayvaij%2Fpython-syntax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanmayvaij%2Fpython-syntax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanmayvaij%2Fpython-syntax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tanmayvaij","download_url":"https://codeload.github.com/tanmayvaij/python-syntax/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanmayvaij%2Fpython-syntax/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260278362,"owners_count":22985270,"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":["python","python-syntax","python3","syntax"],"created_at":"2024-11-21T04:19:08.983Z","updated_at":"2025-06-17T02:35:57.611Z","avatar_url":"https://github.com/tanmayvaij.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Python Syntax\n\n### 1. Hello World\n```python\nprint(\"Hello World\")\n\nprint(\"1 first statement\", end=\" \")\nprint(\"1 second statement\")\n\nprint(\"2 first statement\", \"2 second statement\")\n\nprint(\"tan \\nis good\\t hi\")\nprint(\"\\\"\")\n```\n\u003cbr/\u003e\n\n### 2. Comments\n```python\n# This is a single line comment\n\n\"\"\" This is a \nmultiline comment \"\"\"\n```\n\u003cbr/\u003e\n\n### 3. Data Types\n```python\na = 1\nb = 6.7\nc = \"word\"\nd = True\ne = None\n\nprint(a, type(a))\nprint(b, type(b))\nprint(c, type(c))\nprint(d, type(d))\nprint(e, type(e))\n\nvar1 = 14\nvar2 = 76\nvar3 = \"45\"\nvar4 = \"105\"\nprint(var1 + var2)\nprint(var3 + var4)\nprint(str(14) + str(76))\nprint(10 * var3)\n\n\"\"\"\nOutput:- \n\n1 \u003cclass 'int'\u003e\n6.7 \u003cclass 'float'\u003e\nword \u003cclass 'str'\u003e\nTrue \u003cclass 'bool'\u003e\nNone \u003cclass 'NoneType'\u003e\n90\n45105\n1476\n45454545454545454545\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 4. User Input\n```python\na = int(input(\"Enter a number: \"))\nb = int(input(\"Enter another number: \"))\nprint(a+b)\n```\n\u003cbr/\u003e\n\n### 5. String\n```python\nmystr = \"This is a string\"\nprint(len(mystr))\nprint(mystr)\nprint(mystr[4])\nprint(mystr[2:7])\nprint(mystr[2:9:2])\nprint(mystr[::])\nprint(mystr[::-1])\nprint(mystr.endswith(\"ing\"))\nprint(mystr.count(\"i\"))\nprint(mystr.capitalize())\nprint(mystr.upper())\nprint(mystr.lower())\n\n\"\"\"\nOutput:- \n\n16\nThis is a string\n \nis is\ni sa\nThis is a string\ngnirts a si sihT\nTrue\n3\nThis is a string\nTHIS IS A STRING\nthis is a string\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 6. List and Tuple\n```python\nmylst = [\"Tanmay\", \"Tejas\", \"Parth\", \"Viraj\"]\nmynum = [5, 2, 6, 87, 1, 4]\nprint(mylst)\nprint(mylst[3])\nprint(mylst[0:3])\nprint(mynum)\nmynum.sort()\nmynum.reverse()\nprint(mynum)\nprint(min(mynum))\nmynum.append(7)\nprint(mynum)\nmynum.remove(6)\nmynum.pop()\nprint(mynum)\n\nmytp = (1, 6, 7)\nprint(type(mytp))\n\n\"\"\"\nOutput:- \n\n['Tanmay', 'Tejas', 'Parth', 'Viraj']\nViraj\n['Tanmay', 'Tejas', 'Parth']\n[5, 2, 6, 87, 1, 4]\n[87, 6, 5, 4, 2, 1]\n1\n[87, 6, 5, 4, 2, 1, 7]\n[87, 5, 4, 2, 1]\n\u003cclass 'tuple'\u003e\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 7. Dictionary\n```python\nmydc = {\n   \"Tanmay\": \"Python\",\n   \"Tejas\": \"C#\",\n   \"Atharva\": \"C\",\n   \"Yash\": \"C++\",\n   \"nums\": {\"one\": 1, \"two\": 2, \"three\": 3}\n}\nprint(mydc)\nprint(mydc[\"Tanmay\"])\nmydc[\"Viraj\"] = \"AutoCAD\"\nprint(mydc[\"nums\"][\"one\"])\nprint(mydc)\ndel mydc[\"Atharva\"]\nprint(mydc)\nydc = mydc.copy()\nprint(ydc.items())\n\n\"\"\"\nOutput:- \n\n{'Tanmay': 'Python', 'Tejas': 'C#', 'Atharva': 'C', 'Yash': 'C++', 'nums': {'one': 1, 'two': 2, 'three': 3}}\nPython\n1\n{'Tanmay': 'Python', 'Tejas': 'C#', 'Atharva': 'C', 'Yash': 'C++', 'nums': {'one': 1, 'two': 2, 'three': 3}, 'Viraj': 'AutoCAD'}\n{'Tanmay': 'Python', 'Tejas': 'C#', 'Yash': 'C++', 'nums': {'one': 1, 'two': 2, 'three': 3}, 'Viraj': 'AutoCAD'}\ndict_items([('Tanmay', 'Python'), ('Tejas', 'C#'), ('Yash', 'C++'), ('nums', {'one': 1, 'two': 2, 'three': 3}), ('Viraj', 'AutoCAD')])\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 8. Sets\n```python\nst1 = {2, 3, 4, 5}\nst2 = {5, 6, 7}\nst2.add(8)\nprint(st1, st2)\nst3 = st1.intersection(st2)\nprint(st3)\n\n\"\"\"\nOutput:- \n\n{2, 3, 4, 5} {8, 5, 6, 7}\n{5}\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 9. Conditions\n```python\nval = int(input(\"Enter your age: \"))\nif val \u003c 18:\n    print(\"You cant drive\")\nelif val \u003e= 18:\n    print(\"You can drive\")\nelse:\n    print(\"Go to hell\")\n\nintp = int(input(\"Enter num: \"))\nprint(\"Less\") if intp \u003c= 100 else print(\"Great\")\n\n\"\"\"\nOutput:- \n\nEnter your age: 12\nYou cant drive\nEnter num: 34\nLess\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 10. Loops\n```python\nfriend = [\"Viraj\", \"Parth\", \"Sandeep\", \"Rohan\", \"Vaishnav\"]\nnums = [[\"one\", 1], [\"two\", 2], [\"three\", 3], [\"four\", 4]]\ndict1 = dict(nums)\nfor name in friend:\n    print(name)\nfor word, num in nums:\n    print(word, num)\nfor word, num in dict1.items():\n    print(word, num)\nfor i in range(0,10):\n    print(i)\nm = 0\nwhile m \u003c 10:\n    print(m)\n    m += 1\n\n\"\"\"\nOutput:- \n\nViraj\nParth\nSandeep\nRohan\nVaishnav\none 1\ntwo 2\nthree 3\nfour 4\none 1\ntwo 2\nthree 3\nfour 4\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 11. Break and Continue\n```python\ni=0\nwhile(True):\n    if i \u003c 5:\n        i+=1\n        continue\n    print(i)\n    if i==44:\n        break\n    i+=1\n\n\"\"\"\nOutput:- \n\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 12. Operators\n```python\n# Arithmetic\nprint(4+5)\nprint(8-6)\nprint(9/3)\nprint(5*2)\nprint(5//2)\nprint(5%2)\nprint(2**2)\n\n# assignment\nx = 5\nx+=5\nprint(x)\n\n# Comparison\nprint(5==5)\n\n# Logical\na = True\nb = False\nprint(a and b)\n\n# identity\nprint(a is not b)\n\n# membership\nlst = [3, 4, 5, 6, 7]\nprint(5 in lst)\n\n# bitwise\na = 0\nb = 1\nprint(a\u0026b)\nprint(a|b)\n\n\"\"\"\nOutput:- \n\n9\n2\n3.0\n10\n2\n1\n4\n10\nTrue\nFalse\nTrue\nTrue\n0\n1\n\"\"\"\n```\n\n### 13. Functions\n```python\ndef add(a:int, b:int):\n    \"\"\"Addition function\"\"\"\n    c = a+b\n    return c\n\nprint(add(4, 5))\nprint(add.__doc__)\n\n\"\"\"\nOutput:- \n\n9\nAddition function\n\"\"\"\n```\n\u003cbr/\u003e\n\n### Exception Handling\n```python\ntry:\n    a = int(input(\"Enter a number: \"))\n    b = int(input(\"Enter another number: \"))\n    print(a+b)\nexcept Exception as e:\n    print(e)\n\n\"\"\"\nOutput:- \n\nEnter a number: r\ninvalid literal for int() with base 10: 'r'\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 15. File Handling\n```python\nf = open(\"15_file.txt\", 'r')\ncontent = f.read(19)\nprint(content)\ncontent = f.read(10)\nprint(content)\nf.close()\n\nf = open(\"15_file.txt\", 'r')\nfor l in f:\n    print(l, end=\"\")\nf.close()\n\nf = open(\"15_file.txt\", 'r')\nprint(f.readline())\nprint(f.readline())\nf.close()\n\nf = open(\"15_file.txt\", 'a')\nf.write(\"\\nHello\\n\")\nf.close()\n\nf = open(\"15_file.txt\", 'r')\nprint(f.tell())\nprint(f.readline())\nprint(f.tell())\nf.seek(0)\nprint(f.tell())\nf.close()\n\nwith open(\"15_file.txt\") as f:\n    print(f.read())\n```\n\u003cbr/\u003e\n\n### 16. Global Variables\n```python\ndef tan():\n    x = 20\n    def rock():\n        global x\n        x = 40\n        print(x)\n    print(x)\n    rock()\n\ntan()\n\n\"\"\"\nOutput:- \n\n20\n40\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 17. Recursion\n```python\ndef factorial(n):\n    if n == 1:\n        return 1\n    else:\n        return n * factorial(n-1)\n\nprint(factorial(5))\n```\n\u003cbr/\u003e\n\n### 18. Lambda\n```python\nadd = lambda a,b : a+b\nprint(add(5, 6))\n```\n\u003cbr/\u003e\n\n### 19. Fstring\n```python\na = 10\nprint(f\"statement {a}\")\n```\n\u003cbr/\u003e\n\n### 20. Args and Kwargs\n```python\ndef myfunc(normal, *args , **kwargs):\n    print(normal)\n    for i in args:\n        print(i)\n    for key , value in kwargs.items():\n        print(key, value)\n    \nnormal = \"Tanmay\"\nargs = [\"Pizza\", \"Burgar\", \"Samosa\", \"Vada Pav\", \"Missal\", \"Dosa\"]\nkwargs = {\n    \"Tanmay\": \"Python\",\n    \"Tejas\": \"C#\",\n    \"Viraj\": \"AutoCAD\",\n    \"Yash\": \"C++\",\n    \"Atharva\": \"C\" \n}\n\nmyfunc(normal, *args, **kwargs)\n\n\"\"\"\nOutput:- \n\nTanmay\nPizza\nBurgar\nSamosa\nVada Pav\nMissal\nDosa\nTanmay Python\nTejas C#\nViraj AutoCAD\nYash C++\nAtharva C\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 21. Enumerate\n```python\nlst = [\"Pizza\", \"Burger\", \"Vada Pav\", \"Biryani\", \"Samosa\"]\n\nfor index, item in enumerate(lst):\n    if index%2==0:\n        print(item)\n\n\"\"\"\nOutput:- \n\nPizza\nVada Pav\nSamosa\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 22. Join\n```python\nlst = [\"Pizza\", \"Burger\", \"Vada Pav\", \"Biryani\", \"Samosa\"]\nprint(\" , \".join(lst))\n\n\"\"\"\nOutput:-\n\nPizza , Burger , Vada Pav , Biryani , Samosa\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 23. Map, Filter and Reduce\n```python\n# Map\nnums = [1, 2, 3, 4, 5, 6, 7]\nsq = list(map(lambda x: x*x, nums))\nprint(sq)\n\n# Filter\nnum1 = [1, 2, 3, 4, 5, 6 ,7, 8, 9]\nnlst = list(filter(lambda x: x\u003e5, num1))\nprint(nlst)\n\n# Reduce\nfrom functools import reduce\nnum2 = [1, 2, 3, 4, 5]\nnlst2 = reduce(lambda x, y: x+y, num2)\nprint(nlst2)\n\n\"\"\"\nOutput:- \n\n[1, 4, 9, 16, 25, 36, 49]\n[6, 7, 8, 9]\n15\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 24. Decorators\n```python\ndef dec1(func):\n    def nowexec():\n        print(\"Module starting\")\n        func()\n        print(\"Ending\")\n    return nowexec\n\n@dec1\ndef command():\n    print(\"This is a test command\")\n\ncommand()\n\n\"\"\"\nModule starting\nThis is a test command\nEnding\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 25. Classes\n```python\nclass Student:\n    pass\n\ntanmay = Student()\nviraj = Student()\n\ntanmay.std = 12\ntanmay.age = 18\nprint(tanmay.age)\n\n\"\"\"\nOutput:- \n\n18\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 26. Constructor\n```python\nclass Employee:\n    def __init__(self, name, age, salary):\n        self.name = name\n        self.age = age\n        self.salary = salary\n    def printdetails(self):\n        print(f\"{self.name} {self.age} {self.salary}\")\nemp1 = Employee(\"Tanmay\", 27, 56000)\nemp1.printdetails()\n\n\"\"\"\nOutput:-\n\nTanmay 27 56000\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 27. Classmethod\n```python\nclass Employee:\n    no_of_leaves = 5\n    def __init__(self, name, age, salary):\n        self.name = name\n        self.age = age\n        self.salary = salary\n    def printdetails(self):\n        print(f\"{self.name} {self.age} {self.salary}\")\n    @classmethod\n    def changeleaves(cls, leaves):\n        cls.no_of_leaves = leaves\nemp1 = Employee(\"Tanmay\", 27, 56000)\nemp1.printdetails()\nprint(emp1.no_of_leaves)\nemp1.changeleaves(45)\nprint(emp1.no_of_leaves)\n\n\"\"\"\nOutput:- \n\nTanmay 27 56000\n5\n45\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 28. Staticmethod\n```python\nclass Employee:\n    no_of_leaves = 5\n    def __init__(self, name, age, salary):\n        self.name = name\n        self.age = age\n        self.salary = salary\n    def printdetails(self):\n        print(f\"{self.name} {self.age} {self.salary}\")\n    @classmethod\n    def changeleaves(cls, leaves):\n        cls.no_of_leaves = leaves\n    @staticmethod\n    def printsom(val):\n        print(\"This is a \" + val)\nemp1 = Employee(\"Tanmay\", 27, 56000)\nemp1.printdetails()\nprint(emp1.no_of_leaves)\nemp1.changeleaves(45)\nprint(emp1.no_of_leaves)\nEmployee.printsom(\"Statement\")\n\n\"\"\"\nOutput:- \n\nTanmay 27 56000\n5\n45\nThis is a Statement\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 29. Single Inheritance\n```python\nclass Employee:\n    def __init__(self, name, age, salary):\n        self.name = name\n        self.age = age\n        self.salary = salary\n    def printdetails(self):\n        print(f\"{self.name} {self.age} {self.salary}\")\nclass Programmer(Employee):\n    pass\nprog1 = Programmer(\"Tanmay\", 18, None)\nprog1.printdetails()\n\n\"\"\"\nOutput:- \n\nTanmay 18 None\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 30. Multiple Inheritance\n```python\nclass Employee:\n    def __init__(self, name, age, salary):\n        self.name = name\n        self.age = age\n        self.salary = salary\n    def printdetails(self):\n        print(f\"{self.name} {self.age} {self.salary}\")\nclass Programmer:\n     def __init__(self, name, language):\n         self.name = name\n         self.language = language\nclass CoolGuy(Employee, Programmer):\n    pass\na = CoolGuy(\"Tanmay\", 16, None)\na.printdetails()\n\n\"\"\"\nOutput:-\n\nTanmay 16 None\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 31. Multilevel Inheritance\n```python\nclass Grandfather:\n    def def1(self):\n        print(\"grandfather\")\nclass Father(Grandfather):\n    def def2(self):\n        print(\"father\")\nclass Son(Father):\n    def def3(self):\n        print(\"son\")\nobj = Son()\nobj.def1()\nobj.def2()\nobj.def3()\n\n\"\"\"\nOutput:-\n\ngrandfather\nfather\nson\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 32. Access Modifier\n```python\nclass Employee:\n    var = 34\n    _myprotec = 5\n    __protected = 45\nobj = Employee()\nprint(obj.var)\nprint(obj._myprotec)\nprint(obj._Employee__protected)\n\n\"\"\"\nOutput:-\n\n34\n5\n45\ntan\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 33. Super init\n```python\nclass A:\n    def __init__(self):\n        self.var1 = \"class A var1\"\n        self.var2 = \"class A var2\"\nclass B(A):\n    def __init__(self):\n        super().__init__()\nobj = B()\nprint(obj.var1)\n\n\"\"\"\nOutput:-\n\nclass A var1\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 34. Diamond Inheritance\n```python\nclass A:\n    def met(self):\n        print(\"A\")\nclass B(A):\n    def met(self):\n        print(\"B\")\nclass C(A):\n    def met(self):\n        print(\"C\")\nclass D(B, C):\n    def met(self):\n        print(\"D\")\na = A()\nb = B()\nc = C()\nd = D()\nd.met()\n\n\"\"\"\nOuput:- \n\nD\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 35. Overloading\n```python\nclass complex:\n    def __init__(self, real, imag):\n        self.real = real\n        self.imag = imag\n    def __add__(self, other):\n        real = self.real + other.real\n        imag = self.imag + other.imag\n        return f\"{real} + ({imag})i\"\n    def __repr__(self):\n        return f\"{self.real} + ({self.imag})i\"\nobj1 = complex(4, 5)\nobj2 = complex(5, 4)\nprint(obj1 + obj2)\nprint(obj1)\n\n\"\"\"\nOuput:-\n\n9 + (9)i\n4 + (5)i\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 36. Abstract Class\n```python\nfrom abc import ABC, abstractmethod\nclass Shape(ABC):\n    @abstractmethod\n    def printarea():\n        return 0\nclass Rectangle(Shape):\n    def printarea():\n        return 0\nobj = Rectangle()\n```\n\u003cbr/\u003e\n\n### 37. Generator\n```python\ndef gen(n):\n    for i in range(n):\n        yield i\ng = gen(34)\nprint(g.__next__())\nprint(g.__next__())\nprint(g.__next__())\n\n\"\"\"\nOutput:-\n\n0\n1\n2\n\"\"\"\n```\n\u003cbr/\u003e\n\n### 38. Comprehension\n```python\nprint([i for i in range(100) if i%3 == 0])\nprint({i:f\"item{i}\" for i in range(1000) if i%100==0})\n\n\"\"\"\nOutput:-\n\n[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]\n{0: 'item0', 100: 'item100', 200: 'item200', 300: 'item300', 400: 'item400', 500: 'item500', 600: 'item600', 700: 'item700', 800: 'item800', 900: 'item900'}\n\"\"\"\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftanmayvaij%2Fpython-syntax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftanmayvaij%2Fpython-syntax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftanmayvaij%2Fpython-syntax/lists"}