Introduction to Python Programming
Python Basics
Getting Started with Python
Python is a popular programming language known for its clear, readable syntax. It’s used for everything from web development and data analysis to artificial intelligence. Before you can write any code, you need to have the Python interpreter installed on your computer. This is the program that reads your Python code and carries out its instructions.
You can download the latest version from the official website, python.org. Make sure you get a version of Python 3, as Python 2 is no longer supported.
Once Python is installed, you need a place to write your code. You can use any plain text editor, but most programmers use an Integrated Development Environment (IDE). An IDE is a text editor with extra features like syntax highlighting and debugging tools. Python comes with a simple one called IDLE, which is great for beginners.
Your First Program
It's a tradition to start with a program that prints "Hello, World!" to the screen. This simple task confirms that your environment is set up correctly. In Python, we use the built-in print() function to display output.
print("Hello, World!")
Type that line into your editor, save the file as hello.py, and run it. If you see "Hello, World!" printed in your console or output window, you've successfully run your first Python program.
Unlike many other languages, Python doesn't require a semicolon at the end of each statement.
Syntax and Indentation
Python uses indentation to define blocks of code. Where other languages might use curly braces {} or keywords, Python uses whitespace. This is a strict rule. If your indentation is wrong, your program won't run.
For now, this just means you should start each new line of code at the very beginning of the line, without any leading spaces or tabs. We'll see why indentation is important when we get to more complex code structures later on.
You can add comments to your code using the hash symbol (#). The interpreter ignores everything on the line after a #. Comments are for humans to read, helping to explain what your code does.
# This is a comment. It won't be executed.
print("This line will be printed.")
Variables and Data Types
A variable is like a container for storing a piece of information. You give it a name and assign a value to it using the equals sign (=). This lets you refer to the data by name, making your code easier to read and manage.
# Assigning the value 10 to a variable named 'age'
age = 10
# Assigning the text "Alice" to a variable named 'name'
name = "Alice"
Python automatically detects the type of data you store in a variable. The most common basic types are:
| Data Type | Description | Example |
|---|---|---|
int | Integer (whole number) | 42 |
float | Floating-point number (decimal) | 3.14 |
str | String (text) | "Hello" |
bool | Boolean (True or False) | True |
You can check a variable's type using the type() function.
price = 19.99
print(type(price)) # This will output: <class 'float'>
Input and Output
We've already seen the print() function for output. To get input from a user, you can use the input() function. It prompts the user to type something and then returns their entry as a string.
# Ask the user for their name and store it in a variable
user_name = input("What is your name? ")
# Greet the user by name
print("Hello, " + user_name)
Notice how we can join strings together using the
+operator. This is called concatenation.
One important thing to know is that input() always returns a string, even if the user types a number. If you need to treat the input as a number, you have to convert it first.
age_str = input("How old are you? ") # age_str is a string, e.g., "25"
age_int = int(age_str) # Convert the string to an integer
next_year_age = age_int + 1
print("Next year you will be " + str(next_year_age))
Here, int() converts the string to an integer so we can do math with it. Then, we use str() to convert the result back to a string before printing it with the rest of the text.
Let's test your understanding of these basic concepts.
What is the primary role of indentation in Python?
Which line of code correctly prints the text Welcome! to the console?
With these fundamentals, you're ready to start building more complex programs.
