What is Python?
Python is a programming language. A programming language is a way to tell a computer what to do. A script is a small program saved in a text file. Python is popular because its code is easy to read. People use it for websites, tools and data work.
What you need
- Python installed on your computer or server. Ask Hostvento support whether Python is on your hosting plan.
- A plain text editor, such as Notepad.
- For a server, an SSH client. SSH is a safe way to type commands on a faraway computer.
Your first script
Open your text editor and type:
name = "Sam"
print("Hello, " + name)
The first line stores the word "Sam" in a box called name. That box is called a variable. The second line prints a greeting.
Save the file as hello.py. The .py ending tells everyone it is Python.
Steps to run it
- Open a command line. On Windows, open Command Prompt. On a server, log in with SSH.
- Go to the folder that holds your file. Use the
cdcommand, for example:
This command moves you into the folder namedcd myscriptsmyscripts. - Run the script:
This command asks Python to read your file and do what it says. On Windows you may typepython3 hello.pypython hello.pyinstead. - Read the output. You should see
Hello, Sam.
Four ideas to learn next
- Variables hold information.
- If statements let the script choose between actions.
- Loops repeat an action many times.
- Functions are small reusable blocks of code.
Good to know
Python cares about spaces at the start of a line. This is called indentation. Lines inside an if statement or loop must be pushed in by the same amount, usually four spaces. A wrong space gives an error.
Quick recap
- Python is an easy language for writing scripts.
- Save scripts with the
.pyending. - Run them with
python3 filename.py. - Watch your indentation.
- Ask Hostvento support whether Python is on your plan.