Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can write Python code, you need a place to run it. You have two main options. The first is to install Python directly on your computer. You can download the latest version from the official website, python.org. The installation is straightforward and gives you everything you need to run Python programs locally.
For a quicker start, you can use an online Python interpreter. Websites like Replit or Google Colab let you write and run Python code directly in your web browser without any installation. This is a great way to experiment and follow along with examples as you're just starting out.
Syntax and Indentation
Python is known for its clean and readable syntax. It's designed to look a lot like plain English, which makes it less intimidating for beginners. For instance, to display a message on the screen, you use the print() function.
print("Hello, Python!")
# This line is a comment. Python ignores anything after a # symbol.
One of the most unique features of Python is its use of indentation. Unlike many other programming languages that use braces {} to define blocks of code, Python uses whitespace. You must indent your code to group statements together. The standard is to use four spaces for each level of indentation.
In Python, indentation isn't just for style. It's a rule that the interpreter enforces. Getting it right is crucial for your code to run correctly.
Variables and Data Types
Think of a variable as a labeled box where you can store information. You give the box a name and put something inside it. To create a variable in Python, you simply choose a name and use the equals sign = to assign it a value.
name = "Alex"
age = 28
Here, we've created two variables. name holds the text "Alex", and age holds the number 28. Python automatically figures out what type of data you're storing. Let's look at the most common types.
Integer
noun
A whole number, without any decimal part.
Integers are for counting things. Floats, or floating-point numbers, are numbers with a decimal point. They're useful for representing quantities that can be fractional, like measurements or prices.
# An integer
items = 5
# A float
price = 19.99
Textual data is stored in strings. A string is just a sequence of characters, like a word or a sentence. You create a string by wrapping text in either single quotes (') or double quotes (").
greeting = "Welcome to Python!"
response = 'Thank you.'
Finally, we have booleans. This data type can only have one of two values: True or False. Booleans are the foundation of logic in programming and are used to make decisions in your code.
is_logged_in = True
has_permission = False
Playing with Operators
Operators are special symbols that perform operations on variables and values. You're already familiar with many of them from basic math.
Arithmetic operators are used for mathematical calculations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 6 / 3 | 2.0 |
** | Exponent | 5 ** 2 | 25 |
% | Modulo (remainder) | 5 % 3 | 2 |
Comparison operators are used to compare two values. They always result in a boolean (True or False).
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 3 | False |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 5 < 3 | False |
>= | Greater than or equal to | 5 >= 5 | True |
<= | Less than or equal to | 5 <= 3 | False |
Notice the double equals sign
==for comparison. A single equals sign=is used for assigning values to variables.
Logical operators (and, or, not) are used to combine boolean values.
# Both conditions must be True
print(5 > 3 and 1 < 2) # Output: True
# Only one condition needs to be True
print(5 > 3 or 1 > 2) # Output: True
# Inverts the boolean value
print(not True) # Output: False
How does Python define blocks of code, such as the body of a loop or function?
Which of the following is NOT a primary way to start writing and running Python code as described for a beginner?
You've just covered the absolute fundamentals of Python. With variables, data types, and operators, you have the basic tools to start building simple programs.