No history yet

Python Basics

Getting Started with Python

Python is a popular programming language known for its readability. It's designed to be straightforward, which makes it a great choice for beginners. Before you can write any code, you need to set up your programming environment.

First, you'll need to install Python itself. You can download it for free from the official website, python.org. The installer will guide you through the process. During installation, make sure to check the box that says "Add Python to PATH." This allows you to run Python from your computer's command line, a tool you'll use often.

Next, you'll want a good code editor, also known as an Integrated Development Environment (IDE). An IDE is a program that gives you a place to write your code and provides helpful tools like syntax highlighting and debugging. For beginners, an IDE like Thonny is excellent because it's designed for learning. Other popular choices include VS Code with the Python extension or PyCharm.

Lesson image

Your First Lines of Code

With your environment set up, you're ready to write some code. A long-standing tradition in programming is to make your first program print the text "Hello, World!" to the screen. In Python, this is incredibly simple.

print("Hello, World!")

The text you see, print(), is a built-in function that tells the computer to display whatever is inside the parentheses. Simple, right? This cleanness is a core feature of Python's syntax.

One of the most important rules in Python is indentation. In many programming languages, indentation is just for making code look nice. In Python, it's a strict rule. It's how Python knows which lines of code belong together in a block. You'll see this in action more when we get to loops and functions, but for now, just know that you should not indent lines of code randomly.

Indentation isn't just for style in Python; it's a rule the language enforces.

You can also leave notes for yourself or other programmers in your code. These are called comments, and they start with a # symbol. The Python interpreter completely ignores them.

# This is a comment. The computer ignores it.
print("This line will run.") # This is an inline comment.

Storing Information with Variables

Writing code often involves working with data. A variable is like a labeled box where you can store a piece of information. To create a variable, you give it a name and use the equals sign (=), called the assignment operator, to put a value inside it.

# Assign the text "Hello, Python learner!" to a variable named 'message'
message = "Hello, Python learner!"

# Print the value stored in the variable
print(message)

You can change the value of a variable by assigning it something new. This is why they're called "variables"—their contents can vary.

When naming variables, there are a few rules to follow. Names must start with a letter or an underscore, they can't start with a number, and they can only contain letters, numbers, and underscores. They are also case-sensitive, so age and Age are two different variables.

Good NamesBad NamesReason
user_nameuser nameCannot contain spaces
score11scoreCannot start with a number
total_valuetotal-valueHyphens are not allowed
greetingprintCannot be a reserved keyword

Python's Basic Data Types

Variables can hold different types of data. Python has several built-in data types, and understanding them is fundamental. For now, we'll focus on the four most common ones.

integer

noun

A whole number, positive or negative, without decimals.

# Integer examples
user_age = 30
items_in_stock = -5

When you need to represent numbers with decimal points, you use a float.

float

noun

A number that has a decimal point. The name comes from "floating-point number."

# Float examples
price = 19.99
pi_value = 3.14159

To work with text, you use strings.

string

noun

A sequence of characters, such as letters, numbers, and symbols, enclosed in single or double quotes.

# String examples
user_name = "Alice"
welcome_message = 'Welcome to the course!'

Finally, booleans are used to represent truth values.

boolean

adjective

A data type that can only have one of two values: True or False.

# Boolean examples
is_logged_in = True
has_permission = False

Notice that True and False are capitalized. Python is case-sensitive, so true and false would be treated as variable names, not boolean values.

And that's it! You've taken your first steps into Python by setting up your environment and learning about its syntax, variables, and basic data types. These are the building blocks for everything else you'll do.