>>> 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. Very often however, 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 Editor installation.)
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 to the subdirectory you created -
for example by 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.
Change to the
~/pyladies/02 directory and issue the following command:
You have learned about the command line in
a previous lesson which shows how to change the current directory
After you run
python3 hello.py you should see the program responding with writing
Hello world! into the terminal.
If it does not work, make sure that:
~/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 should type only:
If it still does not work, ask your coach for help!
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.