Write and run your first program in the command line
Now that you are set up, it is time to write your first line of code in Python! This line of code is almost a tradition for people who are programming for the first time, and it allows us to use one of the most basic, but most useful, functions in the Python language.
First, you need to start running a Python shell. On Mac or Linux, open your terminal and type this:
python
In the Mac or Ubuntu terminal, your resulting Python shell will look like this:
>>>
In Windows, type Python
in the search bar at the bottom of the page. Then, select Python 2.7.11 from your apps. You will also have a Python shell open:
>>>
Once you see this symbol, your computer is now ready to work with the Python code. In your terminal or IDLE, type the following:
>>>print("Hello, world!")
Once you have typed this, double-check to make sure that all of the spaces are exactly as they've been written. In Python, every space actually matters. Every punctuation mark matters. Once you have checked your code, hit Enter.
What is your result or the output of your code? If the output looks like the following image, then great! You typed all of your code properly so the computer will understand what you want it to do. The expected output will be similar to what is shown here:
For Windows users, the output window will look like this:
So, if your output does not look like the preceding code, you need to figure out what's wrong with it. Here are some of the reasons for this:
- Did you make a typing error?
- Did you forget to use parenthesis or round brackets () for the words
'Hello, world!'
? - Did you forget to use the
''
single quotation marks forHello, world!
?
If you still have a problem, compare your code to the sample input code and fix any mistakes. Then, try to run the code again.
Note
Python is what is called a case-sensitive language. Python cares about uppercase, lowercase, and whitespace. You need to watch what you type. You might get some strange messages from your computer if you make a typing mistake or a syntax error.