No history yet

Python Basics

Setting Up Your Workspace

Before you can write any Python code, you need to set up your environment. This means installing the Python interpreter, which is the program that reads and executes your code. You can download it for free from the official Python website, python.org. Be sure to grab the latest stable version.

Once Python is installed, you need a place to write your code. While a simple text editor works, most developers use an Integrated Development Environment (IDE). An IDE is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools, and a debugger. Think of it as a specialized workshop for writing code, complete with tools that help you write, test, and fix your programs more efficiently.

Lesson image

Popular choices include Visual Studio Code, PyCharm, and even Python's built-in IDLE. For now, any of these will do. The important part is having a place to type your code and a way to run it.

Python's Clean Syntax

Python is known for its clean and readable syntax. One of its most distinctive features is its use of indentation. Unlike many other programming languages that use brackets or keywords to define blocks of code, Python uses whitespace. This means the spacing at the beginning of a line is not just for looks—it's part of the syntax and tells the interpreter how your code is structured.

Consistent indentation is mandatory in Python. Four spaces per indentation level is the standard convention.

Another key part of writing clean code is adding comments. Comments are notes for yourself or other programmers that the Python interpreter ignores. They're useful for explaining what your code does. In Python, anything following a hash symbol (#) on the same line is a comment.

# This is a comment. Python ignores it.
# The line below will be executed.
print("Hello, World!") # This is an inline comment.

Storing Information

To do anything useful, programs need to store and work with information. We do this using variables. A variable is like a labeled box where you can store a piece of data. You give the box a name, and you can put different things inside it.

user_name = "Alice"
user_age = 30

In this example, user_name and user_age are variables. The data they hold can be of different types. Python has several built-in data types, but let's start with the most common ones.

integer

noun

A whole number, positive or negative, without decimals.

Examples include -5, 0, and 120.

float

noun

A number that contains a decimal point.

Examples include 3.14, -0.01, and 99.0.

string

noun

A sequence of characters, such as text. Strings are enclosed in single (' ') or double (" ") quotes.

boolean

noun

Represents one of two values: True or False.

Interacting with Your Program

Programs aren't very useful if they can't communicate with the user. The simplest way to display information is with the print() function. Whatever you put inside the parentheses will be shown on the screen.

planet = "Earth"
print("Welcome to planet")
print(planet)

To get information from a user, you can use the input() function. It pauses the program and waits for the user to type something and press Enter. The text the user types is then returned as a string.

# Ask for the user's name and store it in a variable
name = input("What is your name? ")

# Greet the user by name
print("Hello, " + name + "!")

Making Calculations

Python can also work as a powerful calculator using operators. Arithmetic operators perform mathematical calculations.

OperatorNameExample
+Addition5 + 2 is 7
-Subtraction5 - 2 is 3
*Multiplication5 * 2 is 10
/Division5 / 2 is 2.5
//Floor Division5 // 2 is 2
%Modulus5 % 2 is 1
**Exponentiation5 ** 2 is 25

Comparison operators are used to compare two values, and they result in a boolean value: True or False.

OperatorDescriptionExample
==Equal to5 == 5 is True
!=Not equal to5 != 2 is True
>Greater than5 > 2 is True
<Less than5 < 2 is False
>=Greater than or equal to5 >= 5 is True
<=Less than or equal to5 <= 2 is False

Finally, logical operators (and, or, not) are used to combine conditional statements.

a = True
b = False

print(a and b)  # Prints False
print(a or b)   # Prints True
print(not a)    # Prints False

Let's review the key terms we've just covered.

Now, let's test your knowledge.

Quiz Questions 1/6

What is the primary purpose of indentation in Python code?

Quiz Questions 2/6

Which symbol is used to create a single-line comment in Python?

You now have the basic building blocks of Python. With variables, data types, and operators, you can start writing simple but useful programs.