>>> 3 + 4 7
You can issue commands in the Python interactive console. But it has a disadvantage: The program that you type is lost when the session ends. It is good for trying simple commands. But you need a way to save more complex programs somewhere.
Open your editor (You should have an editor installed. If not, follow the instructions in previous lesson.)
Create a new file in your editor and type the following code:
Then save the file as
pyladies(or whatever folder name you've created last lecture) directory.
If you can choose the encoding in your editor, you should use
If you can choose the file type, use
Some operating systems or editors hide extensions or add their own extensions.
You can check the real name using the command line.
Open your command line and change your current directory using the
cd command to
List what is in the directory using the command
ls (Mac or Linux) or
and check that the filename is really
hello.py and not, for example,
Open the command line and activate the virtual environment.
Change to the
~/pyladies/02 directory and issue the following command:
$ python hello.py
You have learned about the command line in
a previous lesson which shows how to change the current directory
You know how to activate the virtual environment since the lesson about
Do you see the greeting? If you do, you just wrote your first Python program!
If it does not work, make sure that:
(venv)in your command line prompt. If you do not see it, use the
source activatecommand that you have used in a previous lession.)
~/pyladies/02(you need to replace
~/pyladieswith the directory that you have created previously).
hello.pyfile contains the correct command, including quotes and parentheses.
>character in the command line – it is there to indicate that this is a command line. It is printed by the operating system after any program has finished. You type only:
If it still does not work, ask your coach.
Note about code style
It does not usually matter where you use space in Python inside a command.
print("Hello world!") has the same effect as:
print ( "Hello world!" )
It is however a good practice to follow some typical guidelines.
In English, we do not write a space after an opening parenthesis.
In Python, we do not write a space even between
The recommended style is:
The spaces between the quotes have a meaning: If you type
" Hello world!", then the extra spaces are printed.