Learn with Yasir

Share Your Feedback

Python Basics - Beginner-Friendly Tutorials, Examples, and Exercises


Learn Python basics with beginner-friendly tutorials, examples, and exercises. Master Python programming concepts like print function, variables, comments, indentation and more. Perfect for students and professionals starting their Python journey.

Table of Contents

  • What is Python
  • What is Python

  • Python is a high-level, general-purpose programming language.
  • It is known for its clear syntax, readability, and versatility.
  • Python is widely used for web development, data science, machine learning, and automation.

Getting Started

Important: Python source code files always use the .py extension.

Comments

  • Comments are important for making code more readable and understandable, especially for other programmers who may need to read or modify the code.
  • Comments in Python are non-executable lines of code and ignored by the Python interpreter when the code is executed.

There are two main types of comments in Python:

  • Single-line comments: These comments start with the hash symbol (#) and extend to the end of the line.
# This is a single-line comment
print("Hello, World!")
  • Multi-line comments: These comments are enclosed in triple quotes (“”” or ‘’’).
"""
This is a multi-line comment.
It can span multiple lines of code.
"""
print("Hello, World!")

See also:

Indentation

In Python, indentation refers to the use of whitespace (spaces or tabs) to denote block-level structure in the code. Python uses indentation to define the scope of code blocks, such as:

  • Function definitions
  • Loops (for, while)
  • Conditional statements (if, elif, else)
  • Exception handling (try, except)

In Python, indentation is mandatory and is used to determine the grouping of statements. The number of spaces used for indentation is not fixed, but it’s standard to use 4 spaces for each level of indentation. Read more: Indentation - PEP 8 – Style Guide for Python Code

Here’s an example:

if True:
    print("Hello")  # This line is indented with 4 spaces
    print("World")  # This line is also indented with 4 spaces

In this example, the two print statements are indented with 4 spaces, indicating that they are part of the if block.

Python’s indentation system has several benefits, including:

  • Improved readability: Indentation makes the code structure clear and easy to read.
  • Reduced errors: Indentation helps avoid errors caused by mismatched braces or parentheses.
  • Simplified syntax: Python’s indentation system eliminates the need for explicit block delimiters like braces or keywords.

Another example, consider the following code snippet:

if True:
    print("True")
else:
    print("False")

Task: 15 Please correct the following Python code:

if True:
  print("True")
    print("False")

Error message: IndentationError: unexpected indent

See also:

Task 16: Code Together, Lead Together

Title: “Teach Your Team”
Instructions:

  1. Each student picks one basic Python topic (e.g., print function,Basic Printing, Printing Different Data Types, Using sep and end Parameters, Print Variables, String Formatting with f-Strings, Printing Special Characters, Syntax Error, Comments, Indentation).
  2. Prepare a 2-minute explanation for the group (using examples).
  3. Deliver their explanation to the team.
  4. Peers ask questions and give feedback.

💡 Key Learning Points:
✔ Leadership through teaching.
✔ Confidence in public speaking.
✔ Understanding Python better by explaining it.

Practice & Progress


Multiple-Choice Questions (MCQs)


Find and Fix Mistakes

Online Quizzes for Python Basics


Coding Exercises


Mini Projects


Review Questions


References and Bibliography


🧠 Practice & Progress

Explore More Topics

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules