Using the Python Console
Now that we have Spyder up and running, let's see how to write a basic program. In the Python console, type print("Hello, world!"). You should see your text echoed back from the console. Congratulations, you have just written your first piece of Python code! Let's break it down a little bit.
print() is a function in Python. It takes certain parameters, and outputs to the screen. In this case, we passed "Hello, world!" as a parameter, and print() simply printed our data to the screen. Note that the parameters of a function go in parentheses.
The print() function is a part of Python's standard library. A library is a collection of functions and data structures that can be used in your projects. The standard library is always ready to be used when you are writing Python code, and you can call a standard library function any time you like. We will be learning about more of the available functions in the standard library as we need them, and we will use third party libraries that help us accomplish complicated tasks. We will also start writing our own functions soon.
The " " tell Python that Hello, world! is a string. Strings are how text is stored in Python. You can use either double or single quotes (" or '), as long as they match! Experiment with the print() function by passing it different strings, using single and double quotes. Strings are one type of data, called str in Python. We will be using other data types in the next tutorial, and eventually making our own to suit our needs.
Tip: if the console gets full of statements, you can type clear to wipe the screen and start from a blank slate. This only clears the console, and preserves any data that you have stored. You can also use the up arrow to find the last commands you ran.
The print() function is a part of Python's standard library. A library is a collection of functions and data structures that can be used in your projects. The standard library is always ready to be used when you are writing Python code, and you can call a standard library function any time you like. We will be learning about more of the available functions in the standard library as we need them, and we will use third party libraries that help us accomplish complicated tasks. We will also start writing our own functions soon.
The " " tell Python that Hello, world! is a string. Strings are how text is stored in Python. You can use either double or single quotes (" or '), as long as they match! Experiment with the print() function by passing it different strings, using single and double quotes. Strings are one type of data, called str in Python. We will be using other data types in the next tutorial, and eventually making our own to suit our needs.
Tip: if the console gets full of statements, you can type clear to wipe the screen and start from a blank slate. This only clears the console, and preserves any data that you have stored. You can also use the up arrow to find the last commands you ran.
Scripts
We have been writing our code in the Python console, where what we write is interpreted line-by-line, or in short blocks. Most of time, you will be writing whole files and running them. This makes it easier to edit and fix your code, and is far more convenient when your task requires more than one or two lines of code.
Back in Spyder, navigate to the Editor pane, and delete any placeholder text, and write our print() statement as above. Save the file as hello.py, then click the green triangle (or F5) to run your new script. You'll see that the script output pops up in the python console.
Back in Spyder, navigate to the Editor pane, and delete any placeholder text, and write our print() statement as above. Save the file as hello.py, then click the green triangle (or F5) to run your new script. You'll see that the script output pops up in the python console.
All Python files have to end with .py. This lets Python know that the file is filled with Python code, and that it is able to run it. Play around with the hello.py script by adding more lines of print() statements and running it. How is each statement printed to the console?
Once you are satisfied, let's move on to the next tutorial to learn about basic math operations and using Python modules.
Once you are satisfied, let's move on to the next tutorial to learn about basic math operations and using Python modules.