Introduction to Python Programming
Python Basics
Getting Started with Python
Before you can write Python, you need a place to write and run it. Think of this as getting your kitchen ready before you start cooking. You have two main options: install Python on your computer or use an online tool.
Installing Python gives you the full setup, which is great for big projects. You’d also use a text editor, a simple program for writing code, or an Integrated Development Environment (IDE), which is like a text editor with extra tools built in. For now, let’s keep it simple. Using an online Python interpreter is the fastest way to start. It’s just a website where you can type code and see it run instantly, with no setup required.
The Rules of the Road
Every language has rules. In English, we use punctuation and grammar to make our sentences clear. Python has its own set of rules, called syntax. If you break the rules, the program won't understand what you mean.
One of Python's most important and unique rules is about indentation. The spaces at the beginning of a line are not just for looks; they are part of the syntax. Indentation is used to group statements together. Get it wrong, and your code won't run. For now, just remember not to put extra spaces or tabs at the beginning of a line unless a specific structure requires it.
Storing Information
To do anything useful, a program needs to remember information. We store 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. You can also change what's inside the box later.
Creating a variable is simple. You just pick a name, use the equals sign (=), and provide the data you want to store.
# The variable 'name' now holds the text "Alice".
name = "Alice"
# The variable 'age' now holds the number 30.
age = 30
The kind of data you store is called its data type. Python has several built-in types, but let's start with the most common ones.
| Data Type | What It Is | Example |
|---|---|---|
| String | Textual data | "Hello, world!" |
| Integer | Whole numbers | 42 |
| Float | Numbers with decimals | 3.14159 |
| Boolean | Represents truth values | True or False |
Python is smart enough to figure out the data type on its own when you create a variable. You don't need to tell it that "Alice" is text or that 30 is a whole number.
Talking to Your Program
Programs would be pretty boring if they couldn't communicate. The most basic way to make a program 'talk' is to have it display information on the screen. In Python, we do this with the print() function.
A function is a named block of code that performs a specific task. The print() function's job is to show output. Whatever you put inside the parentheses will be displayed.
# This will display the text Hello, World! on the screen.
print("Hello, World!")
You can also print the value stored in a variable.
user_greeting = "Welcome to Python!"
print(user_greeting)
Getting information from the user is just as important. For that, we use the input() function. This function pauses your program, waits for the user to type something and press Enter, and then returns whatever they typed as a string.
It's important to know that the
input()function always gives you back a string, even if the user types a number.
# Ask the user for their name and store it in the 'name' variable.
name = input("What is your name? ")
# Greet the user by name.
print("Hello, " + name + "!")
Here, the program asks a question, stores the answer in the name variable, and then uses that variable to print a personalized greeting. You've just learned how to have a basic conversation with your program.
What is the fastest way to start writing and running Python code with no setup required?
In Python, the spaces at the beginning of a line (indentation) are crucial because they are part of the language's syntax.
