Introduction to Python Programming
Python Basics
Setting Up Your Workspace
Before you can write Python code, you need a place to write and run it. When you install Python on your computer, it often comes with a simple program called IDLE (Integrated Development and Learning Environment). Think of it as a basic text editor that understands Python. It lets you write code, save it in files, and run it to see the results.
There are many other powerful code editors and IDEs available, like VS Code, PyCharm, and Sublime Text. For now, IDLE is all you need to get started. When you open it, you’ll see a window called the Python Shell, which lets you run commands one at a time. To write a full program, you’ll want to open a new file (File > New File).
Syntax, Indentation, and Comments
Every language has rules, and programming languages are no different. These rules are called syntax. Python is known for its clean and readable syntax. One of its most distinctive features is its use of indentation—the spaces at the beginning of a line of code.
Unlike many other languages that use brackets or keywords to group code, Python uses whitespace. For now, this just means you should start your lines of code at the far left, with no leading spaces. If you indent when you aren't supposed to, Python will give you an error. The importance of indentation will become much clearer when we start grouping code into blocks.
In Python, indentation isn't just for style. It's a rule that the computer uses to understand the structure of your program.
You can also leave notes in your code that the computer will ignore. These are called comments, and they start with a hash symbol (#). Comments are for humans; they help you and others understand what your code is supposed to do.
# This is a comment. Python ignores it.
# Use comments to explain complex parts of your code.
Variables and Data Types
A program needs to work with information, like numbers, text, or more complex data. We store this information in variables. Think of a variable as a labeled box where you can put something. You give the box a name, and you can put data inside it, take it out, or replace it with something new.
To create a variable in Python, you just choose a name, use the equals sign (=), and provide the data you want to store.
# Assign the text "Ada Lovelace" to a variable named 'pioneer'
_pioneer = "Ada Lovelace"
# Assign the number 1815 to a variable named 'birth_year'
birth_year = 1815
Notice that variable names can be descriptive. It's a good practice to choose names that reflect the data they hold. There are a few rules for naming variables: they must start with a letter or an underscore (_), can't be a reserved Python keyword (like if or for), and are case-sensitive (age is different from Age).
Data comes in different forms, or types. Python automatically figures out the type of data you assign to a variable. The most common basic types are:
| Data Type | Description | Example |
|---|---|---|
| String | A sequence of characters (text) | "Hello, Python!" |
| Integer | A whole number | 42 |
| Float | A number with a decimal point | 3.14159 |
| Boolean | A value that is either True or False | True |
Knowing the data type is important because it determines what you can do with the variable. For instance, you can perform math operations on integers and floats, but not on strings.
Input and Output
Most programs need to interact with the user. This involves showing information to the user (output) and getting information from the user (input).
To display output, you use the print() function. You can give it text directly, or you can give it a variable, and it will print the value stored in that variable.
planet = "Earth"
moons = 1
# Print the value of the 'planet' variable
print(planet)
# Print the value of the 'moons' variable
print(moons)
To get input from a user, you can use the input() function. This function displays a prompt to the user, waits for them to type something and press Enter, and then returns whatever they typed as a string.
name = input("What is your name? ")
print("Hello, " + name + "!")
When you run this code, it will first ask "What is your name? ". After you type your name and press Enter, the program will greet you. The + symbol here is used to combine, or concatenate, the strings. We'll explore more operations like this soon.
What is the primary role of the hash symbol (#) in Python code?
Which of the following is a valid variable name in Python?
