TutorialsPythonCore Python
Share:

Core Python

intermediate

What You'll Learn

    Theory

    Core Python programming revolves around three pillars: control flow directs execution, functions organize logic, and data structures store and manage data.

    Control flow in Python uses if/elif/else for conditionals, for loops for iteration, and while loops for repeated execution. Python's range() function generates sequences for for loops. The in operator checks membership. Control flow relies on indentation — consistent 4-space indentation defines blocks.

    Functions are defined with def. They can accept parameters (including default values, *args, and **kwargs) and return values with return. Functions are first-class objects — they can be assigned to variables, passed as arguments, and returned from other functions. Docstrings ("""...""") document function purpose.

    Built-in data structures:

    • Lists — ordered, mutable sequences [1, 2, 3]
    • Tuples — ordered, immutable sequences (1, 2, 3)
    • Sets — unordered, unique elements {1, 2, 3}
    • Dictionaries — key-value pairs {"key": "value"}
    • Strings — immutable sequences of characters

    Comprehensions provide concise syntax for creating collections: [x**2 for x in range(10)] creates a list of squares.

    Core Python Examples
    python

    Why this matters

    Control flow, functions, and data structures are the core tools you'll use in every Python program. Whether you're processing data, building a web app, or writing automation scripts, these fundamentals are your daily toolkit.

    What's next

    In the next lessons, you'll dive deeper into each topic with hands-on examples and exercises.

    Exercises

    Build a Grade Calculator

    easy

    Write a function get_grade(score) that returns 'A' for 90-100, 'B' for 80-89, 'C' for 70-79, 'D' for 60-69, and 'F' for below 60. Then test it with scores 95, 83, 72, 55.

    Starter Code:

    def get_grade(score):\n    # Your code here\n    pass\n\n# Test your function\nprint(get_grade(95))\nprint(get_grade(83))\nprint(get_grade(72))\nprint(get_grade(55))

    Expected Output:

    A\nB\nC\nF

    Mini Quiz

    Mini Quiz