Python Programming Fundamentals
Python Basics
Getting Started with Python
Python is a versatile programming language known for its clear, readable syntax. It’s used everywhere, from building websites and analyzing data to creating games and automating tasks. Before you can write any code, you need to set up your programming environment.
First, you'll need to install Python on your computer. You can download the latest version directly from the official website, python.org. The installation process is straightforward; just follow the instructions in the installer. Make sure to check the box that says "Add Python to PATH" during installation on Windows, as this makes it easier to run Python from your command line.
You can write Python code in any plain text editor, but many developers use an Integrated Development Environment (IDE) like VS Code, PyCharm, or Thonny. These tools offer helpful features like syntax highlighting and debugging.
Writing Your First Lines
One of Python's defining features is its emphasis on readability. Unlike many other languages that use brackets or keywords to define blocks of code, Python uses indentation. The amount of space at the beginning of a line is not just for looks—it's a strict rule that tells Python how your code is structured.
In Python, whitespace matters. Consistent indentation is crucial for your code to run correctly.
Let’s start with the most basic command: printing something to the screen. The print() function is used to display output. To use it, you place the text you want to show inside the parentheses, enclosed in quotes.
print("Hello, World!")
# This line will display 'Hello, World!' in the output.
Programs aren't just about displaying information; they often need to receive it from a user. You can get input using the input() function. This function prompts the user for text and waits for them to type something and press Enter.
# The text inside input() is the prompt shown to the user.
name = input("What is your name? ")
# The program then prints a greeting using the input.
print("Nice to meet you, " + name)
Storing and Using Data
In the example above, we stored the user's name in something called a variable. Think of a variable as a labeled box where you can keep a piece of information. You give it a name and put a value inside. You can then refer to that information later just by using its name.
The kind of information a variable holds is its data type. Python automatically detects the type when you assign a value. The most common basic types are:
| Data Type | Description | Example Code |
|---|---|---|
String (str) | A sequence of characters, like text. | message = "Python is fun" |
Integer (int) | A whole number, without a decimal. | item_count = 5 |
Float (float) | A number with a decimal point. | pi_value = 3.14 |
Notice that strings are surrounded by quotes (either single ' or double "), while numbers are not. This is how Python tells them apart.
You can also explicitly convert data from one type to another. For example, the input() function always gives you a string, even if the user types a number. To treat that input as a number, you need to convert it.
age_string = input("How old are you? ")
# Convert the string to an integer
age_number = int(age_string)
# Now you can do math with it
years_until_100 = 100 - age_number
print(years_until_100)
variable
noun
A named storage location in a computer's memory that holds a value. The value can be changed during program execution.
Performing Operations
Once you have data, you can perform operations on it using operators. These are special symbols that carry out computations. You're already familiar with many of them from math class.
Arithmetic operators work on numbers to perform calculations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 10 / 2 | 5.0 |
// | Floor Division | 10 // 3 | 3 |
% | Modulus | 10 % 3 | 1 |
** | Exponent | 2 ** 3 | 8 |
Note that standard division (
/) always results in a float, while floor division (//) discards the remainder and gives an integer.
The assignment operator (=) is used to store a value in a variable. You can combine it with arithmetic operators to create a handy shortcut.
# Standard assignment
score = 10
# Add 5 to score and re-assign
score = score + 5 # score is now 15
# Shortcut version
score += 5 # score is now 20
Finally, the + operator has a special function when used with strings: it concatenates them, which is a fancy way of saying it joins them together.
first_name = "Ada"
last_name = "Lovelace"
full_name = first_name + " " + last_name
print(full_name) # Output: Ada Lovelace
Now that you have a grasp of these core concepts, it's time to test your knowledge.
In Python, what is used to define the structure of code blocks, such as loops and functions?
What is the data type of the value returned by the input() function, regardless of what the user enters?
These are the fundamental building blocks of Python. With variables, data types, and operators, you have the tools to start writing simple but powerful scripts.
