Introduction to Python Programming
Python Basics
Getting Ready to Code
Think of Python as a language that lets you give instructions to a computer. Before you can start talking to it, you need two things: the Python interpreter and a place to write your code.
The interpreter is what translates your Python code into a language the computer's hardware understands. You can download it for free from the official website, python.org. The installation is straightforward, much like any other software.
Once Python is installed, you need a text editor or an Integrated Development Environment (IDE) to write your scripts. While you could use a basic text editor, an IDE like Visual Studio Code, PyCharm, or Thonny offers helpful features like syntax highlighting and error checking, which are great for beginners.
Your First Program
It's a tradition for a programmer's first program to simply display the text "Hello, World!" on the screen. In Python, this is incredibly simple. Just open your editor, type the following line, and save the file with a .py extension, like hello.py.
print("Hello, World!")
To run the script, you'll typically open a terminal or command prompt, navigate to the directory where you saved your file, and type python hello.py.
You should see Hello, World! printed in the terminal. That's it! You've written and executed your first Python program. The print() command is a built-in function that tells the computer to display whatever you put inside the parentheses.
Syntax and Structure
Every language has rules, and Python is no different. Its rules, or syntax, are known for being clean and readable. Two of the most important rules involve indentation and comments.
Unlike many other programming languages that use brackets or keywords to define blocks of code, Python uses indentation. This isn't just for style; it's a strict rule.
Forgetting to indent or indenting inconsistently will cause your program to fail. The standard is to use four spaces for each level of indentation. Most code editors can be configured to insert four spaces automatically when you press the Tab key.
Another key part of writing good code is leaving comments. Comments are notes for yourself or other programmers that the Python interpreter completely ignores. They're used to explain what your code does, making it easier to understand later. In Python, a comment starts with a hash symbol (#).
# This is a comment. The interpreter will ignore it.
print("This line will be executed.") # This is an inline comment.
Storing Information
To do anything useful, programs need to work with data. We store data in variables. You can think of a variable as a labeled box where you can put information. To create a variable, you give it a name and use the equals sign (=) to assign it a value.
message = "Welcome to Python!"
user_age = 25
pi = 3.14
Here, we created three variables: message, user_age, and pi. You can change the value of a variable at any time. The type of data you store determines what you can do with it. Let's look at the most basic data types.
| Data Type | Description | Example |
|---|---|---|
| Integer | A whole number | 42, -100 |
| Float | A number with a decimal point | 3.14, -0.001 |
| String | A sequence of text | "hello", 'Python' |
| Boolean | A value of True or False | True, False |
Python automatically detects the data type when you assign a value to a variable. Integers and floats are numeric types used for mathematical calculations. Strings are for text and must be enclosed in either single (') or double (") quotes. Booleans are fundamental for logic and decision-making, representing truth values.
Time to check your understanding of these core concepts.
What is the primary role of the Python interpreter?
Which line of Python code correctly prints the text "Hello, World!" to the screen?
Getting comfortable with variables and data types is the first big step in learning to program. They are the building blocks for all the complex and interesting things you'll learn to do next.
