{"id":25469860,"url":"https://github.com/thinkphp/computer-science-in-python","last_synced_at":"2026-03-01T21:35:16.011Z","repository":{"id":20380486,"uuid":"23656069","full_name":"thinkphp/computer-science-in-python","owner":"thinkphp","description":"Computer Science in Python language.","archived":false,"fork":false,"pushed_at":"2026-02-15T20:16:26.000Z","size":50850,"stargazers_count":6,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-02-16T02:35:58.514Z","etag":null,"topics":["algorithms","datastructures","machine-learning","python3"],"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/thinkphp.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2014-09-04T09:17:52.000Z","updated_at":"2026-02-15T20:16:29.000Z","dependencies_parsed_at":"2023-11-30T15:26:01.125Z","dependency_job_id":"ac3aa2f8-bcd4-4609-83a0-5004346fad44","html_url":"https://github.com/thinkphp/computer-science-in-python","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thinkphp/computer-science-in-python","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thinkphp","download_url":"https://codeload.github.com/thinkphp/computer-science-in-python/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-python/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29984725,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T21:06:37.093Z","status":"ssl_error","status_checked_at":"2026-03-01T21:05:45.052Z","response_time":124,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["algorithms","datastructures","machine-learning","python3"],"created_at":"2025-02-18T08:31:04.231Z","updated_at":"2026-03-01T21:35:15.938Z","avatar_url":"https://github.com/thinkphp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Computer Science in Python\n## **1. Introduction to Python Programming**\n\n### **1.1. What is Python?**\nPython is a high-level, interpreted language known for its simplicity and readability. It is widely used in web development, data analysis, artificial intelligence, and scientific computing.\n\n### **1.2. Python Syntax \u0026 Basics**\n\n- **Comments:** Use `#` for single-line comments and `''' ... '''` for multi-line comments.\n  \n- **Variables and Data Types**  \n  Python handles different data types automatically.\n\n    ```python\n    # Example 1: Defining variables\n    x = 5               # Integer\n    y = 3.14            # Float\n    name = \"Stanford\"   # String\n    is_student = True   # Boolean\n\n    # Example 2: Changing types\n    age = 21\n    age = \"Twenty-one\"  # Reassigned to a string\n    ```\n\n- **Print Function**\n\n    ```python\n    # Example 3: Printing values\n    print(\"Welcome to Python!\")  \n    print(f\"My age is {age}\")   # String formatting with f-strings\n    ```\n\n---\n\n## **2. Control Flow**\n\n### **2.1. Conditional Statements**\n\n- **if-else Statements:**\n\n    ```python\n    # Example 1: Basic if-else\n    age = 20\n    if age \u003e= 18:\n        print(\"Adult\")\n    else:\n        print(\"Minor\")\n\n    # Example 2: if-elif-else\n    marks = 85\n    if marks \u003e= 90:\n        print(\"Grade A\")\n    elif marks \u003e= 80:\n        print(\"Grade B\")\n    else:\n        print(\"Grade C\")\n\n    # Example 3: Nested conditionals\n    num = 7\n    if num % 2 == 0:\n        if num \u003e 5:\n            print(\"Even and greater than 5\")\n        else:\n            print(\"Even and less than or equal to 5\")\n    else:\n        print(\"Odd\")\n    ```\n\n### **2.2. Loops**\n\n- **for Loops:**\n\n    ```python\n    # Example 1: Loop through a list\n    students = [\"Alice\", \"Bob\", \"Charlie\"]\n    for student in students:\n        print(student)\n\n    # Example 2: Loop with range\n    for i in range(5):  # Loop from 0 to 4\n        print(i)\n\n    # Example 3: Loop with step\n    for i in range(0, 10, 2):  # Loop with step 2\n        print(i)\n    ```\n\n- **while Loops:**\n\n    ```python\n    # Example 1: Basic while loop\n    count = 0\n    while count \u003c 5:\n        print(count)\n        count += 1\n\n    # Example 2: Break statement\n    i = 0\n    while i \u003c 10:\n        if i == 7:\n            break\n        print(i)\n        i += 1\n\n    # Example 3: Continue statement\n    for i in range(10):\n        if i % 2 == 0:\n            continue  # Skip even numbers\n        print(i)\n    ```\n\n---\n\n## **3. Functions**\n\n### **3.1. Defining and Calling Functions**\n\n- **Basic Function:**\n\n    ```python\n    # Example 1: Simple function\n    def greet(name):\n        print(f\"Hello, {name}!\")\n\n    greet(\"Stanford\")\n\n    # Example 2: Return values\n    def add(a, b):\n        return a + b\n\n    result = add(5, 3)\n    print(result)\n\n    # Example 3: Default arguments\n    def greet_with_time(name, time_of_day=\"morning\"):\n        print(f\"Good {time_of_day}, {name}!\")\n\n    greet_with_time(\"Alice\", \"evening\")\n    greet_with_time(\"Bob\")\n    ```\n\n### **3.2. Lambda Functions**\n\n- **Lambda Syntax:**\n\n    ```python\n    # Example 1: Basic lambda function\n    add = lambda x, y: x + y\n    print(add(5, 3))\n\n    # Example 2: Lambda in filter\n    numbers = [1, 2, 3, 4, 5]\n    even_numbers = list(filter(lambda x: x % 2 == 0, numbers))\n    print(even_numbers)\n\n    # Example 3: Lambda with map\n    squares = list(map(lambda x: x ** 2, numbers))\n    print(squares)\n    ```\n\n---\n\n## **4. Data Structures**\n\n### **4.1. Lists**\n\n- **List Basics:**\n\n    ```python\n    # Example 1: Creating a list\n    fruits = [\"apple\", \"banana\", \"cherry\"]\n    print(fruits)\n\n    # Example 2: List indexing\n    print(fruits[0])   # First element\n    print(fruits[-1])  # Last element\n\n    # Example 3: Modifying a list\n    fruits.append(\"orange\")\n    fruits[1] = \"blueberry\"\n    print(fruits)\n    ```\n\n### **4.2. Dictionaries**\n\n- **Dictionary Basics:**\n\n    ```python\n    # Example 1: Creating a dictionary\n    student = {\"name\": \"Alice\", \"age\": 20, \"major\": \"CS\"}\n    print(student)\n\n    # Example 2: Accessing values\n    print(student[\"name\"])\n    print(student.get(\"age\"))\n\n    # Example 3: Modifying a dictionary\n    student[\"age\"] = 21\n    student[\"university\"] = \"Stanford\"\n    print(student)\n    ```\n\n### **4.3. Tuples**\n\n- **Tuple Basics:**\n\n    ```python\n    # Example 1: Creating a tuple\n    coordinates = (10, 20)\n    print(coordinates)\n\n    # Example 2: Unpacking a tuple\n    x, y = coordinates\n    print(x, y)\n\n    # Example 3: Tuple immutability\n    try:\n        coordinates[0] = 5  # Raises an error\n    except TypeError:\n        print(\"Tuples are immutable!\")\n    ```\n\n---\n\n## **5. Object-Oriented Programming**\n\n### **5.1. Classes and Objects**\n\n- **Class Syntax:**\n\n    ```python\n    # Example 1: Defining a class\n    class Student:\n        def __init__(self, name, age):\n            self.name = name\n            self.age = age\n\n        def greet(self):\n            print(f\"Hello, my name is {self.name}.\")\n\n    # Example 2: Creating an object\n    student1 = Student(\"Alice\", 20)\n    student1.greet()\n\n    # Example 3: Modifying attributes\n    student1.age = 21\n    print(f\"{student1.name} is now {student1.age} years old.\")\n    ```\n\n---\n\n## **6. File Handling**\n\n### **6.1. Reading and Writing Files**\n\n- **File Operations:**\n\n    ```python\n    # Example 1: Writing to a file\n    with open(\"file.txt\", \"w\") as file:\n        file.write(\"Hello, Stanford!\")\n\n    # Example 2: Reading from a file\n    with open(\"file.txt\", \"r\") as file:\n        content = file.read()\n        print(content)\n\n    # Example 3: Appending to a file\n    with open(\"file.txt\", \"a\") as file:\n        file.write(\"\\nNew line added.\")\n    ```\n\n---\n\n## **7. Error Handling**\n\n### **7.1. Try-Except Blocks**\n\n- **Exception Handling:**\n\n    ```python\n    # Example 1: Basic try-except\n    try:\n        x = 10 / 0\n    except ZeroDivisionError:\n        print(\"Cannot divide by zero!\")\n\n    # Example 2: Multiple exceptions\n    try:\n        x = int(\"abc\")\n    except (ValueError, TypeError) as e:\n        print(f\"Error: {e}\")\n\n    # Example 3: Finally clause\n    try:\n        file = open(\"file.txt\", \"r\")\n    except FileNotFoundError:\n        print(\"File not found.\")\n    finally:\n        print(\"This block always runs.\")\n    ```\n\n---\n\n## **8. Modules and Packages**\n\n### **8.1. Importing Modules**\n\n- **Using Built-in Modules:**\n\n    ```python\n    # Example 1: Importing a module\n    import math\n    print(math.sqrt(16))\n\n    # Example 2: Importing specific functions\n    from math import pi, pow\n    print(pi, pow(2, 3))\n\n    # Example 3: Custom module (if students create their own)\n    import my_module  # Assuming there's a my_module.py file\n    ```\n\n---\n\n\n\nComputer science is no more about computers than astronomy is about telescopes, biology is about microscopes or chemistry is about beakers and test tubes. Science is not about tools, it is about how we use them and what we find out when we do. (Michael Fellows and Ian Parberry, “SIGACT trying to get children excited about CS”)\n\n\n\n#### Preface\n\nPython functions are defined using def keywords:\n\n```python\ndef sign(x):\n    if x \u003e 0:\n        return 'positive'\n    elif x \u003c 0:\n        return 'negative'\n    else:\n        return 'zero'\n\nfor x in [-1, 0, 1]:\n    print sign(x)\n# Prints \"negative\", \"zero\", \"positive\"\n\ndef hello(name, loud=False):\n    if loud:\n        print 'HELLO, %s!' % name.upper()\n    else:\n        print 'Hello, %s' % name\n\nhello('Bob') # Prints \"Hello, Bob\"\nhello('Fred', loud=True)  # Prints \"HELLO, FRED!\"\n```\n\n#### Variables\n\n#### Data Types\n\n#### Strings\n\n```python\n\ntxt = \"I love apples, apple are my favorite fruit\"\nx = txt.count(\"apple\")\nprint(x)\n\nmyDict = {\"name\": \"John\", \"country\": \"Norway\"}\nmySeparator = \"TEST\"\nx = mySeparator.join(myDict)\nprint(x)\n\n# Python code to demonstrate working of \n# strip(), lstrip() and rstrip() \nstr = \"---python3---\"\n\n# using strip() to delete all '-' \nprint ( \" String after stripping all '-' is : \", end=\"\") \nprint ( str.strip('-') ) \n\n# using lstrip() to delete all trailing '-' \nprint ( \" String before stripping all leading '-' is : \", end=\"\") \nprint ( str.lstrip('-') ) \n\n# using rstrip() to delete all leading '-' \nprint ( \" String after stripping all trailing '-' is : \", end=\"\") \nprint ( str.rstrip('-') ) \n```\n\n#### Numbers\n\n#### Boolean\n\n#### Exception handling\n\n#### Data Members\n\n```python\nclass Person:\n  # data members\n  salary = 23 \n  def __init__(self, name, age):\n    Person.name = name\n    self.age = age\n    Person.salary += 1\n\n  def __str__(self):\n    return f\"{self.name}({self.age})\" + str(Person.salary)    \n\np1 = Person(\"John\", 36)\n\nprint(p1)\n\n```\n\n### Class\n\nWe can easily create the classes using class keyword in the following manner:\n\n```python\nclass Greeter(object):\n    \n    # Constructor\n    def __init__(self, name):\n        self.name = name  # Create an instance variable\n        \n    # Instance method\n    def greet(self, loud=False):\n        if loud:\n            print 'HELLO, %s!' % self.name.upper()\n        else:\n            print 'Hello, %s' % self.name\n        \ng = Greeter('Fred')  # Construct an instance of the Greeter class\ng.greet()            # Call an instance method; prints \"Hello, Fred\"\ng.greet(loud=True)   # Call an instance method; prints \"HELLO, FRED!\"\n```\n### Programming Contests\n\n#### Educational Codeforces \n\nRound  1 https://codeforces.com/contest/598\n\nRound 26 https://codeforces.com/contest/837\n\nExperimental Educational Round: VolBIT Formulas Blitz https://codeforces.com/contest/630\n\nVK Cup 2015 - Wild Card Round 1 https://codeforces.com/contest/530\n\nCodeforces Alpha Round 20 https://codeforces.com/contest/20 (A. BerOS file system1, B. Equation, C. Dijkstra?)\n\nApril Fools Day Contest 2024 https://codeforces.com/contest/1952\n\n#### Div. 1\n\nCodeforces Round 808 (Div. 1) https://codeforces.com/contest/1707\n\n#### Div. 2\n\nCodeforces Round 213 (Div. 2) https://codeforces.com/contest/365\n\nCodeforces Round 934 (Div. 2) https://codeforces.com/contest/1944\n\nCodeforces Round 726 (Div. 2) https://codeforces.com/contest/1537\n\nCodeforces Round 926 (Div. 2) https://codeforces.com/contest/1929\n\nCodeforces Beta Round 9 (Div. 2) https://codeforces.com/contest/9\n\nEducational Codeforces Round 161 (Div. 2) https://codeforces.com/contest/1922\n\nCodeforces Beta Round 91 (Div. 2) https://codeforces.com/contest/122\n\nCodeforces Round 948 (Div. 2) https://codeforces.com/contest/1977\n\nCodeforces Beta Round 82 (Div. 2) https://codeforces.com/contest/106\n\nCodeforces Round 907 (Div. 2) https://codeforces.com/contest/1891\n\nCodeforces Round 134 (Div. 2) https://codeforces.com/contest/218\n\n#### Div.3 \nCodeforces Round 946 (Div. 3) https://codeforces.com/contest/1974\n\nCodeforces Round 920 (Div. 3) https://codeforces.com/contest/1921\n\nCodeforces Round 587 (Div. 3) https://codeforces.com/contest/1216\n\nCodeforces Round 486 (Div. 3) https://codeforces.com/contest/988\n\nCodeforces Round 479 (Div. 3) https://codeforces.com/contest/977\n\nCodeforces Round 481 (Div. 3) https://codeforces.com/contest/978\n\nCodeforces Round 929 (Div. 3) https://codeforces.com/contest/1933\n\nCodeforces Round 923 (Div. 3) https://codeforces.com/contest/1927\n\nCodeforces Round 535 (Div. 3) https://codeforces.com/contest/1108\n\nCodeforces Round 529 (Div. 3) https://codeforces.com/contest/1095\n\n#### Div. 4\n\nCodeforces Round 799 (Div. 4) https://codeforces.com/contest/1692\n\nCodeforces Round 928 (Div. 4) https://codeforces.com/contest/1926\n\nCodeforces Round 640 (Div. 4) https://codeforces.com/contest/1352\n\nCodeforces Round 784 (Div. 4) https://codeforces.com/contest/1669\n\nCodeforces Round 835 (Div. 4) https://codeforces.com/contest/1760\n\nCodeforces Round 849 (Div. 4) https://codeforces.com/contest/1791\n\nCodeforces Round 859 (Div. 4) https://codeforces.com/contest/1807\n\nCodeforces Round 827 (Div. 4) https://codeforces.com/contest/1742\n\nCodeforces Round 817 (Div. 4) https://codeforces.com/contest/1722\n\nCodeforces Round 790 (Div. 4) https://codeforces.com/contest/1676\n\nCodeforces Round 898 (Div. 4) https://codeforces.com/contest/1873\n\n\n####  Happy New Year\n\nGood Bye 2014 https://codeforces.com/contest/500 Editorial: https://codeforces.com/blog/entry/15513\n\nGood Bye 2015 https://codeforces.com/contest/611 Editorial: https://codeforces.com/blog/entry/22441\n\nGood Bye 2016 https://codeforces.com/contest/611 Editorial: https://codeforces.com/blog/entry/49412\n\nGood Bye 2017 https://codeforces.com/contest/908 Editorial: https://codeforces.com/blog/entry/56713 https://codeforces.com/blog/entry/56848\n\nGood Bye 2018 https://codeforces.com/contest/1091 Editorial: https://codeforces.com/blog/entry/64196\n\nGood Bye 2019 https://codeforces.com/contest/1270 Editorial: https://codeforces.com/blog/entry/72611\n                                                  \nGood Bye 2020 https://codeforces.com/contest/1466 Editorial: https://codeforces.com/blog/entry/86126\n\nGood Bye 2021 https://codeforces.com/contest/1616  Editorial: https://codeforces.com/blog/entry/98501\n\nGood Bye 2022 https://codeforces.com/contest/1770 Editorial: https://codeforces.com/blog/entry/110754\n\nGood Bye 2023 https://codeforces.com/contest/1916 Editorial: https://codeforces.com/blog/entry/124138 \n\n### EDU Codeforces\nhttps://codeforces.com/edu/courses\n\n\n### Baltic Olympiad in Informatics 2020\nDay 1: https://codeforces.com/contest/1386\n\nDay 2: https://codeforces.com/contest/1387\n\n### AtCoder\nhttps://atcoder.jp/contests/abc272/tasks\n\nhttps://atcoder.jp/contests/abc356/tasks\n\nhttps://atcoder.jp/contests/abc354/tasks\n\nhttps://atcoder.jp/contests/abc349/tasks\n\nhttps://atcoder.jp/contests/abc224/tasks\n\n\n\n\n### Math:\n\n* https://brilliant.org/wiki/bezouts-identity/\n* https://www.intmath.com/quadratic-equations/sum-product-roots-quadratic-equation.php\n* https://codeforces.com/blog/entry/97623\n* https://www.cuemath.com/algebra/nature-of-roots/#What-Do-You-Mean-By-Nature-of-Roots\n* https://www.cuemath.com/geometry/x-intercept/\n* https://brilliant.org/wiki/vietas-formula/#vietas-formula-problem-solving-easy\n* https://artofproblemsolving.com/wiki/index.php/Euclidean_algorithm\n* https://artofproblemsolving.com/wiki/index.php/Goldbach_Conjecture\n* https://artofproblemsolving.com/wiki/index.php/Math_books\n\n### References\n\nhttps://www.amazon.com/Introduction-Algorithms-Thomas-H-Cormen/dp/0262033844/\n\nhttps://www.geeksforgeeks.org/learn-data-structures-and-algorithms-dsa-tutorial/?ref=shm\n\nhttps://web.stanford.edu/class/cs97si/\n\nhttps://www.enjoyalgorithms.com/\n\nhttps://www.programming-books.io/essential/python/\n\nhttps://mcsp.wartburg.edu/zelle/python/\n\nhttps://www.cfm.brown.edu/people/dobrush/am33/python/index.html\n\nhttps://github.com/rtoal/ple/tree/main/python\n\n\n#### My Favourite Books:\n\nAn Introduction to Programming in Python. An interdisciplinary Approach. Robert Sedgwick.\n\nA Beginners Guide To Python 3 Programming - Springer\n\nAdvanced Guide To Python 3 Programming - Springer \n\nPython for Probability, Statistics, and Machine Learning - Springer\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkphp%2Fcomputer-science-in-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthinkphp%2Fcomputer-science-in-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkphp%2Fcomputer-science-in-python/lists"}