This is a machine-generated translation. If you're not at the in-person workshop, try the DjangoGirls tutorial for an intro to Python!
So far, you have written all programs in the console in the interactive mode of Python, in which Python always responded with a result after a command was written. When you exit Python (or turn off the computer), everything you have programmed so far will be lost.
Larger programs are more durable: they are saved in files and can be run again anytime.
It's time to try it out. You will need:
As a first step, turn off Python. There is a function for that:
So you'll get back to the command line. Do you remember it? You won't see
>>> anymore, but a line ending with
>. Commands like
mkdir will work here, but not Python commands like
1 + 1.
I hope you have installed a text editor. Now open it, create a new file and write this command into it:
Save a new file under a descriptive name:
Save it to a directory where you keep files for this workshop.
The name must end with
.py: this extension tells the editor or operating system that it is a Python program and Python can run it.
After saving, the text should be colored. In interactive Python mode, everything should have the same color, but now you should see that the function name
del. That's one of the reasons we use programming editors :)
If you have the file saved, it's time to run it! Using the skills you learned in the command line section, change the directory to where you saved the file. "
Now, using Python, run the code in the file: enter the command
python, a space and the name of the file to run. (It's similar to the
cd command for a specific directory -
(venv) $ python python_intro.py Hello, PyLadies!
This is an output from running a Python script called
python_intro.py while in a virtual environment (
venv). The script outputs the message "Hello, PyLadies!" to the console.
Does it work? Can you see the text? If yes, you have just run your first real Python program! Do you feel awesome?
print() that you used can output something on the screen. In the console, the values of expressions were automatically printed so that you could check them continuously, but programs in files tend to be more complex and outputs from every step would be confusing. Therefore, you need
print() for outputting. Try the following program:
name = 'Ola' "I am" + name # Python will not display this. print(name * 8) # This is displayed!
Then run the program.
(venv) $ python python_intro.py OlaOlaOlaOlaOlaOlaOlaOla
What happened? When you run a program file, Python goes through it from top to bottom, line by line, executing individual commands:
nameto the string
'Ola', but discards the result,
'Ola'eight times and prints the result using
You can put multiple values separated by commas inside the parentheses of the
print() function. Try changing the contents of the file to the following program and run it again:
name = 'Amálka' age = 5 print('I am', jmeno, 'and I am', vek, 'years old') print('Next year I will be', vek + 1)
Another useful function is
input(), which can ask a question. It will then return the answer as a string, which you can save to a variable.
name = input('What is your name?') print(name, 'knows how to program!')
When you run the program, it will ask you a question. Enter the answer directly into the command line using your keyboard. Python will read it and use it as the result of the
input function, which will store it in the
name variable. And as part of the next line of the program, it will print it out.
(venv) $ python python_intro.py What's your name? Ola Ola knows how to program! (venv) $ python python_intro.py What's your name? Princess Princess knows how to program!
input() from the keyboard always returns text: when the user enters
1234, Python takes it as a string of digits 1, 2, 3, 4.
When you want to load a number from the keyboard instead of text, you have to use a function that can convert a string to a number:
year = int(input('What is the current year? ')) print('Last year was', year - 1)
Did you notice in one of the previous program notes the symbol
name = 'Ola' "I am" + name # This will not be printed by Python. print(name * 8) # This is it!
These are so-called "comments". They are intended for humans only: Python completely ignores them.
"Now tha you are saving your programs to the disk and can return to them, it is important that they are readable: so that not only computers, but also people can recognize what those instructions are supposed to do. Whenever you write a more complex piece of code, try to add a comment with an explanation. When you return to the program in a few days or months, you will thank yourself!
The following is the English translation of the Czech text: