Introduction to Python Programming
Python Basics
Your First Python Program
Let's jump right in and write a computer program. It's a tradition for a programmer's first program to simply display the text "Hello, World!" on the screen. In Python, it looks like this:
print("Hello, World!")
That's it. That's the entire program. The word print is a built-in Python command, or function, that tells the computer to display whatever you put inside the parentheses (). Because we want to display text, we wrap it in double quotes "".
To run this program, you would save the text in a file, perhaps named hello.py. The .py extension tells your computer that it's a Python file. Then, you'd open a terminal or command prompt, navigate to the folder where you saved the file, and type python hello.py. The computer will execute the code and you'll see Hello, World! printed on your screen.
Python's Rules of Grammar
Like any language, Python has rules about how you can write it. This is called syntax. If you break the rules, Python won't understand what you're trying to do and will give you an error. Think of it like human grammar. If you say "Me store to go," people might figure out what you mean, but it's not correct. Python is much stricter. It needs the syntax to be perfect.
Python executes your code one line at a time, from top to bottom. Each new command generally goes on a new line.
One unique rule in Python is its use of whitespace. Unlike many other programming languages that use brackets or keywords to group code, Python uses indentation. This means the empty space at the beginning of a line is very important. For now, just remember not to indent lines of code unless you have a specific reason to. We'll explore those reasons later on.
Leaving Notes in Your Code
Sometimes you want to leave notes in your code for yourself or other programmers. These are called comments. The Python interpreter completely ignores them; they're just for human readers. Comments are helpful for explaining what a tricky piece of code does or why you wrote it a certain way.
To write a comment, you just start the line with a hash symbol (#). Anything after the # on that same line is ignored.
# This is a comment. Python will ignore it.
print("This is code.") # You can also add comments after your code.
This program will only print "This is code." The comments are invisible to the computer.
If you need to write a longer, multi-line comment, you can use triple quotes. While these have a more formal use we'll learn about later, they work well for blocking out large chunks of text.
"""
This is a multi-line comment.
You can write as many lines as you want here,
and Python will ignore all of them.
"""
print("Let's get started!")
Time to check what you've learned.
What is the primary function in Python used to display text on the screen?
What is the term for the set of rules that define how a Python program must be written?
Now you know the very basics of how to write and document a simple Python program. You've written your first line of code and learned how Python reads it.
