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:
- Download the installer from python.org.
- Run the installer and check the box "Add Python to PATH".
- macOS:
- Download the installer from python.org.
- Run the installer. Alternatively, use Homebrew with
brew install python3.
- Linux:
- Use your package manager:
sudo apt-get install python3.
- Use your package manager:
Running Python Code
- Interactive Shell:
- Open your terminal and type
python3to start. - Execute code line by line.
- Open your terminal and type
- Scripts:
- Write code in a file with a
.pyextension. - Run it using
python3 your_script.py.
- Write code in a file with a
Writing Your First Python Program
Easy Example: Interactive Hello, World!
- Open your terminal.
- Start the Python shell:
python3- Type:
print("Hello, World!")- Press Enter to see the output:
Hello, World!Advanced Example: Scripted Hello, World!
- Open a text editor and write the following code:
# hello_world.py
def main():
print("Hello, World!")
if __name__ == "__main__":
main()- Save the file as
hello_world.py. - Run the script from the terminal:
python3 hello_world.py- 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.