Introduction to Python

Python is a versatile, high-level programming language known for its readability and simplicity. It's widely used in web development, data analysis, artificial intelligence, and more.

Estimated reading time: 10 minutes

Python Version

This tutorial assumes you're using Python 3.10 or newer. If you're using an older version, some syntax may differ.

Understanding Python Basics

What is Python?

Python is an interpreted, object-oriented language with dynamic semantics. Its straightforward syntax emphasizes readability and reduces the cost of program maintenance.

Installing Python and Setting Up Your Environment

  • Windows:
    1. Download the installer from python.org.
    2. Run the installer and check the box "Add Python to PATH".
  • macOS:
    1. Download the installer from python.org.
    2. Run the installer. Alternatively, use Homebrew with brew install python3.
  • Linux:
    • Use your package manager: sudo apt-get install python3.

Running Python Code

  • Interactive Shell:
    • Open your terminal and type python3 to start.
    • Execute code line by line.
  • Scripts:
    • Write code in a file with a .py extension.
    • Run it using python3 your_script.py.

Writing Your First Python Program

Easy Example: Interactive Hello, World!

  1. Open your terminal.
  2. Start the Python shell:
python3
  1. Type:
print("Hello, World!")
  1. Press Enter to see the output:
Hello, World!

Advanced Example: Scripted Hello, World!

  1. Open a text editor and write the following code:
# hello_world.py

def main():
    print("Hello, World!")

if __name__ == "__main__":
    main()
  1. Save the file as hello_world.py.
  2. Run the script from the terminal:
python3 hello_world.py
  1. Output:
Hello, World!

Key Takeaways

  • Python is beginner-friendly with clear and readable syntax.
  • You can run Python code interactively or via scripts.
  • The print() function outputs data to the screen.
  • Using if __name__ == "__main__": allows your script to be run directly.

Next Steps

  • Experiment with the input() function to receive user input.
  • Explore basic data types: integers, floats, strings, lists, and dictionaries.
  • Try writing a program that performs simple arithmetic operations.