Python Programming Fundamentals
Python Basics
Getting Started with Python
Before you can write any code, you need a place to write and run it. This is your development environment. Python is a programming language, and to use it, you need to install its interpreter on your computer. The interpreter is a program that reads your Python code and carries out its instructions.
Many computers, especially those running macOS or Linux, come with Python pre-installed. However, it might be an older version. It's best to have the latest version. You can download it for free from the official Python website, python.org.
When you install Python, it also comes with a simple program called IDLE (Integrated Development and Learning Environment). Think of IDLE as a basic text editor and a command window combined. It’s a great place to start writing your first lines of code and see immediate results.
Syntax and Spacing
Python's syntax was designed to be clean and readable. One of its most famous features is how it uses indentation—the spaces at the beginning of a line. Unlike many other languages that use curly braces {} to group code, Python uses whitespace. This might seem strange at first, but it forces code to be written in a structured, clean way.
For now, just remember that the spacing in your code matters. If a line of code has incorrect indentation, your program won't run. The standard is to use four spaces for each level of indentation. We'll see why this is so important when we start writing more complex programs.
Storing and Displaying Information
Programming is all about working with data. To keep track of data, we store it in variables. A variable is like a labeled box where you can put information. You can give a variable a name and assign it a value using the equals sign =.
# Assigning a value to a variable
message = "Hello, Python!"
Here, message is the name of our variable, and it holds the text "Hello, Python!". This text is called a string, which is one of Python’s basic data types.
Variable names can't start with a number and can't contain spaces. Use an underscore
_to separate words, likeuser_name.
Python has several fundamental data types to handle different kinds of information:
| Data Type | Description | Example |
|---|---|---|
| String | Text | "Hello" |
| Integer | Whole numbers | 10, -5 |
| Float | Numbers with decimals | 3.14, -0.5 |
| Boolean | True or False | True, False |
To show information to the user, you can use the print() function. It displays whatever you put inside its parentheses on the screen.
# Storing different data types
book_title = "The Hitchhiker's Guide to the Galaxy"
publication_year = 1979
rating = 4.8
is_classic = True
# Printing the variables
print(book_title)
print(publication_year)
print(rating)
print(is_classic)
Interacting with the User
Besides displaying information, you can also ask the user to provide some. The input() function lets you do this. It pauses the program and waits for the user to type something and press Enter.
You can include a message inside the parentheses to prompt the user. The value the user types is then returned as a string, which you can store in a variable.
# Ask the user for their name
user_name = input("What is your name? ")
# Greet the user
print("Hello, " + user_name + "!")
Notice how we used the
+operator to combine strings. This is called string concatenation. It joins strings together to create a new, longer string.
One important thing to know is that input() always gives you a string, even if the user types a number. If you need to perform math with that number, you have to convert it first. You can use int() to convert a string to an integer and float() to convert it to a float.
# Get the user's age as a string
age_string = input("How old are you? ")
# Convert the string to an integer
age_number = int(age_string)
# Calculate age in 10 years
future_age = age_number + 10
# Print the result
print("In 10 years, you will be", future_age)
In the final print() statement, we passed two items separated by a comma: a string and the future_age variable. When you do this, print() automatically adds a space between them, which can be a handy way to display mixed data types.
What is IDLE, which is typically included with a standard Python installation?
In Python, indentation is used to define blocks of code and is syntactically significant, unlike in many other languages that use curly braces {}.
You've just taken your first steps into Python. You know how to set up your environment, write simple statements, use variables to store data, and interact with a user. These are the essential building blocks for everything that comes next.
