Introduction & Getting Started
Introduction to Python
Python is a popular high-level programming language that was first released in 1991. It is designed to be easy to read and write, making it a great choice for beginners, while still being powerful enough for experienced programmers to use for a wide range of tasks.
Python is often used for tasks like data analysis, scientific computing, web development, machine learning, and automation. It has a large and active community of developers who contribute to its libraries and tools, making it a versatile and powerful language.
Why use Python?
There are many reasons why people choose to use Python, including:
- Easy to learn: Python's syntax is designed to be easy to read and write, making it accessible to beginners.
- Versatile: Python can be used for a wide range of tasks, from scientific computing to web development to automation.
- Large ecosystem: Python has a large and active community of developers who contribute to its libraries and tools, making it easy to find and use existing code.
- Cross-platform: Python can be run on a variety of operating systems, including Windows, macOS, and Linux.
Python 2 vs. Python 3
Python has two major versions: Python 2 and Python 3. While Python 2 is still used in some legacy systems, it is recommended to use Python 3 for new projects as it has several key differences and improvements. Here are some of the main differences (no need to memorize these):
- Print statement: In Python 2, the
print
statement is used without parentheses, whereas in Python 3 it requires parentheses. For example,print "Hello, World!"
in Python 2 becomesprint("Hello, World!")
in Python 3. - Division: In Python 2, dividing two integers with the
/
operator returns an integer, whereas in Python 3 it returns a float. To get integer division in Python 3, use the//
operator. - Unicode: In Python 2, strings are represented as ASCII by default, whereas in Python 3 they are represented as Unicode by default. This means that Python 3 can handle a wider range of characters and is better suited for internationalization.
- Syntax: There are several minor differences in syntax between Python 2 and Python 3, such as changes to the
range
andxrange
functions, and the introduction of theyield from
statement in Python 3.
If you're starting a new project, it's generally recommended to use Python 3. However, if you're working on a legacy system that uses Python 2, it is important to be familiar with the former conventions.
Checking if Python is already installed
Before you download and install Python, you should check if it's already installed on your computer. Here's how to do it for Windows, macOS, and Linux:
Windows
- Open the Start menu and search for "cmd".
- Open the Command Prompt.
- Type
python
and press Enter. - If Python is installed, you should see a message that includes the version number, such as "Python 3.9.1".
macOS
- Open Terminal, which you can find in the Utilities folder of your Applications folder.
- Type
python
and press Enter. - If Python is installed, you should see a message that includes the version number, such as "Python 3.9.1".
Linux
- Open a terminal window.
- Type
python
orpython3
and press Enter. - If Python is installed, you should see a message that includes the version number, such as "Python 3.9.1".
Getting started with Python
If Python is not installed on your computer, you can download the latest version from the official website: https://www.python.org/downloads/.
Once you've installed Python, you can open up a terminal or command prompt and start typing Python code.
Hello, World!
Let's start with a classic "Hello, World!" program. This program will simply print the text "Hello, World!" to the console. Python can be run using a file and directly in the terminal.
Running Python using a file
- Open a text editor of your choice (e.g. Notepad, Sublime Text, VS Code, etc.).
- Type the following code into the editor:
print("Hello, World!")
- Save the file with a .py extension (e.g. hello.py).
- Open a terminal or command prompt.
- Navigate to the directory where you saved the file.
- Type the following command into the terminal:
python hello.py
- Press Enter.
- You should see "Hello, World!" printed in the terminal.
Running Python directly in the terminal
- Open a terminal or command prompt.
- Type the following command into the terminal:
python
- Press Enter.
- You should see a new prompt that looks like this:
>>>
- Type the following print code next to the prompt:
print("Hello, World!")
- Press Enter.
- You should see "Hello, World!" printed in the terminal.
- Type the following command into the prompt to exit:
exit()
- Press Enter.
Conclusion
That's it for this getting started tutorial on Python! We've covered the basics of the language, its uses, and how to get started with a simple "Hello, World!" program. From here, you can start exploring the vast world of Python and all the amazing things you can do with it.