Introduction to Python Programming
Python Basics
Getting Started with Python
Before you can write Python code, you need a place to run it. You can download and install Python for free from the official website, python.org. This gives you the full language and a basic tool called IDLE to write and run scripts.
For a quicker start without installing anything, you can use an online Python interpreter. Websites like Replit or Google Colab let you write and run code directly in your browser. They're perfect for trying things out and learning the basics.
Once you're set up, you can run your first line of code. The classic first program for any language is one that prints "Hello, World!" to the screen. In Python, it's remarkably simple.
# This is a comment. Python ignores lines starting with a #.
print("Hello, World!")
That's it. The print() function is a built-in tool that displays whatever you put inside its parentheses on the screen.
Syntax and Spacing
Every language has grammar rules, and in programming, these rules are called syntax. Python's syntax is known for being clean and readable, partly because of one important rule: indentation matters.
Unlike many other languages that use brackets or keywords to group code, Python uses whitespace. The amount of space at the beginning of a line is significant. Lines that are part of the same code block must have the same level of indentation.
You won't see complex code blocks just yet, but this is a fundamental concept to remember. Incorrect indentation is one of the most common errors for beginners.
In Python, whitespace isn't just for looks—it's part of the grammar. Consistent indentation is required for the code to run correctly.
Storing Information
Programming is all about working with data. To keep track of information, we store it in variables. Think of a variable as a labeled box where you can put a piece of data. You create a variable by giving it a name and assigning it a value using the equals sign (=).
message = "Welcome to Python!"
user_age = 25
price = 19.99
Here, message, user_age, and price are variable names. The data they hold has a specific type.
| Data Type | Description | Example |
|---|---|---|
String (str) | A sequence of characters, like text. | "Hello", 'Python' |
Integer (int) | A whole number, without a decimal. | 10, -5, 0 |
Float (float) | A number with a decimal point. | 3.14, -0.5 |
Boolean (bool) | Represents one of two values: True or False. | True, False |
Python automatically figures out the data type when you assign a value. You can change a variable's value and even its type later on.
Operators and Expressions
Once you have data, you can perform operations on it. Operators are special symbols that perform computations, like addition or subtraction. Combining variables, values, and operators creates an expression.
An expression is a combination of values, variables, and operators that Python evaluates to produce a result.
Arithmetic operators work just like in math.
sum = 5 + 3 # result is 8
difference = 10 - 4 # result is 6
product = 7 * 6 # result is 42
quotient = 20 / 5 # result is 4.0
# The exponent operator ** raises a number to a power
square = 5 ** 2 # 5 to the power of 2, result is 25
You can also use operators to compare values. These expressions always evaluate to a Boolean: True or False.
is_equal = (5 == 5) # True. Note the double equals for comparison.
is_not_equal = (5 != 6) # True
is_greater = (10 > 2) # True
is_less_equal = (4 <= 4) # True
Input and Output
We've already seen the print() function, which is Python's primary way to output information to the user. You can print variables, raw values, or even the result of expressions.
name = "Alex"
print("Hello, my name is", name)
print("5 + 3 is:", 5 + 3)
To get input from a user, we use the input() function. It prompts the user to type something and then returns their entry as a string.
# The text inside input() is the prompt shown to the user
user_name = input("What is your name? ")
print("Nice to meet you,", user_name)
One important detail: input() always gives you a string, even if the user types a number. If you want to treat it as a number, you need to convert it first using int() or float().
age_string = input("How old are you? ")
age_number = int(age_string) # Convert the string to an integer
years_until_100 = 100 - age_number
print("You will be 100 in", years_until_100, "years.")
Let's check your understanding of these core concepts.
What is a common way to start writing Python code immediately without installing anything on your computer?
In Python, how are blocks of code, such as the contents of a loop or a function, defined and grouped?