Introduction to Python Programming
Python Basics
The Rules of the Language
Every language has rules. In English, we use punctuation and paragraphs to make our sentences clear. Programming languages have similar rules, which are called syntax. Python is known for having a simple, readable syntax that often looks a lot like plain English.
One of Python's most important syntax rules is indentation. Indentation is the space at the beginning of a line of code. Where other languages might use brackets or keywords to group code, Python uses whitespace. This makes the code clean and easy to read. Getting the indentation right is essential; if it’s wrong, your program won't run.
Think of indentation like an outline for an essay. Major points are on the left, and supporting details are indented underneath them. It visually organizes the structure.
Storing Information
To do anything useful, a program needs to remember information. We do this using variables. A variable is like a labeled box where you can store a piece of data. You give the box a name, and you can put things in it, take things out, or just see what's inside.
Creating a variable in Python is simple. You just choose a name, use the equals sign (=), and give it a value.
# Assigning a value to a variable named 'greeting'
greeting = "Hello there!"
# Assigning a number to a variable named 'user_age'
user_age = 25
Once you've stored data in a variable, you can use the variable's name to access it. For example, you can print the value to the screen to see what's inside.
Talking to Your Program
Programs aren't very interesting if they can't communicate. The most basic way for a program to communicate with you is by printing output to the screen. In Python, we use the print() function for this.
You can print simple text, or you can print the value stored in a variable.
# Storing a name in a variable
user_name = "Alex"
# Printing a greeting using the variable
print("Welcome,", user_name)
Communication goes both ways. You also need a way to give information to your program while it's running. For that, we use the input() function. This function displays a message to the user and waits for them to type something and press Enter.
Whatever the user types is then returned by the function, and you can store it in a variable.
# Ask the user for their favorite color
favorite_color = input("What is your favorite color? ")
# Print a response using their input
print(favorite_color, "is a great color!")
Let's put all these pieces together into a single, simple script. This program will ask for your name and then use it to say hello. This is your first interactive Python script.
# 1. Ask the user for their name and store it
name = input("Please enter your name: ")
# 2. Print a personalized greeting
print("Hello,", name, "! Welcome to Python.")
Now, it's time to check what you've learned.
What is the primary role of indentation in Python?
Which of the following correctly creates a variable named user_age and assigns the value 25 to it?
You've now covered the foundational rules of Python: its syntax, how to store information in variables, and how to communicate with your program. These are the essential building blocks for everything that comes next.
