Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can write Python, you need a place to write and run it. The first step is to install Python itself from the official website, python.org. This gives your computer the ability to understand and execute Python code.
Next, you'll need a code editor or an Integrated Development Environment (IDE). These are programs designed to make writing code easier. Popular options include Visual Studio Code, PyCharm, and Thonny. They offer features like syntax highlighting, which colors your code to make it more readable, and debugging tools to help you find errors.
To keep things simple for now, you can also use an online Python editor. Websites like Replit or Google Colab let you write and run code directly in your browser, no installation required. This is a great way to get started immediately.
Syntax and Your First Command
Every language has rules for grammar and punctuation, and programming languages are no different. These rules are called syntax. In Python, the syntax is known for being clean and readable.
One of Python's most distinctive features is its use of indentation, meaning the spaces at the beginning of a line. Unlike many other languages that use brackets or keywords to group code, Python uses whitespace. For now, just know that consistent indentation is crucial. We'll see why it matters more as we learn about more complex structures.
Let's write our first piece of code. The most basic and traditional first step is to make the computer say something. In Python, we do this with the print() function. A function is a reusable piece of code that performs a specific action. The print() function's action is to display information on the screen.
print("Hello, Python!")
When you run this line, the text inside the parentheses and quotation marks, Hello, Python!, will appear as output. Congratulations, you've just written your first Python program.
Storing Information
Programs need to store and manage information. We do this using variables. Think of a variable as a labeled box where you can keep a piece of data. You give the box a name, and you can put things in it, take them out, or replace them with something new.
Creating a variable is simple. You choose a name, use the equals sign (=), and provide the value you want to store.
greeting = "Welcome to programming!"
Here, greeting is the variable name, and it now holds the text "Welcome to programming!". We can use the print() function to see what's inside our variable.
print(greeting)
The information stored in variables comes in different forms, called data types. Let's look at the most common ones.
- Strings: Used for text. Any sequence of characters inside single (
') or double (") quotes is a string."Python"and'learning'are both strings.- Integers: Whole numbers, like
-5,0, and100. They don't have decimal points.- Floats: Numbers with a decimal point, like
3.14,-0.5, or99.0. The name comes from "floating-point number."- Booleans: Can only have one of two values:
TrueorFalse. They are essential for making decisions in your code.
# An integer
user_age = 25
# A float
item_price = 19.99
# A string
user_name = "Alex"
# A boolean
is_learning = True
Python automatically figures out the data type when you assign a value to a variable. This makes the language flexible and easy to use.
Interacting with Users
Besides showing output with print(), we can also get input from a user. The input() function pauses the program and waits for the user to type something and press Enter. It's a great way to make your programs interactive.
The input() function can take a string as an argument, which it will display as a prompt to the user.
user_name = input("What is your name? ")
print("Hello, " + user_name + "!")
In this example, the program first asks "What is your name? ". Whatever the user types is then stored in the user_name variable. Finally, it combines "Hello, ", the user's name, and "!" into a new string and prints a personalized greeting.
One important detail: the input() function always returns the user's input as a string, even if they type a number. If you need to perform math with that number, you'll first have to convert it to an integer or a float, but we'll get to that later.
Time to test what you've learned.
Which function is used to display text on the screen in Python?
What is the correct way to store the text "Hello" in a variable named message?
You now have the fundamental building blocks for writing simple Python programs. You can set up your environment, display messages, store data, and get input from a user.
