Introduction to Python Programming
Python Basics
Meet Python
Python is a programming language created in the late 1980s by a Dutch programmer named Guido van Rossum. He wanted to build a language that was easy to read and write. The goal was to let programmers write clear, logical code for projects big and small. Its name doesn't come from a snake, but from the British comedy troupe Monty Python's Flying Circus.
The core philosophy of Python is often summarized as the Zen of Python, which values beauty, simplicity, and readability over complexity.
Setting Up Your Workspace
Before you can write Python code, you need a Python interpreter installed on your computer. The interpreter is a program that reads your Python code and carries out its instructions. You can find official installers on the python.org website for Windows, macOS, and Linux.
Once installed, you can interact with Python in two main ways. You can open an interactive shell (also called a REPL), where you type commands one by one and see immediate results. Or, you can write your code in a text file (usually ending with .py) and run the entire file through the interpreter.
Syntax, Variables, and Data
Python's syntax is designed to be clean and uncluttered. One of its most distinctive features is the use of indentation. Where other languages might use brackets or keywords to group code, Python uses whitespace. This enforces a readable style because the visual structure of the code reflects its logical structure. We'll see how this works later on when we look at more complex code.
The most basic building blocks of any program are variables. Think of a variable as a labeled box where you can store a piece of information. You give the box a name, and you can put data inside it, look at it, or change it later.
message = "Hello, Python!"
user_age = 30
Here, message and user_age are variable names. We've used the assignment operator (=) to store data in them. The data itself has a type.
string
noun
A sequence of characters, used to represent text.
In the example above, "Hello, Python!" is a string. You can create strings using either single quotes ('...') or double quotes ("...").
integer
noun
A whole number, not a fraction. It can be positive, negative, or zero.
The value 30 stored in user_age is an integer. Integers are for numbers without decimal points.
float
noun
A number that contains a decimal point.
For numbers with decimal points, we use floats. For example, price = 19.99 creates a float.
boolean
adjective
A data type that has one of two possible values, true or false.
Booleans are used for representing truth values. They can only be True or False (with a capital T and F). For instance, is_logged_in = True.
Input and Output
Programs need to communicate. The simplest way to show information to a user is by printing it to the screen. In Python, we use the print() function for this.
# This is a comment. The interpreter ignores it.
# Printing a simple string
print("Hello, World!")
# Printing the value of a variable
planet = "Earth"
print(planet)
You can also get information from the user. The input() function pauses the program, displays a prompt to the user, and waits for them to type something and press Enter. Whatever the user types is returned as a string.
# Ask the user for their name
user_name = input("What is your name? ")
# Create a personalized greeting
welcome_message = "Hello, " + user_name + "!"
# Print the greeting
print(welcome_message)
If you run this code, it will ask for your name, and then greet you personally. This simple example combines everything we've learned: getting input, storing it in a variable, combining strings, and printing the result.
Who is the creator of the Python programming language?
In Python, what is the primary purpose of indentation?
